]> git.wh0rd.org - home.git/blobdiff - .bin/custom-chroot
le-renew: switch to cryptography module
[home.git] / .bin / custom-chroot
index 113ccfad31e27faff8574e351cb3dff325bb23ab..67da2eaa286fedeae1350c457bd906903d347aef 100755 (executable)
@@ -1,12 +1,18 @@
 #!/bin/bash -e
 
 bootstrap() {
-       [[ -w / ]] || exec sudo env -uUNSHARE HOME="$HOME" "$0" "$@"
+       [[ $(id -u) -eq 0 ]] || exec sudo env -uUNSHARE HOME="$HOME" "$0" "$@"
 
        if [[ -z ${UNSHARE} ]] ; then
                mount_args=
                if type -P unshare >&/dev/null ; then
-                       UNSHARE=true exec unshare -m -- "$0" "$@"
+                       test_arg() { unshare "$@" -- true >&/dev/null && uargs+=( "$@" ) || :; }
+                       uargs=( -m )
+                       test_arg -u
+                       test_arg -i
+                       test_arg -p -f --mount-proc
+                       test_arg --propagation=private
+                       UNSHARE=true exec unshare "${uargs[@]}" -- "$0" "$@"
                fi
        else
                mount_args='-n'
@@ -31,6 +37,13 @@ get_type() {
        *64-bit*PowerPC*) echo ppc64;;
        *32-bit*S/390*)   echo s390;;
        *64-bit*S/390*)   echo s390x;;
+       *64-bit*MIPS*)    echo mips64;;
+       *32-bit*MIPS*N32*)echo mips64;;
+       *32-bit*MIPS*)    echo mips;;
+       *32-bit*PA-RISC*) echo parisc;;
+       *64-bit*PA-RISC*) echo parisc64;;
+       *32-bit*SPARC*)   echo sparc;;
+       *64-bit*SPARC*)   echo sparc64;;
        esac
 }
 
@@ -38,10 +51,9 @@ init_chroot() {
        [[ -w . ]] || return 0
 
        if [[ ! -L etc/mtab ]] ; then
-               rm -f etc/mtab
-               ln -sf /proc/mounts etc/mtab
+               ln -sfT /proc/mounts etc/mtab
        fi
-       local f
+       local f dst
        local etc=(
                hosts
                locale.gen
@@ -57,14 +69,24 @@ init_chroot() {
        for f in \
                $(printf 'etc/%s ' "${etc[@]}") \
        ; do
-               if [ -e "/${f}" ] ; then
-                       cp /${f} ${f}
+               if [[ -e /${f} ]] ; then
+                       cp "/${f}" "${f}"
                fi
        done
        for f in "${home[@]}" ; do
-               cp "${HOME}/${f}" "root/${f}"
+               df="root/${f}"
+               f="${HOME}/${f}"
+               if [[ -e ${f} ]] ; then
+                       cp "${f}" "${df}"
+               fi
        done
-       cat ~/.profile.d/aliases.sh > root/.bash_profile
+
+       if [[ ! -d root/.git ]] ; then
+               f="${HOME}/.profile.d/aliases.sh"
+               if [[ -e ${f} ]] ; then
+                       cat "${f}" > root/.bash_profile
+               fi
+       fi
 }
 
 usage() {
@@ -86,7 +108,7 @@ usage() {
 main() {
        bootstrap "$@"
 
-       local mounts=( proc sys tmp dev dev/pts usr/portage usr/portage/distfiles usr/local/src )
+       local mounts=( proc sys tmp dev dev/pts dev/shm run usr/portage usr/portage/distfiles usr/local/src )
 
        local chroot=${0%/*}
        case ${chroot} in
@@ -100,7 +122,7 @@ main() {
                -u) cmd='umount' ;;
                -m) mounts+=( "$2" ); shift ;;
                -d) chroot=$(realpath "$2"); shift ;;
-               -h) usage ;;
+               --help|-h) usage ;;
                -*) echo "${0##*/}: unknown option $1"; exit 1 ;;
                *)  break ;;
                esac
@@ -127,6 +149,8 @@ main() {
                fi
        fi
 
+       # Doubtful these settings we want to leak into the chroot.
+       unset ROOT PORTAGE_CONFIGROOT LD_LIBRARY_PATH
        unset LS_COLORS # format changes over time
        [[ $# -eq 0 ]] && set -- env HOME=/root /bin/bash -l
        exec \