Advertisement
Guest User

dechroot

a guest
Jul 11th, 2019
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. debootstrap='DEBOOTSTRAP_DIR=/opt/debootstrap/usr/share/debootstrap /opt/debootstrap/usr/sbin/debootstrap'
  4. debootstrap_url='http://ftp.debian.org/debian/pool/main/d/debootstrap/debootstrap_1.0.115_all.deb'
  5.  
  6. function install_debootstrap()
  7. {
  8. local curdir tmpdir
  9. echo "Installing debootstrap"
  10. tmpdir=$(mktemp -d)
  11. cd $tmpdir
  12. curl -o debootstrap.deb "$debootstrap_url"
  13. ar x debootstrap.deb
  14. sudo mkdir -p /opt/debootstrap
  15. sudo cp data.tar.gz /opt/debootstrap
  16. curdir=$(pwd)
  17. cd /opt/debootstrap
  18. sudo tar -xf data.tar.gz
  19. sudo rm -f data.tar.gz
  20. cd $curdir
  21. rm -rf $tmpdir
  22. }
  23.  
  24. function mount_filesystems()
  25. {
  26. sudo echo -n '' # just to ensure we still have sudo
  27. echo -n "Mounting filesystems..."
  28. sudo mount -t proc /proc "$1/proc"
  29. sudo mount -t sysfs /sys "$1/sys"
  30. sudo mount -o bind /dev "$1/dev"
  31. sudo mount -o bind /dev/pts "$1/dev/pts"
  32. echo " Done!"
  33. }
  34.  
  35. function unmount_filesystems()
  36. {
  37. sudo echo -n '' # just to ensure we still have sudo
  38. echo -n "Unmounting filesystems..."
  39. sudo umount "$1/proc"
  40. sudo umount "$1/sys"
  41. sudo umount "$1/dev/pts"
  42. sudo umount "$1/dev"
  43. echo " Done!"
  44. }
  45.  
  46. function install_debian()
  47. {
  48. echo "Installing Debian..."
  49. sudo bash -c "$debootstrap --arch amd64 buster '$1' http://ftp.uk.debian.org/debian/"
  50.  
  51. echo -n "Writing Debian sources file..."
  52. sudo bash -c "cat > '$1/etc/apt/sources.list'" << EOF
  53. deb http://ftp.uk.debian.org/debian/ buster main non-free contrib
  54. deb-src http://ftp.uk.debian.org/debian/ buster main non-free contrib
  55.  
  56. deb http://security.debian.org/ buster/updates main non-free contrib
  57. deb-src http://security.debian.org/ buster/updates main non-free contrib
  58.  
  59. deb http://ftp.uk.debian.org/debian/ buster-updates main non-free contrib
  60. deb-src http://ftp.uk.debian.org/debian/ buster-updates main non-free contrib
  61. EOF
  62. echo " Done!"
  63.  
  64. echo -n "Copying across hostname..."
  65. sudo cp /etc/hostname "$1/etc/hostname"
  66. echo " Done!"
  67.  
  68. mount_filesystems "$1"
  69.  
  70. echo "Performing initial package updates within chroot..."
  71. sudo chroot "$1" /bin/bash -c 'apt-get update && apt-get dist-upgrade -y'
  72. sudo chroot "$1" /bin/bash -c 'apt-get install -y locales'
  73. sudo chroot "$1" /bin/bash -c '/usr/sbin/dpkg-reconfigure locales'
  74. sudo chroot "$1" /bin/bash -c 'apt-get install -y nano sudo ncurses-term git curl wget'
  75.  
  76. echo "Creating group wheel"
  77. sudo chroot "$1" /bin/bash -c "/usr/sbin/groupadd wheel"
  78.  
  79. echo "Adding group wheel to sudoers"
  80. sudo chroot "$1" /bin/bash -c "echo '%wheel ALL=(ALL) ALL' >> /etc/sudoers"
  81.  
  82. echo "Creating user: $USER"
  83. sudo chroot "$1" /bin/bash -c "/usr/sbin/useradd -m -g users -G sudo -s /bin/bash $USER"
  84.  
  85. echo "Please enter a password for $USER..."
  86. sudo chroot "$1" /bin/bash -c "passwd $USER"
  87. echo "Done!"
  88.  
  89. unmount_filesystems "$1"
  90. }
  91.  
  92. if [ "`id -u`" -eq 0 ] ; then
  93.     echo "Please do not run this script as root."
  94.     exit 1
  95. fi
  96.  
  97. if ! wget -V > /dev/null 2>&1 ; then
  98.     echo "Please install wget. Required for debootstrap"
  99.     exit 1
  100. fi
  101.  
  102. if ! (bash -c "$debootstrap --version" > /dev/null 2>&1) ; then
  103.     install_debootstrap
  104. fi
  105.  
  106. if [[ "$1" == "" ]] ; then
  107.     echo "Please supply a chroot directory!"
  108.     exit 1
  109. fi
  110.  
  111. if [[ ! -d "$1" ]] ; then
  112.     echo "Creating directory: $1"
  113.     mkdir -p "$1"
  114.     install_debian "$1"
  115. fi
  116.  
  117. mount_filesystems "$1"
  118. sudo chroot "$1" /bin/bash -c "su $USER --login"
  119. unmount_filesystems "$1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement