Browse Source

rewrite with functions

pull/1/head
Markus Bergholz 6 years ago
parent
commit
6b405322f0
1 changed files with 131 additions and 62 deletions
  1. +131
    -62
      superchroot

+ 131
- 62
superchroot View File

@@ -1,89 +1,158 @@
#!/bin/bash
# WTFPL

terminal="/bin/bash"
set -e
set -e

if (("$#" == 1)); then
## functions
# ==========

if [[ "$1" = "--help" ]]; then
echo '
superchroot <folder>
superchroot [chroot-option] <folder>
set_chroot (){

[chroot options]: systemd (root), chroot (root), fakechroot, proot, exit (umount directories from chroot methode)
if [[ "$option" = "exit" ]]; then
printf "trying to umount used directories from chroot option...\n\n"
chroot_umount "$@"

elif [[ "$option" = "proot" ]]; then
printf "forcing proot method...\n\n"
proot -r $folder

EXAMPLES - force method
~$: su -c "superchroot systemd arch/"
~$: sudo superchroot chroot arch/
~$: superchroot proot arch/
~$: superchroot fakechroot arch/
elif [[ "$option" = "fakechroot" ]]; then
printf "forcing fakechroot method...\n\n"
export FAKECHROOT_EXCLUDE_PATH=/proc:/dev:/sys
fakechroot -s fakeroot chroot $folder

EXAMPLES - automaticaly best choice
~#: superchroot arch/
~$: superchroot arch/
'
exit
fi
elif [[ "$option" = "systemd" ]]; then
printf "forcing systemd method...\n\n"
systemd-nspawn -D $folder

elif [[ "$option" = "chroot" ]]; then
printf "forcing chroot method...\n\n"
mount -t devtmpfs /dev $folder/dev
mount -t devpts /dev/pts $folder/dev/pts
mount -t sysfs /sys $folder/sys
mount -t proc /proc $folder/proc
chroot $folder $terminal

elif [[ "$option" = "arch" ]]; then
printf "forcing arch-chroot method...\n\n"
arch-chroot $folder

if [ "$UID" -ne 0 ]; then
else
printf "unknown option\n"
fi

}

super_chroot(){

if [ "$UID" -ne 0 ]; then

if which fakechroot >/dev/null; then
echo -e "fakechroot found...\n"
printf "fakechroot found...\n"
export FAKECHROOT_EXCLUDE_PATH=/proc:/dev:/sys
fakechroot -s fakeroot chroot $1
fakechroot -s fakeroot chroot $folder

elif which proot >/dev/null; then
echo -e "proot found ...\n"
proot -r $1
printf "proot found ...\n"
proot -r $folder

else
echo -e "I can not find fakechroot nor proot, nor are root. Sorry, but I can\'t chroot.\n"
printf "I can not find fakechroot nor proot, nor are root. Sorry, but I can\'t chroot.\n"
fi
else

if which systemd-nspawn >/dev/null; then
systemd-nspawn -D $1
systemd-nspawn -D $folder

elif which arch-chroot >/dev/null; then
arch-chroot $1
arch-chroot $folder

else
echo -e "\nRunning classic chroot with mount -t... \n\n"
mount -t devtmpfs /dev $1/dev
mount -t devpts /dev/pts $1/dev/pts
mount -t sysfs /sys $1/sys
mount -t proc /proc $1/proc
chroot $1 $terminal
printf "\nRunning classic chroot with mount -t... \n\n"
chroot_mount "$folder"
chroot $folder $terminal
fi
fi

}




chroot_mount() {

mount proc "$folder/proc" -t proc -o nosuid,noexec,nodev &&
mount sys "$folder/sys" -t sysfs -o nosuid,noexec,nodev,ro &&
mount udev "$folder/dev" -t devtmpfs -o mode=0755,nosuid &&
mount devpts "$folder/dev/pts" -t devpts -o mode=0620,gid=5,nosuid,noexec &&
mount shm "$folder/dev/shm" -t tmpfs -o mode=1777,nosuid,nodev &&
mount run "$folder/run" -t tmpfs -o nosuid,nodev,mode=0755 &&
mount tmp "$folder/tmp" -t tmpfs -o mode=1777,strictatime,nodev,nosuid

}

chroot_umount() {

printf "Trying to umount chroot folder ...\n"
umount "$folder/proc"
umount "$folder/sys"
umount "$folder/dev"
umount "$folder/dev/pts"
umount "$folder/dev/shm"
umount "$folder/run"
umount "$folder/tmp"

}

print_help(){

echo '
superchroot <folder>
superchroot [chroot-option] <folder>

[chroot options]: systemd (root), chroot (root), arch (root), fakechroot, proot, exit (umount directories from chroot methode)


EXAMPLES - force method
~$: su -c "superchroot systemd arch/"
~$: sudo superchroot chroot arch/
~$: superchroot proot arch/
~$: superchroot fakechroot arch/

EXAMPLES - automaticaly best choice
~#: superchroot arch/
~$: superchroot arch/
'

}

## Variable
## ========

terminal="/bin/bash"


## MAIN
## ====

if (("$#" == 1)); then

if [[ "$1" = "--help" ]]; then
print_help
exit
fi

folder=$1
super_chroot "$@"

elif (("$#" == 2)); then

if [[ "$1" = "exit" ]]; then
echo -e "trying to umount used directories from chroot option...\n\n"
umount $2/dev
umount $2/dev/pts
umount $2/sys
umount $2/proc
elif [[ "$1" = "proot" ]]; then
echo -e "forcing proot method...\n\n"
proot -r $2
elif [[ "$1" = "fakechroot" ]]; then
echo -e "forcing fakechroot method...\n\n"
export FAKECHROOT_EXCLUDE_PATH=/proc:/dev:/sys
fakechroot -s fakeroot chroot $2
elif [[ "$1" = "systemd" ]]; then
echo -e "forcing systemd method...\n\n"
systemd-nspawn -D $2
elif [[ "$1" = "chroot" ]]; then
echo -e "forcing chroot method...\n\n"
mount -t devtmpfs /dev $2/dev
mount -t devpts /dev/pts $2/dev/pts
mount -t sysfs /sys $2/sys
mount -t proc /proc $2/proc
chroot $2 $terminal
elif [[ "$1" = "arch" ]]; then
arch-chroot $2
else
echo -e "unknown option\n"
fi
option=$1
folder=$2
set_chroot "$@"
else
echo -e "wrong usage.\n"
printf "Wrong usage!\n\n"
print_help
exit
fi


Loading…
Cancel
Save