summaryrefslogtreecommitdiff
path: root/src/xorriso-usb.sh
diff options
context:
space:
mode:
Diffstat (limited to 'src/xorriso-usb.sh')
-rw-r--r--src/xorriso-usb.sh182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/xorriso-usb.sh b/src/xorriso-usb.sh
new file mode 100644
index 0000000..61fcda0
--- /dev/null
+++ b/src/xorriso-usb.sh
@@ -0,0 +1,182 @@
1#!/bin/bash
2
3. samizdat-paths.sh || exit 1
4
5outdev=
6volid=SamizdatLive
7gpg_iso_path=gnupghome
8gnupghome=
9child_dir=$samizdat_child_dir
10vmlinuz_dir=$samizdat_isolinux_dir
11efi_dir=$samizdat_grub_efi_dir
12
13die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; }
14
15TEMP="$(getopt -o '' --long adam,usb,detach,out:,test -n "$0" -- "$@")" ||
16 die 'getopt error'
17eval set -- "$TEMP"
18
19ADAM=; DETACH=; USB=
20while [ $# -gt 0 ]; do
21 case "$1" in
22 --adam) shift; ADAM=y;;
23 --usb) shift; USB=y;;
24 --detach) shift; DETACH=y;;
25 --test) shift; QUICK_TEST=y;;
26 --out) CMDLINE_OUTDEV="$2"; shift 2;;
27 --) shift; break;;
28 *) die 'getopt error';;
29 esac
30done
31
32if [ $# = 0 ]; then
33 set -- debian-live-8.4.0-amd64-standard.btrfs layer.btrfs
34fi
35
36for fs; do
37 [ -f "$fs" ] || die "not a file: $fs"
38 shift
39 set -- "$@" "rootfs/${fs##*/}=$fs"
40done
41
42
43whole_device()
44{
45 case "$1" in
46 *-part?) false ;;
47 *-part??) false ;;
48 *-part???) false ;;
49 */usb\*) false ;;
50 *) true ;;
51 esac
52}
53
54confirm_usb()
55{
56 local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? "
57 printf "$msg" "$1" >&2
58 read line
59 case "$line" in
60 [yY][eE][sS]) return ;;
61 *) die "Aborted by user." ;;
62 esac
63}
64
65choose_usb()
66{
67 local devs maj
68 set -- /dev/disk/by-id/usb*
69 for dev; do
70 shift
71 whole_device "$dev" || continue
72 set -- "$@" "$dev"
73 done
74 if [ $# = 0 ]; then
75 die "no usb device found"
76 elif [ $# = 1 ]; then
77 confirm_usb "$1" || die impossible
78 outdev="$1"
79 else
80 die "multiple USB devices connected and choice between them is unimplemented. ($*)"
81 fi
82}
83
84choose_cdrom()
85{
86 die 'choose_cdrom: unimplemented'
87}
88
89choose_outdev()
90{
91 if [ "$CMDLINE_OUTDEV" ]; then
92 outdev=$CMDLINE_OUTDEV~
93 NEED_STDIO=y
94 elif [ "$USB" ]; then
95 choose_usb
96 NEED_STDIO=y
97 else
98 choose_cdrom
99 NEED_STDIO=
100 fi
101}
102
103generate_keys()
104{
105 if [ "$ADAM" ]; then
106 kiki init || die 'kiki init failed'
107 gnupghome=/root/.gnupg
108 else
109 keygen.sh "$child_dir" || die "keygen.sh failed"
110 gnupghome=$child_dir/root/.gnupg
111 trap 'umount "$child_dir"; rmdir "$child_dir"' EXIT
112 fi
113}
114
115
116[ "$(id -u)" = 0 ] || die "you are not root."
117
118grub-efi.sh || die "grub-efi.sh failed"
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 "${efi_dir}"/embedded.img \
172 --protective-msdos-label \
173 grub="${efi_dir}"/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