Last active
October 29, 2022 19:38
-
-
Save KsaRedFx/2b4863e5d18eea57a6ca to your computer and use it in GitHub Desktop.
Install Ubuntu with debootstrap and chroot, without rebooting the host machine or using a iso
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "--------------------------------------------------------------------------------------" | |
echo "This script builds a full operating system with debootstrap and chroot" | |
echo "To use this script, please mount an empty partition or alternate disk" | |
echo "" | |
echo "If you are using btrfs, and using subvolumes, mount the root subvolume explicity" | |
echo "Example: mount /dev/sdb1 /media/bootstrap -o subvol=@" | |
echo "Failure to do this will cause the script to break. It needs the root directory" | |
echo "" | |
echo "The script is specifically built for Ubuntu" | |
echo "--------------------------------------------------------------------------------------" | |
sleep 1 | |
echo "Starting" | |
echo "Location to work from? (Ex: /media/bootstrap) This must be a mounted empty partition or disk" | |
read startpoint | |
echo "Version of Ubuntu you wish to install? (Ex: trusty)" | |
read flavour | |
startpoint=${startpoint%/} | |
oldflavour=`lsb_release -cs` | |
apt-get install debootstrap | |
ln -s /usr/share/debootstrap/scripts/gutsy /usr/share/debootstrap/scripts/$flavour | |
debootstrap $flavour $startpoint | |
cd $startpoint | |
info=`mount | cut -f 1,3,5,6 -d ' ' | grep $startpoint` | |
info=(${info// / }) && device=${info[0]} && part=${info[0]} && filesystem=${info[2]} && settings=${info[3]} | |
ORIG=$IFS && IFS="/" && part=($part) && disk=${part[2]:0:3} && IFS=$ORIG | |
uuid=`blkid | sed -n "/${part[2]}/s/.*UUID=\"\([^\"]*\)\".*/\1/p"` | |
swap=`blkid | grep "swap" | sed -n "/$disk/s/.*UUID=\"\([^\"]*\)\".*/\1/p"` | |
if [ $filesystem == 'btrfs' ]; then | |
echo "Found filesystem $filesystem" | |
subvol=$(sed 's/.*subvol=\(.\).*/\1/g' <<< $settings) | |
if [ -z "$subvol" ]; then | |
echo "Could not find a subvolume, do you wish to continue? (y/n)" | |
read reply | |
if [ $reply != "y" ]; then | |
exit | |
else | |
echo "Continuing" | |
fi | |
else | |
echo "Found subvolume $subvol" | |
extras="subvol=$subvol" | |
fi | |
elif [ $filesystem == 'ntfs' ]; then | |
echo "Found filesystem $filesystem" | |
id=`grep plugdev $startpoint/etc/group | cut -f 3 -d ':'` | |
extras="umask=007,gid=$id" | |
else | |
echo "Found filesystem $filesystem" | |
fi | |
echo "Writing fstab" | |
if [ -z "$extras" ]; then | |
extras="defaults" | |
else | |
extras="defaults,$extras" | |
fi | |
echo "UUID=$uuid / $filesystem $extras 0 2" > $startpoint/etc/fstab | |
if [ -z "$swap" ]; then | |
echo "No swap partitions found" | |
else | |
echo "UUID=$swap none swap sw 0 0" >> $startpoint/etc/fstab | |
fi | |
echo "Setting up hosts" | |
echo "Please input the name you wish to use for this machine (Ex: bobs-pc)" | |
reply="" && read reply | |
if [ -z "$reply" ]; then | |
echo $reply > $startpoint/etc/hostname | |
else | |
echo $flavour > $startpoint/etc/hostname | |
fi | |
echo "Setting up interfaces" | |
echo "Do you wish to copy your current interfaces? (only useful when not installing to a usb or external hdd) (y/n)" | |
reply="" && read reply | |
if [ $reply == "y" ]; then | |
cp /etc/network/interfaces $startpoint/etc/network/interfaces | |
else | |
echo "auto lo" > $startpoint/etc/network/interfaces | |
echo "iface lo inet loopback" >> $startpoint/etc/network/interfaces | |
fi | |
echo "Setting up sources" | |
cp /etc/apt/sources.list $startpoint/etc/apt/sources.list | |
sed -i "s/$oldflavour/$flavour/g" $startpoint/etc/apt/sources.list | |
sed -i "s/old-releases.ubuntu.com/archive.ubuntu.com/g" $startpoint/etc/apt/sources.list | |
sed -i "/security/s/archive.ubuntu.com/security.ubuntu.com/g" $startpoint/etc/apt/sources.list | |
echo "Prepping devices and mounting required systems" | |
for x in dev proc sys dev/pts ; do | |
mount --bind /$x $startpoint/$x | |
done | |
echo "Updating repositories" | |
echo 'apt-get update && apt-get upgrade && apt-get install language-pack-en-base aptitude' > $startpoint/tmp/installer.sh | |
chroot $startpoint bash /tmp/installer.sh | |
chroot $startpoint dpkg-reconfigure tzdata | |
packages="fakeroot fakechroot cgmanager pm-utils debhelper libpam-ssh keychain monkeysphere rssh python-tk binutils libparted0-dev libparted0-i18n traceroute isoquery geoip-bin apparmor-profiles apparmor-utils cpp wamerican whois vacation dbus-x11 rblcheck groff apmd ssh-askpass openssh-server nano ubuntu-standard zip unzip rar unrar xz-utils iftop iotop htop wget util-linux tar sshfs screen rfkill resolvconf python-pip p7zip parted ngrep laptop-detect git btrfs-tools linux-firmware wpasupplicant iw wireless-tools htop curl build-essential" | |
echo "Do you wish to install desktop ubuntu? (y/n)" | |
reply="" && read reply | |
if [ $reply == "y" ]; then | |
packages="$packages ubuntu-desktop" | |
fi | |
echo "Installing required packages, this will take a while" | |
chroot $startpoint aptitude install $packages | |
echo "Please enter your username" | |
echo "Leave this blank if you wish to copy usernames from the old system" | |
reply="" && read reply | |
if [ -z "$reply" ]; then | |
echo "Adding user $user" | |
chroot $startpoint adduser $reply sudo | |
else | |
echo "WARNING: You've chosen to skip adding a user" | |
fi | |
echo "Do you wish to install grub2? (bootmanager) (y/n)" | |
reply="" && read reply | |
if [ $reply == "y" ]; then | |
echo "Installing Grub" | |
chroot $startpoint aptitude install grub-pc linux-generic | |
grub-install $disk | |
update-grub | |
echo "Make sure to check the grub.cfg for errors!" | |
else | |
echo "Skipping Grub" | |
chroot $startpoint aptitude install linux-generic | |
fi | |
echo "Do you want the wireless-wpa.sh script? (y/n)" | |
echo " - It sets up and connects you to a wireless network from cli" | |
reply="" && read reply | |
if [ $reply == "y" ]; then | |
wget 'https://gist.githubusercontent.com/KsaRedFx/2d92b7956dd5c23fc6e2/raw/wireless-wpa.sh' -o $startpoint/wireless-wpa.sh | |
else | |
echo "WARNING: You've chosen to skip the wireless script" | |
fi | |
echo "Do you wish to copy users and passwords over from the old machine? (y/n)" | |
reply="" && read reply | |
if [ $reply == "y" ]; then | |
echo "Copying over users, groups, and passwords" | |
UGIDLIMIT=1000 | |
workdir=$startpoint/tmp/passwd | |
mkdir $workdir | |
users=`awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd | grep /home/ | cut -f 1 -d ':'` | |
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > $workdir/passwd.mig | |
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > $workdir/group.mig | |
awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - | egrep -f - /etc/shadow > $workdir/shadow.mig | |
cp /etc/gshadow $workdir/gshadow.mig | |
mkdir $startpoint/etc/passwd.bak | |
chroot $startpoint cp /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/passwd.bak/ | |
cd $workdir | |
cat passwd.mig >> $startpoint/etc/passwd | |
cat shadow.mig >> $startpoint/etc/shadow | |
cat group.mig >> $startpoint/etc/group | |
cp gshadow.mig $startpoint/etc/gshadow | |
echo "Do you wish to copy homedir skeletons too? (y/n)" | |
reply="" && read reply | |
if [ $reply = "y" ]; then | |
echo "Copying homedir skeletons" | |
files=".ssh .irssi .bashrc .bash_history .render.py .litecoin .dogecoin .bitcoin .screenrc" | |
for y in $files ; do | |
rsync -rAXog /root/$y $startpoint/root/ | |
done | |
for x in $users ; do | |
mkdir $startpoint/home/$x | |
for y in $files ; do | |
rsync -rAXog /home/$x/$y $startpoint/home/$x/ | |
done | |
done | |
fi | |
fi | |
echo "Complete!" | |
echo "" | |
echo "Cleaning up" | |
rm $startpoint/installer.sh | |
echo "Unmounting required systems" | |
for x in dev/pts proc sys dev ; do | |
umount $startpoint/$x | |
done | |
cd ~ | |
echo "You may now unmount the device" | |
echo "" | |
echo "Installed $flavour to $device on disk $disk" | |
echo "Found filesystem $filesystem and subvolumes $subvol" | |
echo "Added the extra settings $extras" | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment