#!/bin/bash export LC_ALL=C SFS_NAME="$(ls -1 ../*iron*.sfs | head -n 1 | sed -r 's#^[.][.]/##g' | sed -r 's#[.]sfs##g')" Parent_WD="$(realpath "$PWD/..")" SFS_PATH="$Parent_WD/$SFS_NAME".sfs Cont_Root=/mnt/cont CONT_NAME_SOUCE=cont #These manes must have a length greater than 1 so as to exclude "/". I'll make this more general later CONT_NAME_DEST=cont Mount_PT_ID="" #Don't edit this function umountall(){ set -x umount -l /${CONT_NAME_DEST}/tmp/.X11-unix umount -l /${CONT_NAME_DEST}/dev/pts umount -l /${CONT_NAME_DEST}/dev umount -l /${CONT_NAME_DEST}/proc umount -l /${CONT_NAME_DEST}/sys umount -l "${BIND_Target}" for to_remove in "/${CONT_NAME_DEST}"; do to_remove="$(realpath "/${to_remove}")" if [ -z "$(cat /proc/mounts | grep "${to_remove}")" ] && [ ${#to_remove} -gt 1 ]; then rm -rf ${to_remove} fi done xhost - } trap 'umountall' 1 mkdir -p /${CONT_NAME_DEST} if [ ! -z "$(ls -A "/${CONT_NAME_DEST}")" ]; then cd / CONT_NAME_DEST=$(mktemp -u ${CONT_NAME_DEST}.XXXXXXX) fi mkdir -p "/$CONT_NAME_DEST" loop=$(losetup -a | grep "$SFS_PATH" | sed "s/:.*$//" ) if [ ! -z "$loop" ]; then Mount_PT=$(findmnt -o TARGET,SOURCE -D -n | grep "${loop}\$" | cut -f1 -d' ') else Mount_PT=${CHROOT_DIR:-/mnt/cont/sfs_img} if [ ! -z "$(ls -A "$Mount_PT")" ]; then Mount_PT=$(mktemp -d -p "$Cont_Root" sfs_img.XXXXXXX) Mount_PT_ID=".${Mount_PT##*.}" fi mkdir -p "${Mount_PT}" mount -o loop "$SFS_PATH" "$Mount_PT" fi #MNTPNT=${CHROOT_DIR:-/mnt/chroot-$SFS_NAME} if [ ${#CONT_NAME_SOUCE} -le 1 ]; then BIND_Source="${Mount_PT}" else BIND_Source="${Mount_PT}/${CONT_NAME_SOUCE}" fi mkdir -p "$BIND_Source" BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID} if [ ${#Mount_PT_ID} -gt 0 ]; then BIND_Target_Root="$Cont_Root"/sfs__target${Mount_PT_ID} elif [ -z "$(ls -A "$BIND_Target_Root")" ]; then BIND_Target_Root="$Cont_Root"/sfs__target else BIND_Target_Root=$(mktemp -d -p "$Cont_Root" sfs__target.XXXXXXX) fi mkdir -p "$BIND_Target_Root" if [ ${#CONT_NAME_DEST} -gt 0 ]; then BIND_Target=$BIND_Target_Root/${CONT_NAME_DEST} else BIND_Target=$BIND_Target_Root fi mkdir -p "$BIND_Target" if [ ${#Mount_PT_ID} -gt 0 ]; then Cont_TMPFS="$Cont_Root"/tmpfs${Mount_PT_ID} elif [ -z "$(ls -A "${Cont_Root}/tmpfs")" ]; then Cont_TMPFS="$Cont_Root"/tmpfs else Cont_TMPFS=$(mktemp -d -p "$Cont_Root" tmpfs.XXXXXXX) fi mkdir -p "$Cont_TMPFS" mount -t tmpfs none "$Cont_TMPFS" #mount -t tmpfs none $BIND_Target_Root; #NEW='' #MOUNTED_PUP_RO=$(busybox df | grep -o '/initrd/pup_ro.*') ## pup_ro1 and pup_ro2 are reserved #for i in $(seq 3 99) # find free pup_roX #do # if ! [ "$(echo "$MOUNTED_PUP_RO" | grep "pup_ro${i}$")" ] ; then # NEW=${i} # break # fi #done #Cont_Layer=/initrd/pup_ro$NEW mount --bind "${BIND_Source}" "${BIND_Target}" #ln -s "$BIND_Target_Root" "$Cont_Layer" set +x read -p "Press enter to continue" set -x busybox mount -t aufs -o "udba=reval,diropq=w,br:${Cont_TMPFS}=rw:${BIND_Target}=rr" aufs "/$CONT_NAME_DEST" || { umountall && exit 1; } #busybox mount -t aufs -o remount,append:$BIND_Target_Root=rr / || { umountall && exit 1; } mount --bind /dev /${CONT_NAME_DEST}/dev mount --bind /proc /${CONT_NAME_DEST}/proc mount --bind /sys /${CONT_NAME_DEST}/sys mount -t devpts devpts /${CONT_NAME_DEST}/dev/pts cp /etc/resolv.conf /${CONT_NAME_DEST}/etc/resolv.conf cp /var/lib/dbus/machine-id /${CONT_NAME_DEST}/var/lib/dbus/machine-id xhost + mkdir -p /${CONT_NAME_DEST}/tmp/.X11-unix mount --rbind /tmp/.X11-unix /${CONT_NAME_DEST}/tmp/.X11-unix set +x read -p "Ready to chroot Press enter to continue" chroot /${CONT_NAME_DEST} iron "$@" set -x umountall