diff options
-rwxr-xr-x | xorriso-usb.sh | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/xorriso-usb.sh b/xorriso-usb.sh new file mode 100755 index 0000000..3f7df8e --- /dev/null +++ b/xorriso-usb.sh | |||
@@ -0,0 +1,182 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | outdev= | ||
4 | volid=SamizdatLive | ||
5 | gpg_iso_path=gnupghome | ||
6 | gnupghome= | ||
7 | secrets=secrets | ||
8 | child_dir=./child | ||
9 | vmlinuz_dir=isolinux/linux | ||
10 | |||
11 | die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } | ||
12 | |||
13 | TEMP="$(getopt -o '' --long adam,usb,detach,out:,test -n "$0" -- "$@")" || | ||
14 | die 'getopt error' | ||
15 | eval set -- "$TEMP" | ||
16 | |||
17 | ADAM=; DETACH=; USB= | ||
18 | while [ $# -gt 0 ]; do | ||
19 | case "$1" in | ||
20 | --adam) shift; ADAM=y;; | ||
21 | --usb) shift; USB=y;; | ||
22 | --detach) shift; DETACH=y;; | ||
23 | --test) shift; QUICK_TEST=y;; | ||
24 | --out) CMDLINE_OUTDEV="$2"; shift 2;; | ||
25 | --) shift; break;; | ||
26 | *) die 'getopt error';; | ||
27 | esac | ||
28 | done | ||
29 | |||
30 | if [ $# = 0 ]; then | ||
31 | set -- debian-live-8.4.0-amd64-standard.btrfs layer.btrfs | ||
32 | fi | ||
33 | |||
34 | for fs; do | ||
35 | [ -f "$fs" ] || die "not a file: $fs" | ||
36 | shift | ||
37 | set -- "$@" "rootfs/${fs##*/}=$fs" | ||
38 | done | ||
39 | |||
40 | |||
41 | whole_device() | ||
42 | { | ||
43 | case "$1" in | ||
44 | *-part?) false ;; | ||
45 | *-part??) false ;; | ||
46 | *-part???) false ;; | ||
47 | */usb\*) false ;; | ||
48 | *) true ;; | ||
49 | esac | ||
50 | } | ||
51 | |||
52 | confirm_usb() | ||
53 | { | ||
54 | local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? " | ||
55 | printf "$msg" "$1" >&2 | ||
56 | read line | ||
57 | case "$line" in | ||
58 | [yY][eE][sS]) return ;; | ||
59 | *) die "Aborted by user." ;; | ||
60 | esac | ||
61 | } | ||
62 | |||
63 | choose_usb() | ||
64 | { | ||
65 | local devs maj | ||
66 | set -- /dev/disk/by-id/usb* | ||
67 | for dev; do | ||
68 | shift | ||
69 | whole_device "$dev" || continue | ||
70 | set -- "$@" "$dev" | ||
71 | done | ||
72 | if [ $# = 0 ]; then | ||
73 | die "no usb device found" | ||
74 | elif [ $# = 1 ]; then | ||
75 | confirm_usb "$1" || die impossible | ||
76 | outdev="$1" | ||
77 | else | ||
78 | die "multiple USB devices connected and choice between them is unimplemented. ($*)" | ||
79 | fi | ||
80 | } | ||
81 | |||
82 | choose_cdrom() | ||
83 | { | ||
84 | die 'choose_cdrom: unimplemented' | ||
85 | } | ||
86 | |||
87 | choose_outdev() | ||
88 | { | ||
89 | if [ "$CMDLINE_OUTDEV" ]; then | ||
90 | outdev=$CMDLINE_OUTDEV~ | ||
91 | NEED_STDIO=y | ||
92 | elif [ "$USB" ]; then | ||
93 | choose_usb | ||
94 | NEED_STDIO=y | ||
95 | else | ||
96 | choose_cdrom | ||
97 | NEED_STDIO= | ||
98 | fi | ||
99 | } | ||
100 | |||
101 | generate_keys() | ||
102 | { | ||
103 | if [ "$ADAM" ]; then | ||
104 | kiki init || die 'kiki init failed' | ||
105 | gnupghome=/root/.gnupg | ||
106 | else | ||
107 | ./keygen.sh "$child_dir" || die "keygen.sh failed" | ||
108 | gnupghome=$child_dir/root/.gnupg | ||
109 | trap 'umount "$child_dir"; rmdir "$child_dir"' EXIT | ||
110 | fi | ||
111 | } | ||
112 | |||
113 | |||
114 | [ "$(id -u)" = 0 ] || die "you are not root." | ||
115 | |||
116 | if [ grub-efi.sh -nt grub-efi ]; then | ||
117 | ./grub-efi.sh || die "grub-efi.sh failed" | ||
118 | fi | ||
119 | |||
120 | choose_outdev | ||
121 | |||
122 | generate_keys | ||
123 | |||
124 | if [ "$INPUT_DEVICE" ]; then | ||
125 | REPLACE_INITRD= | ||
126 | REMOVE_BTRFS= | ||
127 | ADD_BTRFS= | ||
128 | else | ||
129 | REPLACE_INITRD=y | ||
130 | REMOVE_BTRFS=y | ||
131 | ADD_BTRFS=y | ||
132 | fi | ||
133 | |||
134 | if [ "$QUICK_TEST" ]; then | ||
135 | REMOVE_BTRFS=y | ||
136 | ADD_BTRFS= | ||
137 | fi | ||
138 | |||
139 | if [ "$REPLACE_INITRD" ]; then | ||
140 | ./initrd.sh | ||
141 | fi | ||
142 | |||
143 | (set -x | ||
144 | xorriso \ | ||
145 | ${INPUT_DEVICE:+ -indev "$INPUT_DEVICE" } \ | ||
146 | -outdev ${NEED_STDIO:+stdio:}"$outdev" \ | ||
147 | -blank as_needed \ | ||
148 | -report_about mishap \ | ||
149 | -return_with sorry 0 \ | ||
150 | -volid "$volid" \ | ||
151 | -pathspecs on \ | ||
152 | \ | ||
153 | \ | ||
154 | ${REPLACE_INITRD:+ -rm_r linux -- -add linux="${vmlinuz_dir}" -- } \ | ||
155 | ${REMOVE_BTRFS:+ -rm_r btrfs -- } \ | ||
156 | ${ADD_BTRFS:+ -follow link -add "$@" -- -follow default } \ | ||
157 | \ | ||
158 | \ | ||
159 | -rm_r "${gpg_iso_path}" -- \ | ||
160 | -add "${gpg_iso_path}=${gnupghome}" -- \ | ||
161 | \ | ||
162 | \ | ||
163 | -chown_r 0 / -- \ | ||
164 | -chgrp_r 0 / -- \ | ||
165 | -chmod_r go-rwx "${gpg_iso_path}" -- \ | ||
166 | \ | ||
167 | \ | ||
168 | -as mkisofs -graft-points \ | ||
169 | -b grub/i386-pc/eltorito.img \ | ||
170 | -no-emul-boot -boot-info-table \ | ||
171 | --embedded-boot grub-efi/embedded.img \ | ||
172 | --protective-msdos-label \ | ||
173 | grub=grub-efi/grub | ||
174 | ) || die "xorriso exited $?" | ||
175 | |||
176 | case "$outdev" in | ||
177 | *~) [ -f "$outdev" ] && mv "$outdev" "${outdev%\~}" ;; | ||
178 | esac | ||
179 | |||
180 | if [ "$USB" -a "$DETACH" -a $? = 0 ]; then | ||
181 | udisks --detach "$outdev" | ||
182 | fi | ||