summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xxorriso-usb.sh182
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
3outdev=
4volid=SamizdatLive
5gpg_iso_path=gnupghome
6gnupghome=
7secrets=secrets
8child_dir=./child
9vmlinuz_dir=isolinux/linux
10
11die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; }
12
13TEMP="$(getopt -o '' --long adam,usb,detach,out:,test -n "$0" -- "$@")" ||
14 die 'getopt error'
15eval set -- "$TEMP"
16
17ADAM=; DETACH=; USB=
18while [ $# -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
28done
29
30if [ $# = 0 ]; then
31 set -- debian-live-8.4.0-amd64-standard.btrfs layer.btrfs
32fi
33
34for fs; do
35 [ -f "$fs" ] || die "not a file: $fs"
36 shift
37 set -- "$@" "rootfs/${fs##*/}=$fs"
38done
39
40
41whole_device()
42{
43 case "$1" in
44 *-part?) false ;;
45 *-part??) false ;;
46 *-part???) false ;;
47 */usb\*) false ;;
48 *) true ;;
49 esac
50}
51
52confirm_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
63choose_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
82choose_cdrom()
83{
84 die 'choose_cdrom: unimplemented'
85}
86
87choose_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
101generate_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
116if [ grub-efi.sh -nt grub-efi ]; then
117 ./grub-efi.sh || die "grub-efi.sh failed"
118fi
119
120choose_outdev
121
122generate_keys
123
124if [ "$INPUT_DEVICE" ]; then
125 REPLACE_INITRD=
126 REMOVE_BTRFS=
127 ADD_BTRFS=
128else
129 REPLACE_INITRD=y
130 REMOVE_BTRFS=y
131 ADD_BTRFS=y
132fi
133
134if [ "$QUICK_TEST" ]; then
135 REMOVE_BTRFS=y
136 ADD_BTRFS=
137fi
138
139if [ "$REPLACE_INITRD" ]; then
140 ./initrd.sh
141fi
142
143(set -x
144xorriso \
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
176case "$outdev" in
177 *~) [ -f "$outdev" ] && mv "$outdev" "${outdev%\~}" ;;
178esac
179
180if [ "$USB" -a "$DETACH" -a $? = 0 ]; then
181 udisks --detach "$outdev"
182fi