summaryrefslogtreecommitdiff
path: root/src/xorriso-usb.sh
blob: ef26cb215348357ac77cde70a36a088601604d2e (plain)
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
219
220
221
222
223
224
#!/bin/bash

. samizdat-paths.sh || exit 1

outdev=
volid=SamizdatLive
gpg_iso_path=gnupghome
gnupghome=
child_dir=$samizdat_child_dir/child.$$
vmlinuz_dir=$samizdat_linux_dir
efi_dir=$samizdat_grub_efi_dir

die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; }

TEMP="$(getopt -o '' --long adam,usb,detach,in:,out:,test -n "$0" -- "$@")" ||
    die 'getopt error'
eval set -- "$TEMP"

ADAM=; DETACH=; USB=
while [ $# -gt 0 ]; do
    case "$1" in
        --adam)   shift; ADAM=y;;
        --usb)    shift; USB=y;;
        --detach) shift; DETACH=y;;
        --test)   shift; QUICK_TEST=y;;
        --out)    CMDLINE_OUTDEV="$2"; shift 2;;
        --in)     INPUT_DEVICE="$2"; shift 2;;
        --)       shift; break;;
        *) die 'getopt error';;
    esac
done

if [ -z "$INPUT_DEVICE" -a $# = 0 ]; then
    if mountpoint -q /cdrom; then
        INPUT_DEVICE=/cdrom
    elif [ -e debian-live-8.4.0-amd64-standard.btrfs -a -e layer.btrfs ]; then
        set -- debian-live-8.4.0-amd64-standard.btrfs layer.btrfs
    else
        die "no input device and no input btrfs layers: aborting."
    fi
fi

for fs; do
    [ -f "$fs" ] || die "not a file: $fs"
    case "$fs" in
        *.btrfs) ;;
        *) die "invalid name (does not match *.btrfs): $fs" ;;
    esac
    shift
    set -- "$@" "rootfs/${fs##*/}=$fs"
done


whole_device()
{
    case "$1" in
        *-part?) false ;;
        *-part??) false ;;
        *-part???) false ;;
        */usb\*) false ;;
        *) true ;;
    esac
}

confirm_usb()
{
    local msg="This will completely overwrite device:\n\n\t%s\n\nType 'yes' to confirm.\nContinue? "
    printf "$msg" "$1" >&2
    read line
    case "$line" in
        [yY][eE][sS]) return ;;
        *) die "Aborted by user." ;;
    esac
}

choose_usb()
{
    local devs maj
    set -- /dev/disk/by-id/usb*
    for dev; do
        shift
        whole_device "$dev" || continue
        set -- "$@" "$dev"
    done
    if [ $# = 0 ]; then
        die "no usb device found"
    elif [ $# = 1 ]; then
        confirm_usb "$1" || die impossible
        outdev="$1"
    else
        die "multiple USB devices connected and choice between them is unimplemented.  ($*)"
    fi
}

choose_cdrom()
{
    die 'choose_cdrom: unimplemented'
}

choose_outdev()
{
    if [ "$CMDLINE_OUTDEV" ]; then
        outdev=$CMDLINE_OUTDEV~
        NEED_STDIO=y
    elif [ "$USB" ]; then
        choose_usb
        NEED_STDIO=y
    else
        choose_cdrom
        NEED_STDIO=
    fi
}

generate_keys()
{
    if [ "$ADAM" ]; then
        kiki init || die 'kiki init failed'
        gnupghome=/root/.gnupg
    else
        keygen.sh "$child_dir" || die "keygen.sh failed"
        gnupghome=$child_dir/root/.gnupg
        trap 'umount "$child_dir"; rmdir "$child_dir"' EXIT
    fi
}


[ "$(id -u)" = 0 ] || die "you are not root."

grub-efi.sh || die "grub-efi.sh failed"

choose_outdev

generate_keys

try_mount()
{
    # TODO: don't use /mnt
    # Ofc the real solution is to get help with xorriso.
    NEED_UNMOUNT="$1"
    mount -r "$1" /mnt && trap 'umount -l /mnt' EXIT
}

if [ "$INPUT_DEVICE" ]; then
    if [ "$INPUT_DEVICE" = /dev/md55 ]; then
        if mountpoint -q /cdrom; then
            XORRISO_IS_DRIVING_ME_INSANE='-add /=/cdrom --'
        fi
    elif [ -d "$INPUT_DEVICE" ]; then
        XORRISO_IS_DRIVING_ME_INSANE="-add /=$INPUT_DEVICE --" # TODO: escape
    elif try_mount "$INPUT_DEVICE"; then
        XORRISO_IS_DRIVING_ME_INSANE='-add /=/mnt --'
    else
        REPLACE_INITRD=
        REMOVE_BTRFS=
        ADD_BTRFS=
    fi
else
    REPLACE_INITRD=y
    REMOVE_BTRFS=y
    ADD_BTRFS=y
fi

if [ "$QUICK_TEST" ]; then
    REMOVE_BTRFS=y
    ADD_BTRFS=
fi

if [ "$REPLACE_INITRD" ]; then
    # TODO: fix the paths so this backup isn't needed. What we need to do is
    # support tftp pointing to the cdrom. This probably means a tftp directory
    # that contains a symlink to /cdrom/linux which gets updated on success here
    mv "${vmlinuz_dir}" "${vmlinuz_dir}".bak
    initrd.sh || {
        mv "${vmlinuz_dir}.bak" "${vmlinuz_dir}"
        die 'initrd.sh failed'
    }
fi

SILENT=

(set -x
xorriso \
        ${disable____INPUT_DEVICE:+ -indev "$INPUT_DEVICE" } \
        -outdev ${NEED_STDIO:+stdio:}"$outdev" \
        -blank as_needed \
        ${SILENT:+ -report_about mishap} \
        -return_with sorry 0 \
        -volid "$volid" \
        -pathspecs on \
        \
        \
        ${XORRISO_IS_DRIVING_ME_INSANE} \
        \
        \
        ${REPLACE_INITRD:+ -rm_r linux -- -add linux="${vmlinuz_dir}" -- } \
        ${REMOVE_BTRFS:+   -rm_r rootfs -- } \
        ${ADD_BTRFS:+      -follow link -add "$@" -- -follow default } \
        \
        \
        -rm_r "${gpg_iso_path}" -- \
        -add "${gpg_iso_path}=${gnupghome}" -- \
        \
        \
        -chown_r 0 / -- \
        -chgrp_r 0 / -- \
        -chmod_r go-rwx "${gpg_iso_path}" -- \
        \
        \
        -as mkisofs -graft-points \
        -b grub/i386-pc/eltorito.img  \
        -no-emul-boot -boot-info-table \
        --embedded-boot "${efi_dir}"/embedded.img \
        --protective-msdos-label \
        grub="${efi_dir}"/grub

) || die "xorriso exited $?"

case "$outdev" in
    *~) [ -f "$outdev" ] && mv "$outdev" "${outdev%\~}" ;;
esac

if [ "$USB" -a "$DETACH" -a $? = 0 ]; then
    udisks --detach "$outdev"
fi