diff options
Diffstat (limited to 'src/xorriso-usb.sh')
-rwxr-xr-x | src/xorriso-usb.sh | 295 |
1 files changed, 0 insertions, 295 deletions
diff --git a/src/xorriso-usb.sh b/src/xorriso-usb.sh deleted file mode 100755 index 436c8fe..0000000 --- a/src/xorriso-usb.sh +++ /dev/null | |||
@@ -1,295 +0,0 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } | ||
4 | |||
5 | whole_device() | ||
6 | { | ||
7 | case "$1" in | ||
8 | *-part?) false ;; | ||
9 | *-part??) false ;; | ||
10 | *-part???) false ;; | ||
11 | */usb\*) false ;; | ||
12 | *) true ;; | ||
13 | esac | ||
14 | } | ||
15 | |||
16 | confirm_usb() | ||
17 | { | ||
18 | local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? " | ||
19 | printf "$msg" "$1" >&2 | ||
20 | read line | ||
21 | case "$line" in | ||
22 | [yY][eE][sS]) return ;; | ||
23 | *) die "Aborted by user." ;; | ||
24 | esac | ||
25 | } | ||
26 | |||
27 | choose_usb() | ||
28 | { | ||
29 | local devs maj | ||
30 | set -- /dev/disk/by-id/usb* | ||
31 | for dev; do | ||
32 | shift | ||
33 | whole_device "$dev" || continue | ||
34 | set -- "$@" "$dev" | ||
35 | done | ||
36 | if [ $# = 0 ]; then | ||
37 | die "no usb device found" | ||
38 | elif [ $# = 1 ]; then | ||
39 | confirm_usb "$1" || die impossible | ||
40 | outdev="$1" | ||
41 | else | ||
42 | die "multiple USB devices connected and choice between them is unimplemented. ($*)" | ||
43 | fi | ||
44 | } | ||
45 | |||
46 | choose_cdrom() | ||
47 | { | ||
48 | die 'choose_cdrom: unimplemented; specify cdrom device with --out' | ||
49 | } | ||
50 | |||
51 | choose_outdev() | ||
52 | { | ||
53 | if [ "$CMDLINE_OUTDEV" ]; then | ||
54 | outdev=$CMDLINE_OUTDEV~ | ||
55 | NEED_STDIO=y | ||
56 | elif [ "$USB" ]; then | ||
57 | choose_usb | ||
58 | NEED_STDIO=y | ||
59 | else | ||
60 | choose_cdrom | ||
61 | NEED_STDIO= | ||
62 | fi | ||
63 | } | ||
64 | |||
65 | generate_keys() | ||
66 | { | ||
67 | if [ "$ADAM" -o "$BOOTLOADER_ONLY" ]; then | ||
68 | kiki init || die 'kiki init failed' | ||
69 | GPG_INPUT_DIR=/root/.gnupg | ||
70 | else | ||
71 | keygen.sh "$child_dir" || die "keygen.sh failed" | ||
72 | GPG_INPUT_DIR=$child_dir/root/.gnupg | ||
73 | trap 'umount "$child_dir"; rmdir "$child_dir"' EXIT | ||
74 | fi | ||
75 | } | ||
76 | |||
77 | try_mount() | ||
78 | { | ||
79 | # TODO: don't use /mnt | ||
80 | # Ofc the real solution is to get help with xorriso. | ||
81 | NEED_UNMOUNT="$1" | ||
82 | mount -r "$1" /mnt && trap 'umount -l /mnt' EXIT | ||
83 | } | ||
84 | |||
85 | verbosely() | ||
86 | { | ||
87 | (set -x; "$@") | ||
88 | } | ||
89 | |||
90 | xorriso_cmd() | ||
91 | { | ||
92 | # input variables: | ||
93 | |||
94 | ## that do not affect the output ISO image: | ||
95 | |||
96 | # SHOW_XORRISO_CMD | ||
97 | # SILENT | ||
98 | # NEED_STDIO | ||
99 | |||
100 | ## that do not vary by invocation: | ||
101 | |||
102 | # volid | ||
103 | # vmlinuz_dir | ||
104 | # efi_dir | ||
105 | # gpg_iso_path | ||
106 | |||
107 | ## that specify sources of input: | ||
108 | |||
109 | # INPUT_DEVICE | ||
110 | # GPG_INPUT_DIR | ||
111 | # $@ (btrfs filesystems) | ||
112 | # EXTRA_INPUT_DIRS | ||
113 | |||
114 | ## boolean flags | ||
115 | |||
116 | # REMOVE_BTRFS | ||
117 | # ADD_BTRFS | ||
118 | |||
119 | ${SHOW_XORRISO_CMD:+ verbosely} ${NO_ACT:+ :} \ | ||
120 | xorrisofs -iso-level 3 -- \ | ||
121 | ${INPUT_DEVICE:+ -indev "$INPUT_DEVICE" } \ | ||
122 | -outdev ${NEED_STDIO:+stdio:}"$outdev" \ | ||
123 | -blank as_needed \ | ||
124 | ${SILENT:+ -report_about mishap} \ | ||
125 | -return_with sorry 0 \ | ||
126 | -volid "$volid" \ | ||
127 | -pathspecs on \ | ||
128 | \ | ||
129 | \ | ||
130 | ${EXTRA_INPUT_DIRS} \ | ||
131 | \ | ||
132 | \ | ||
133 | -rm_r linux -- -add linux="${vmlinuz_dir}" -- \ | ||
134 | ${REMOVE_BTRFS:+ -rm_r rootfs -- } \ | ||
135 | ${ADD_BTRFS:+ -follow link -add "$@" -- -follow default } \ | ||
136 | \ | ||
137 | \ | ||
138 | -rm_r "${gpg_iso_path}" -- \ | ||
139 | -add "${gpg_iso_path}=${GPG_INPUT_DIR}" -- \ | ||
140 | \ | ||
141 | \ | ||
142 | -chown_r 0 / -- \ | ||
143 | -chgrp_r 0 / -- \ | ||
144 | -chmod_r go-rwx "${gpg_iso_path}" -- \ | ||
145 | \ | ||
146 | \ | ||
147 | -as mkisofs -graft-points \ | ||
148 | -b grub/i386-pc/eltorito.img \ | ||
149 | -no-emul-boot -boot-info-table \ | ||
150 | --embedded-boot "${efi_dir}"/embedded.img \ | ||
151 | --protective-msdos-label \ | ||
152 | grub="${efi_dir}"/grub | ||
153 | } | ||
154 | |||
155 | run_xorriso() | ||
156 | { | ||
157 | xorriso_cmd "$@" || die "xorriso exited $?" | ||
158 | |||
159 | case "$outdev" in | ||
160 | *~) [ -f "$outdev" ] && mv "$outdev" "${outdev%\~}" ;; | ||
161 | esac | ||
162 | |||
163 | if [ "$USB" -a "$DETACH" -a $? = 0 ]; then | ||
164 | udisks --detach "$outdev" | ||
165 | fi | ||
166 | } | ||
167 | |||
168 | find_child() | ||
169 | { | ||
170 | # TODO: Lookup by IP address, MAC address | ||
171 | printf '%s' "$samizdat_child_dir"/child."$1" | ||
172 | } | ||
173 | |||
174 | . samizdat-paths.sh || die 'samizdat-path.sh not found' | ||
175 | |||
176 | if [ -f xorriso-usb.config ]; then | ||
177 | . xorriso-usb.config | ||
178 | fi | ||
179 | |||
180 | volid=SamizdatLive | ||
181 | vmlinuz_dir=$samizdat_linux_dir | ||
182 | efi_dir=$samizdat_grub_efi_dir | ||
183 | gpg_iso_path=gnupghome | ||
184 | |||
185 | |||
186 | outdev= | ||
187 | GPG_INPUT_DIR= | ||
188 | child_dir=$samizdat_child_dir/child.$$ | ||
189 | |||
190 | OPT=$(getopt -o '' --long 'bootloader,reuse-child:,adam,usb,detach,in:,out:,test' -n "$0" -- "$@") || die 'getopt error' | ||
191 | eval set -- "$OPT" | ||
192 | unset OPT | ||
193 | |||
194 | ADAM=; DETACH=; USB=y | ||
195 | while [ $# -gt 0 ]; do | ||
196 | case "$1" in | ||
197 | --) shift; break;; | ||
198 | --adam) shift; ADAM=y;; | ||
199 | --bootloader) shift; export BOOTLOADER_ONLY=y;; | ||
200 | --cdrom) shift; USB=;; | ||
201 | --detach) shift; DETACH=y;; | ||
202 | --in) INPUT_DEVICE="$2"; shift 2;; | ||
203 | --out) CMDLINE_OUTDEV="$2"; USB=; shift 2;; | ||
204 | --reuse-child) REUSE_CHILD=y; child_dir="$(find_child "$2")"; shift 2;; | ||
205 | --test) shift; QUICK_TEST=y;; | ||
206 | *) die 'getopt error';; | ||
207 | esac | ||
208 | done | ||
209 | |||
210 | if [ "$INPUT_DEVICE" ]; then | ||
211 | die "support for --in is disabled because xorriso needs different arguments to produce a bootable image" | ||
212 | |||
213 | elif [ $# = 0 -a -z "$BOOTLOADER_ONLY" ]; then | ||
214 | if mountpoint -q /cdrom; then | ||
215 | INPUT_DEVICE=/cdrom | ||
216 | elif [ -e /srv/nbd.btrfs ]; then | ||
217 | set -- /srv/nbd.btrfs | ||
218 | elif [ "$(blockdev --getsz /dev/nbd0)" -gt 0 ]; then | ||
219 | (set -x; dd if=/dev/nbd0 of=/srv/nbd.btrfs~ && mv /srv/nbd.btrfs~ /srv/nbd.btrfs) || die "failed to copy network block device" | ||
220 | set -- /srv/nbd.btrfs | ||
221 | else | ||
222 | die "no input device and no input btrfs layers: aborting." | ||
223 | fi | ||
224 | fi | ||
225 | |||
226 | for fs; do | ||
227 | [ -f "$fs" ] || die "not a file: $fs" | ||
228 | case "$fs" in | ||
229 | *.btrfs) ;; | ||
230 | *) die "invalid name (does not match *.btrfs): $fs" ;; | ||
231 | esac | ||
232 | shift | ||
233 | set -- "$@" "rootfs/${fs##*/}=$fs" | ||
234 | done | ||
235 | |||
236 | |||
237 | [ "$(id -u)" = 0 ] || die "you are not root." | ||
238 | |||
239 | grub-efi.sh || die "grub-efi.sh failed" | ||
240 | |||
241 | choose_outdev | ||
242 | |||
243 | if [ "$REUSE_CHILD" ]; then | ||
244 | GPG_INPUT_DIR=$child_dir/root/.gnupg | ||
245 | [ -d "$GPG_INPUT_DIR" ] || die "invalid child" | ||
246 | else | ||
247 | generate_keys | ||
248 | fi | ||
249 | |||
250 | if [ "$INPUT_DEVICE" ]; then | ||
251 | if [ "$INPUT_DEVICE" = /dev/md55 ]; then | ||
252 | if mountpoint -q /cdrom; then | ||
253 | EXTRA_INPUT_DIRS='-add /=/cdrom --' | ||
254 | fi | ||
255 | elif [ -d "$INPUT_DEVICE" ]; then | ||
256 | EXTRA_INPUT_DIRS="-add /=$INPUT_DEVICE --" # TODO: escape | ||
257 | elif try_mount "$INPUT_DEVICE"; then | ||
258 | EXTRA_INPUT_DIRS='-add /=/mnt --' | ||
259 | else | ||
260 | REPLACE_INITRD= | ||
261 | REMOVE_BTRFS= | ||
262 | ADD_BTRFS= | ||
263 | fi | ||
264 | elif [ "$BOOTLOADER_ONLY" ]; then | ||
265 | REPLACE_INITRD= | ||
266 | REMOVE_BTRFS= | ||
267 | ADD_BTRFS= | ||
268 | else | ||
269 | REPLACE_INITRD=y | ||
270 | REMOVE_BTRFS=y | ||
271 | ADD_BTRFS=y | ||
272 | fi | ||
273 | REPLACE_INITRD= # TODO: fix initrd replacing | ||
274 | |||
275 | if [ "$QUICK_TEST" ]; then | ||
276 | REMOVE_BTRFS=y | ||
277 | ADD_BTRFS= | ||
278 | fi | ||
279 | |||
280 | if [ "$REPLACE_INITRD" ]; then | ||
281 | # TODO: fix the paths so this backup isn't needed. What we need to do is | ||
282 | # support tftp pointing to the cdrom. This probably means a tftp directory | ||
283 | # that contains a symlink to /cdrom/linux which gets updated on success here | ||
284 | mv "${vmlinuz_dir}" "${vmlinuz_dir}".bak | ||
285 | mkdir "${vmlinuz_dir}" | ||
286 | initrd.sh || { | ||
287 | mv "${vmlinuz_dir}.bak" "${vmlinuz_dir}" | ||
288 | die 'initrd.sh failed' | ||
289 | } | ||
290 | fi | ||
291 | |||
292 | SILENT= | ||
293 | SHOW_XORRISO_CMD=y | ||
294 | |||
295 | run_xorriso "$@" | ||