forked from SFSLiveBoot/LiveBootUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-to-disk.sh
More file actions
executable file
·218 lines (197 loc) · 7.18 KB
/
Copy pathinstall-to-disk.sh
File metadata and controls
executable file
·218 lines (197 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh
: ${wd:=$(dirname "$0")}
. "$wd/scripts/common.func"
run_as_root "$@"
trap_fail
tgt="${1%%/}"
test -n "$tgt" || {
tgt_dev=$(for blk in /sys/class/block/*/partition;do
blk_dir="${blk%/partition}"
blk_dev="/dev/${blk_dir##*/}"
blk_blkid="$(cat "$blk_dir"/dev)"
echo "$blk_dev"
blk_mnt="$(blkid2mnt "$blk_blkid" || true)"
echo "${blk_mnt:-Not mounted}"
echo "$(blkid -o value -s TYPE "$blk_dev")"
echo "$(blkid -o value -s LABEL "$blk_dev")"
done | zenity --list --text="Select install destination" --column Partition --column Mountpoint --column Type --column "Volume label")
}
test ! -b "$tgt" || { tgt_dev="$tgt"; tgt=""; }
test -z "$tgt_dev" || if ! tgt="$(blkid2mnt "$(mountpoint -x "$tgt_dev")")";then
tgt="$(mktemp -d /tmp/install-to-disk.$$.XXXXXX)"
mount "$tgt_dev" "$tgt"
fi
mountpoint "$tgt"
dst_dev="$(mnt2dev "$tgt")"
tgt_uuid="$(blkid -s UUID -o value "$dst_dev")"
dst_disk="/dev/$(part2disk "$dst_dev")"
test -n "$tgt_uuid" || {
echo "Cannot figure out UUID for $dst_dev, aborting" >&2
exit 1
}
if storage_dev="$(blkid -o device -t LABEL=Storage | grep "^$dst_disk")";then
storage_uuid="$(blkid -s UUID -o value "$storage_dev")"
fi
: ${dist:=$2}
if test -z "$dist";then
part_binsh="$(echo /.parts/*/bin/sh)"
test ! -e "$part_binsh" || {
root_loop="$(mnt2dev "${part_binsh%/bin/sh}")"
root_sfs="$(cat /sys/block/${root_loop#/dev/}/loop/backing_file)"
dist="${root_sfs%/*}"
dist="${dist##*/}"
}
fi
: ${dist:=live}
: ${kver:=`uname -r`}
: ${arch:=`uname -m`}
no_act() {
if test -n "$no_act";then echo "No-act: $1" >&2
else return 1;fi
}
list_parts() {
local mnt="${1:-/}" aufs_si br_file part_dir
aufs_si="$(grep " $mnt aufs " /proc/mounts | grep -o si=[0-9a-f]* | cut -f2 -d=)"
if test -n "$aufs_si";then
for br_file in /sys/fs/aufs/si_${aufs_si}/br[0-9]*; do
read part_dir <"$br_file"
part_dir="${part_dir%=*}"
echo "$part_dir"
done
else
grep " $mnt overlay " /proc/mounts | grep -o "lowerdir=[^,]*" | cut -f2 -d= | tr : \\n
fi
}
copy_root_parts() {
local dst="$1" part_dev part_file
: ${kernel_dst:=$dst/$arch/vmlinuz-$kver}
: ${initrd_dst:=$dst/$arch/ramdisk-$kver}
for part_mnt in $(list_parts /); do
part_dev="$(mnt2dev "$part_mnt")" || continue
case "$part_dev" in
/dev/loop*) part_dev="${part_dev#/dev/}";;
*) continue;;
esac
read part_file < /sys/block/$part_dev/loop/backing_file
test -r "$part_file" || {
echo -n "Cannot read '$part_file'.. " >&2
case "$part_file" in
*.sfs.*) part_file="${part_file%.sfs.*}.sfs";;
*" (deleted)") part_file="${part_file% (deleted)}";;
esac
if test -r "$part_file";then
echo "using $part_file instead." >&2
else
echo "skipping." >&2
continue
fi
}
part_sfs="${part_file##*/}"
part_fname="${part_sfs%.sfs*}.sfs"
part_name="${part_sfs%.sfs*}"
part_name="${part_name#[0-9][0-9]-}"
test -z "$exclude_sfs" || {
is_excluded=""
for exclude_test in $exclude_sfs; do
case "$part_name" in $exclude_test) is_excluded="$exclude_test"; break;; esac
case "$part_fname" in $exclude_test) is_excluded="$exclude_test"; break;; esac
case "$part_file" in $exclude_test) is_excluded="$exclude_test"; break;; esac
done
test -z "$is_excluded" || {
echo "Skipping '$part_fname' matching exclusion pattern '$exclude_test'" >&2
continue
}
}
part_srcdir="${part_file%/*}"
case "$part_sfs" in
*.sfs.*)
part_file_t="${part_file%.sfs.*}.sfs"
part_sfs="${part_file_t##*/}"
test ! -e "$part_file_t" || part_file="${part_file_t}"
;;
esac
test ! -e "$part_srcdir/logo.png" -o -e "$dst/logo.png" ||
no_act "Copying logo from $part_srcdir" ||
cp -v "$part_srcdir/logo.png" "$dst/logo.png"
case "$part_sfs" in
*"$kver"*)
part_dstdir="$arch"
test -n "$kernel" -o ! -r "$part_srcdir/vmlinuz-$kver" ||
kernel="$part_srcdir/vmlinuz-$kver"
;;
*)
part_dstdir="${part_srcdir##*/}"
test -z "$part_dst_sed" || part_dstdir="$(echo "$part_dstdir" | sed -e "$part_dst_sed")"
if test "x$part_dstdir" = "x$dist" -o "x$part_dstdir" = "xlive";then part_dstdir=""; else
case "$part_dirlist" in
"$part_dstdir"|"$part_dstdir "*|*" $part_dstdir"|*" $part_dstdir "*) ;;
*) part_dirlist="${part_dirlist:+$part_dirlist }$part_dstdir" ;;
esac
fi;;
esac
test -d "$dst${part_dstdir:+/$part_dstdir}" || no_act "Creating $dst/$part_dstdir" || mkdir -p "$dst${part_dstdir:+/$part_dstdir}"
part_dst="$dst${part_dstdir:+/$part_dstdir}/$part_sfs"
test -e "$part_dst" &&
test "$(sfs_stamp "$part_dst")" -ge "$(sfs_stamp "$part_file")" &&
echo "Newer or same version $part_dst ($(date --date=@$(sfs_stamp "$part_dst") +%y%m%d_%H%M%S)) already exists." || no_act "Copying $part_file" || {
cat_file "$part_file" >"${part_dst}.NEW.$$"
replace_sfs "${part_dst}.NEW.$$" "$part_dst"
}
done
if no_act "Installing kernel kver=$kver";then return 0; fi
test -e "$kernel_dst" -o -n "$kernel" -o ! -r "/boot/vmlinuz-$kver" || kernel="/boot/vmlinuz-$kver"
test -e "$kernel_dst" -o -z "$kernel" || {
mkdir -p "$dst/$arch/"
cp -v "$kernel" "$dst/$arch/"
}
test -e "$initrd_dst" || {
for initrd in "$initrd" "${kernel%/*}/ramdisk-$kver" /boot/ramdisk-$kver /live/$arch/ramdisk-$kver /live/ramdisk-$kver;do
test ! -r "$initrd" || break
done
while true;do
if test -r "$initrd";then
cp -v "$initrd" "$initrd_dst"
test ! -r "${initrd%/*}/ramdisk_net-$kver" ||
cp -vi "${initrd%/*}/ramdisk_net-$kver" "${initrd_dst%/*}/ramdisk_net-$kver"
break
else
echo -n "initrd 'ramdisk-$kver' path: "
read initrd
test ! -d "$initrd" || initrd="$initrd/ramdisk-$kver"
fi
done
}
}
install_grub() {
local tgt="$1" dst_disk="$2"
echo -n "installing grub to ${dst_disk}.. "
grub-install \
$(case "$(grub-install --help)" in *--target=*) echo --target=i386-pc;;esac) \
--boot-directory="$tgt/boot" \
"$dst_disk"
cp /usr/share/grub/ascii.pf2 "$tgt/boot/grub"
echo "source /grub.cfg" > "$tgt/boot/grub/grub.cfg"
install_grub_efi "$tgt"
}
create_grub_cfg() {
local tgt="$1" dist="$2" dq='"'
cat >"$tgt/grubvars.cfg" <<EOF
set dist="$dist"
set kver="$kver"
set arch="$arch"
set root_uuid="$tgt_uuid"
set storage_uuid="$storage_uuid"
$(case "$(cat /sys/class/dmi/id/product_name)" in "Latitude E6520") echo "set append=\"reboot=pci\"";;esac)
${part_dirlist:+set extras=$dq$part_dirlist$dq}
EOF
cp "$wd/scripts/grub.cfg" "$tgt"
}
no_act "Creating $tgt/$dist and $tgt/boot/grub" || mkdir -p "$tgt/$dist" "$tgt/boot/grub"
no_act "Installing grub to $dst_disk" || install_grub "$tgt" "$dst_disk"
copy_root_parts "$tgt/$dist"
case "$(mnt2dev "$tgt" 3)" in
ext*) no_act "Converting .sfs files to symlinks" || "$(dirname "$0")/scripts/conv-sfs-to-links.sh" "$tgt/$dist";;
esac
no_act "Creating grub.cfg" || create_grub_cfg "$tgt" "$dist"
case "$tgt" in /tmp/install-to-disk.$$.*) echo -n "Unmounting $tgt.. "; umount "$tgt"; echo "Done."; rmdir "$tgt";; esac
exit_succ