#!/bin/bash . samizdat-paths.sh || exit 1 outdev= volid=SamizdatLive gpg_iso_path=gnupghome gnupghome= child_dir=$samizdat_child_dir 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