#!/bin/sh -e sinfo() { echo ${BASH:+-e} "\033[1;33;41m$@\033[0m" } # Must have root directory as an argument if [ -z "$1" ]; then echo "$0 [copy] [-c 'shell command']" exit 1 fi # Launch in a separate mount namespace if [ unshare != "$1" ]; then exec unshare -m -- "$0" unshare "$@" else shift fi # Variables SRC=`dirname $0`/src RSYNC="rsync -rlptOHS" version=`cat ${SRC}/../conf/version` environment="LVERSION=${version} LOGNAME=root USER=root HOME=/root HOSTNAME=liberte" for env in LANG TERM RSYNC_PROXY http_proxy https_proxy ftp_proxy no_proxy; do if printenv ${env} 1>/dev/null; then environment="${environment} ${env}=`printenv ${env}`" fi done # Tweak target directory if alternative is given, # otherwise, synchronize configuration if [ "$2" = copy ]; then PHASE=$2 LIVECD=$1/${PHASE} shift 2 else PHASE=src LIVECD=$1/${PHASE} shift # NOTE: non-pruned files: /, /etc/portage/gnupg, /etc/privoxy, /var/lib/portage # /home/anon/persist/security/pgp/*.{gpg,crt} # Specify permissions completely, to be independent from ${SRC}/* chmods sinfo "Copying configuration files:" ${RSYNC} --chmod=u=rwX,go=rX -i \ --exclude=/root/ \ --exclude=/home/anon/ \ --exclude=/home/nofw/ \ --exclude=/var/lib/portage/ \ --exclude=/usr/local/portage/ \ --exclude=/etc/sudoers \ --exclude=/etc/sysctl.conf \ --exclude=/etc/polkit-1 \ --exclude=/etc/portage/gnupg/ -C ${SRC}/ ${LIVECD} ${RSYNC} --chmod=u=rwX,go=rX -i --delete-excluded \ -C --exclude=.gitignore ${SRC}/root ${LIVECD} ${RSYNC} --chmod=u=rwX,go= -i -C ${SRC}/etc/portage/gnupg ${LIVECD}/etc/portage ${RSYNC} --chmod=u=rwX,go=rX,Dg+s -i -C ${SRC}/var/lib/portage ${LIVECD}/var/lib ${RSYNC} --chmod=u=rwX,go=rX -i --delete --exclude=/portage/distfiles -C ${SRC}/usr/local/portage ${LIVECD}/usr/local if [ -e ${LIVECD}/var/log/privoxy ]; then chown -hR --from=`stat -c %u:%g ${LIVECD}/var/log/privoxy` 0:0 ${LIVECD}/etc/privoxy fi ${RSYNC} --chmod=ug=r,o= -i ${SRC}/etc/sudoers ${LIVECD}/etc ${RSYNC} --chmod=u=rw,g=r,o= -i ${SRC}/etc/sysctl.conf ${LIVECD}/etc if [ -e ${LIVECD}/etc/polkit-1/rules.d ]; then ${RSYNC} --chmod=u=rwX,go=rX -i -C \ ${SRC}/etc/polkit-1/rules.d/* ${LIVECD}/etc/polkit-1/rules.d fi mkdir -p -m 755 ${LIVECD}/usr/local/addons ${RSYNC} --chmod=u=rwX,go=rX -i --delete-excluded -C ${SRC}/../conf/certs ${LIVECD}/usr/local/addons # rwX------ ${RSYNC} --chmod=u=rwX,go= -i --delete \ --exclude=/anon/persist/security/pgp/\*.gpg --exclude=/anon/persist/security/pgp/\*.crt \ --exclude=/anon/persist/cables/queue --exclude=/anon/persist/cables/rqueue \ --exclude=/anon/persist/mail/inbox -C ${SRC}/home/anon ${LIVECD}/home ${RSYNC} --chmod=ug=rwX,o=,Do+t -i --delete-excluded \ -C ${SRC}/home/anon/persist/cables/queue ${SRC}/home/anon/persist/cables/rqueue \ ${LIVECD}/home/anon/persist/cables ${RSYNC} --chmod=ug=rwX,o=,Do+t -i --delete-excluded \ -C ${SRC}/home/anon/persist/mail/inbox ${LIVECD}/home/anon/persist/mail chown -hR --from=0:0 2101:9000 ${LIVECD}/home/anon ${RSYNC} --chmod=u=rwX,go= -i --delete-excluded -C ${SRC}/home/nofw ${LIVECD}/home chown -hR --from=0:0 2102:9001 ${LIVECD}/home/nofw if [ -e /etc/resolv.conf ]; then ${RSYNC} -L --chmod=u=rw,go=r -i /etc/resolv.conf ${LIVECD}/etc fi fi environment="${environment} PHASE=${PHASE}" setarch=`which setarch` chroot=`which chroot` tty=`tty` mounted= sinfo "Mounting system directories" [ "${tty#/dev/pts/}" = "${tty}" ] || tty= [ -L ${LIVECD}/dev/fd ] || ln -sfT /proc/self/fd ${LIVECD}/dev/fd loop=`losetup -f` for mp in /proc /dev/null /dev/random /dev/urandom /dev/tty /dev/ptmx /dev/pts /dev/shm ${loop} ${tty}; do if [ ${mp#/dev/loop} = ${mp} ]; then lmp=${LIVECD}${mp} else lmp=${LIVECD}/dev/loop fi if [ ! -e ${lmp} ]; then if [ -d ${mp} ]; then mkdir -m 755 ${lmp} elif [ -e ${mp} ]; then touch ${lmp} fi fi mount -B ${mp} ${lmp} mounted="${lmp} ${mounted}" done mount -rB -o remount ${LIVECD}/proc if [ ${PHASE} = src ]; then mkdir -p -m 755 ${LIVECD}/../copy ${LIVECD}/../dist mount -B ${LIVECD}/../copy ${LIVECD}/mnt/live mount -B ${LIVECD}/../dist ${LIVECD}/mnt/boot elif [ ${PHASE} = copy -a -d ${LIVECD}/usr/portage ]; then mount -B ${LIVECD}/../src/usr/portage ${LIVECD}/usr/portage mount -rB -o remount ${LIVECD}/usr/portage fi sinfo "Environment:" echo "${environment}" | tr ' ' '\n' | sed 's/^/ /' sinfo "Launching chrooted shell in ${LIVECD}" set +e env -i ${environment} "${setarch}" i686 "${chroot}" ${LIVECD} /bin/bash -l "$@" retval=$? if [ ${retval} != 0 ]; then sinfo "Failed." fi # Unnecessary with unshare -m, but stale processes might hold the mounts sinfo "Unmounting system directories" if [ ${PHASE} = src ]; then umount ${LIVECD}/mnt/live ${LIVECD}/mnt/boot elif [ ${PHASE} = copy -a -d ${LIVECD}/usr/portage ]; then umount ${LIVECD}/usr/portage fi umount -l ${mounted} # Condition is false for empty and for block-device ${tty} (i.e., unmount failed) [ ! -f ${LIVECD}${tty} ] || rm ${LIVECD}${tty} exit ${retval}