From f41c17e3d07debf0c6a5f9023115561bf56a6f24 Mon Sep 17 00:00:00 2001 From: Eric Hameleers Date: Mon, 20 Jun 2016 11:10:58 +0200 Subject: Allow persistence and (luks) containers in subdirectories. --- iso2usb.sh | 34 ++++++++++++++++++++++++++++------ liveinit | 10 +++++----- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/iso2usb.sh b/iso2usb.sh index acc7839..ad118f9 100644 --- a/iso2usb.sh +++ b/iso2usb.sh @@ -28,7 +28,8 @@ FORCE=0 # By default, we use 'persistence' as the name of the persistence directory, # or 'persistence.img' as the name of the persistence container: -PERSISTENCE="persistence" +DEF_PERSISTENCE="persistence" +PERSISTENCE="${DEF_PERSISTENCE}" # Default persistence type is a directory: PERSISTTYPE="dir" @@ -40,6 +41,7 @@ UNATTENDED=0 VERBOSE=0 # Variables to store content from an initrd we are going to refresh: +OLDPERSISTENCE="" OLDWAIT="" OLDLUKS="" @@ -84,7 +86,7 @@ cleanup() { # In case of failure, only the most recent device should still be open: if mount |grep -q ${CNTDEV} ; then umount -f ${CNTDEV} - cryptsetup luksClose ${CNTBASE} + cryptsetup luksClose $(basename ${CNTBASE}) losetup -d ${LODEV} fi fi @@ -148,6 +150,7 @@ uncompressfs () { read_initrd() { IMGFILE="$1" + OLDPERSISTENCE=$(uncompressfs ${IMGFILE} |cpio -i --to-stdout init |grep "^PERSISTENCE" |cut -d '"' -f2 2>/dev/null) OLDWAIT=$(uncompressfs ${IMGFILE} |cpio -i --to-stdout wait-for-root 2>/dev/null) OLDLUKS=$(uncompressfs ${IMGFILE} |cpio -i --to-stdout luksdev 2>/dev/null) } @@ -182,6 +185,15 @@ update_initrd() { echo "--- Refreshing Slackware initrd..." WAIT="$OLDWAIT" echo "$OLDLUKS" >> luksdev + if [ "${PERSISTENCE}" != "${DEF_PERSISTENCE}" ]; then + # If the user specified a nonstandard persistence, use that: + sed -i -e "s,^PERSISTENCE=.*,PERSISTENCE=\"${PERSISTENCE}\"," init + elif ["${PERSISTENCE}" != "${OLDPERSISTENCE}" ]; then + # The user did not specify persistence, re-use the custome value: + sed -i -e "s,^PERSISTENCE=.*,PERSISTENCE=\"${OLDPERSISTENCE}\"," init + echo "--- Updating 'persistence' from '$PERSISTENCE' to '$OLDPERSISTENCE':" + PERSISTENCE="${OLDPERSISTENCE}" + fi else echo "--- Updating 'waitforroot' time from '$OLDWAIT' to '$WAIT':" fi @@ -253,6 +265,7 @@ create_container() { fi echo "--- Creating ${CNTSIZE} MB container file using 'dd if=/dev/urandom', patience please..." + mkdir -p $USBMNT/$(dirname "${CNTBASE}") CNTFILE="${CNTBASE}.img" # Create a sparse file (not allocating any space yet): dd of=$USBMNT/${CNTFILE} bs=1M count=0 seek=$CNTSIZE @@ -266,8 +279,8 @@ create_container() { cryptsetup -y luksFormat $LODEV # Unlock the LUKS encrypted container: echo "--- Unlocking the LUKS container requires your passphrase again..." - cryptsetup luksOpen $LODEV ${CNTBASE} - CNTDEV=/dev/mapper/${CNTBASE} + cryptsetup luksOpen $LODEV $(basename ${CNTBASE}) + CNTDEV=/dev/mapper/$(basename ${CNTBASE}) # Now we allocate blocks for the LUKS device. We write encrypted zeroes, # so that the file looks randomly filled from the outside. # Take care not to write more bytes than the internal size of the container: @@ -305,7 +318,7 @@ create_container() { # Don't forget to clean up after ourselves: if [ "${CNTENCR}" = "luks" ]; then - cryptsetup luksClose ${CNTBASE} + cryptsetup luksClose $(basename ${CNTBASE}) fi losetup -d ${LODEV} || true @@ -580,11 +593,20 @@ if [ $DOLUKS -eq 1 ]; then LUKSHOME=${CNTFILE} fi -# Update the initrd with longer USB wait time and LUKS /home info: +# Update the initrd with regard to USB wait time, persistence and LUKS. +# If this is a refresh and anything changed to persistence, then the +# variable $PERSISTENCE will have the correct value when exing this call: update_initrd ${USBMNT}/boot/initrd.img if [ $REFRESH -eq 1 ]; then # Determine what we need to do with persistence if this is a refresh. + if [ "${PERSISTENCE}" != "${OLDPERSISTENCE}" ]; then + # The user specified a nonstandard persistence, so move the old one first; + # hide any errors if it did not *yet* exist: + mkdir -p ${USBMNT}/$(dirname ${PERSISTENCE}) + mv ${USBMNT}/${OLDPERSISTENCE}.img ${USBMNT}/${PERSISTENCE}.img 2>/dev/null + mv ${USBMNT}/${OLDPERSISTENCE} ${USBMNT}/${PERSISTENCE} 2>/dev/null + fi if [ -f ${USBMNT}/${PERSISTENCE}.img ]; then # If a persistence container exists, we re-use it: PERSISTTYPE="file" diff --git a/liveinit b/liveinit index 43c8a9c..d42b9ce 100755 --- a/liveinit +++ b/liveinit @@ -691,19 +691,19 @@ if [ "$RESCUE" = "" ]; then mount -o remount,rw /mnt/media # Find a free loop device to mount the persistence container file: prdev=$(find_loop) - prdir=${PERSISTENCE}_$(od -An -N1 -tu1 /dev/urandom |tr -d ' ') + prdir=$(basename ${PERSISTENCE})_$(od -An -N1 -tu1 /dev/urandom |tr -d ' ') mkdir -p /mnt/live/${prdir} losetup $prdev /mnt/media/${PERSISTENCE}.img # Check if the persistence container is LUKS encrypted: if cryptsetup isLuks $prdev 1>/dev/null 2>/dev/null ; then echo "Unlocking LUKS encrypted persistence file '/${PERSISTENCE}.img'" - cryptsetup luksOpen $prdev ${PERSISTENCE} /dev/tty0 2>&1 + cryptsetup luksOpen $prdev $(basename ${PERSISTENCE}) /dev/tty0 2>&1 if [ $? -ne 0 ]; then echo "${MARKER}: Failed to unlock persistence file '/${PERSISTENCE}.img'." echo "${MARKER}: Falling back to RAM." else # LUKS properly unlocked; from now on use the mapper device instead: - prdev=/dev/mapper/${PERSISTENCE} + prdev=/dev/mapper/$(basename ${PERSISTENCE}) fi fi prfs=$(blkid $prdev |rev |cut -d'"' -f2 |rev) @@ -714,10 +714,10 @@ if [ "$RESCUE" = "" ]; then else if [ "$WIPE_PERSISTENCE" = "1" ]; then echo "${MARKER}: Wiping existing persistent data in '/${PERSISTENCE}.img'." - find /mnt/live/${prdir}/${PERSISTENCE}/ -mindepth 1 -exec rm -rf {} \; 2>/dev/null + find /mnt/live/${prdir}/$(basename ${PERSISTENCE})/ -mindepth 1 -exec rm -rf {} \; 2>/dev/null fi echo "${MARKER}: Writing persistent changes to file '/${PERSISTENCE}.img'." - UPPERDIR=/mnt/live/${prdir}/${PERSISTENCE} + UPPERDIR=/mnt/live/${prdir}/$(basename ${PERSISTENCE}) OVLWORK=/mnt/live/${prdir}/.ovlwork fi fi -- cgit v1.2.3