summaryrefslogtreecommitdiff
path: root/src/xorriso-usb.sh
blob: 01fd43638cccba98bc6829e5341f0f3b1a332f4e (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
#!/bin/bash

. samizdat-paths.sh || exit 1

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

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

TEMP="$(getopt -o '' --long adam,usb,detach,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;;
        --)       shift; break;;
        *) die 'getopt error';;
    esac
done

if [ $# = 0 ]; then
    set -- debian-live-8.4.0-amd64-standard.btrfs layer.btrfs
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

if [ "$INPUT_DEVICE" ]; then
    REPLACE_INITRD=
    REMOVE_BTRFS=
    ADD_BTRFS=
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
    initrd.sh || die 'initrd.sh failed'
fi

(set -x
xorriso \
        ${INPUT_DEVICE:+ -indev "$INPUT_DEVICE" } \
        -outdev ${NEED_STDIO:+stdio:}"$outdev" \
        -blank as_needed \
        -report_about mishap \
        -return_with sorry 0 \
        -volid "$volid" \
        -pathspecs on \
        \
        \
        ${REPLACE_INITRD:+ -rm_r linux -- -add linux="${vmlinuz_dir}" -- } \
        ${REMOVE_BTRFS:+   -rm_r btrfs -- } \
        ${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