#! /bin/bash
#IFS=$'\n'
# fresh update on apt in Ubuntu
echo ""
echo "Updating local packages..."
apt-add-repository universe >/dev/null 2>/dev/null && apt update >/dev/null 2>/dev/null
if [ " $? " = "0" ] ; then
echo "Done."
echo ""
else
echo "Failed to add universal repository, update apt repository - possibly bad network connection."
exit 1
fi
echo ""
echo "Installing required setup configuration utilities..."
apt install -y dialog net-tools gdisk zfs-initramfs debootstrap >/dev/null 2>/dev/null
if [ " $? " = "0" ] ; then
echo "Done."
echo ""
else
echo "Failed to install net-tools, gdisk, zfs-initramfs, or debootstrap."
exit 2
fi
#calculate width and height of console
width = ` tput cols`
height = ` tput lines`
window = $(( height - 5 ))
# set line separator
devices = "" ; for device in $( ls /dev/disk/by-id | grep -v part) ; do devices = " $devices $device off " ; done
disks = ` dialog --separate-output --no-cancel --no-items --title "Root devices" --checklist "Select boot drives" $height $width ${ window } $devices 2>& 1 1>/dev/tty`
raidtype = ` dialog --no-items --no-cancel --title "Root pool ZFS RAID" --radiolist "Select root pool ZFS RAID type" 11 35 4 raidz off raidz2 off mirror off none off 2>& 1 1>/dev/tty`
hostname = "" ; while [ -z " $hostname " ] ; do hostname = ` dialog --no-cancel --inputbox "Host name/Server name" 8 100 2>& 1 >/dev/tty` ; done
nicdevices = "" ; for nic in $( ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo' ) ; do nicdevices = " $nicdevices $nic off " ; done
nics = ` dialog --separate-output --no-cancel --no-items --title "Network devices for bridge" --checklist "Select the network devices to be bridged" $height $width ${ window } $nicdevices 2>& 1 1>/dev/tty`
for disk in ` echo " $disks " ` ; do
# remove echo for testing
echo ""
echo " Partitioning disk $disk ... "
# collect the return values, check them for errors and display them
errors = ` sgdisk --zap-all /dev/disk/by-id/$disk 2>& 1 1>/dev/null && sgdisk -n2:1M:+512M -t2:EF00 /dev/disk/by-id/$disk 2>& 1 1>/dev/null && sgdisk -n3:0:+512M -t3:BF01 /dev/disk/by-id/$disk 2>& 1 1>/dev/null && sgdisk -n4:0:0 -t4:BF01 /dev/disk/by-id/$disk 2>& 1 1>/dev/null`
if ! [ " $? " = "0" ] ; then
echo " Failed to partition disk $disk - $errors "
exit 3
fi
echo "Done."
done
# create boot mirror list
bootmirror = "" ; rootraidz = "" ; for disk in ` echo " $disks " ` ; do bootmirror = " $bootmirror /dev/disk/by-id/ ${ disk } -part3 " ; rootraidz = " $rootraidz /dev/disk/by-id/ ${ disk } -part4 " ; done
echo ""
echo "Creating boot zpool..."
echo " bootmirror: $bootmirror "
zpool create -f -o ashift = 12 -d -o feature@async_destroy= enabled -o feature@bookmarks= enabled -o feature@embedded_data= enabled -o feature@empty_bpobj= enabled -o feature@enabled_txg= enabled -o feature@extensible_dataset= enabled -o feature@filesystem_limits= enabled -o feature@hole_birth= enabled -o feature@large_blocks= enabled -o feature@lz4_compress= enabled -o feature@spacemap_histogram= enabled -o feature@userobj_accounting= enabled -O acltype = posixacl -O canmount = off -O compression = lz4 -O devices = off -O normalization = formD -O relatime = on -O xattr = sa -O mountpoint = / -R /mnt bpool mirror$bootmirror
#2>&1 1>/dev/null
if ! [ " $? " = "0" ] ; then
echo "Failed to create boot pool"
exit 4
fi
echo "Done."
if [ " $raidtype " = "none" ] ; then
raidtype = ""
else
raidtype = " $raidtype "
fi
# test for boot pool creation
echo ""
echo "Creating main zpool..."
error = ` zpool create -f -o ashift = 12 -O acltype = posixacl -O canmount = off -O compression = lz4 -O dnodesize = auto -O normalization = formD -O relatime = on -O xattr = sa -O mountpoint = / -R /mnt rpool${ raidtype } ${ rootraidz } 2>& 1 1>/dev/null`
if ! [ " $? " = "0" ] ; then
echo " Failed to create main pool - $errors "
exit 5
fi
echo "Done."
zfs create -o canmount = off -o mountpoint = none rpool/ROOT
zfs create -o canmount = off -o mountpoint = none bpool/BOOT
zfs create -o canmount = noauto -o mountpoint = / rpool/ROOT/ubuntu
zfs mount rpool/ROOT/ubuntu
zfs create -o canmount = noauto -o mountpoint = /boot bpool/BOOT/ubuntu
zfs mount bpool/BOOT/ubuntu
zfs create rpool/home
zfs create -o mountpoint = /root rpool/home/root
zfs create -o canmount = off rpool/var
zfs create -o canmount = off rpool/var/lib
zfs create rpool/var/log
zfs create rpool/var/spool
zfs create -o com.sun:auto-snapshot= false rpool/var/cache
zfs create -o com.sun:auto-snapshot= false rpool/var/tmp
chmod 1777 /mnt/var/tmp
zfs create rpool/opt
zfs create rpool/srv
zfs create -o canmount = off rpool/usr
zfs create rpool/usr/local
zfs create rpool/var/mail
zfs create -o com.sun:auto-snapshot= false rpool/var/lib/docker
zfs create -o com.sun:auto-snapshot= false rpool/var/lib/nfs
zfs create -o com.sun:auto-snapshot= false rpool/tmp
chmod 1777 /mnt/tmp
exit 0
# debootstrap bionic /mnt
# zfs set devices=off rpool
# echo HOSTNAME > /mnt/etc/hostname
# echo "127.0.1.1 $hostname" >> /mnt/etc/hosts
# vi /mnt/etc/hosts
# echo "deb http://archive.ubuntu.com/ubuntu bionic main universe\ndeb-src http://archive.ubuntu.com/ubuntu bionic main universe\ndeb http://security.ubuntu.com/ubuntu bionic-security main universe\ndeb-src http://security.ubuntu.com/ubuntu bionic-security main universe\ndeb http://archive.ubuntu.com/ubuntu bionic-updates main universe\ndeb-src http://archive.ubuntu.com/ubuntu bionic-updates main universe" > /mnt/etc/apt/sources.list
# you really need static config here too!
macaddr = $( echo $hostname | md5sum| sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/' )
network = "network:\n version: 2\n renderer: networkd\n ethernets:"
for nic in " $nicdevices " ; do
network = " $network \n ${ nic } :\n dhcp4: no "
done
network = " $network \n bridges:\n br0:\n macaddress: ${ macaddr } \n dhcp4: yes\n parameters:\n stp: true\n interfaces: "
for nic in ` echo " $nicdevices " ` ; do
network = " $network \n - ${ nic } "
done
# echo "$network" > /mnt/etc/netplan/bridge.yaml
# mount --rbind /dev /mnt/dev
# mount --rbind /proc /mnt/proc
# mount --rbind /sys /mnt/sys
# chroot /mnt /bin/bash --login
# ln -s /proc/self/mounts /etc/mtab
# apt update
#echo -e 'LANG="en_US.UTF-8"\nLANGUAGE="en_US:en"\n' > /etc/default/locale
#dpkg-reconfigure tzdata
#apt install -y --no-install-recommends linux-image-generic
#apt install -y vim bash screen tmux zfs-initramfs dosfstools openssh-server
i = "0"
for disk in " $disks " ; do
echo " mkdosfs -F 32 -s 1 -n EFI /dev/disk/by-id/ ${ disk } -part2 "
echo " mkdir /boot/efi ${ i } "
echo " echo PARTUUID= $( blkid -s PARTUUID -o value /dev/disk/by-id/${ disk } -part2) /boot/efi ${ i } vfat nofail,x-systemd.device-timeout=0 0 1 >> /etc/fstab "
echo " mount /boot/efi ${ i } "
i = $(( i + 1 ))
done
#apt install -y grub-efi-amd64-signed shim-signed
#passwd
#echo "[Unit]\n DefaultDependencies=no\n Before=zfs-import-scan.service\n Before=zfs-import-cache.service\n \n [Service]\n Type=oneshot\n RemainAfterExit=yes\n ExecStart=/sbin/zpool import -N -o cachefile=none bpool\n [Install]\n WantedBy=zfs-import.target " > /etc/systemd/system/zfs-import-bpool.Service"
#systemctl enable zfs-import-bpool.Service
#cp /usr/sharesystemd/tmp.mount /etc/systemd/system/
#systemctl enable tmp.mount
#addgroup --system lpadmin
#addgroup --system sambashare
zfscheck = ` grub-probe /boot`
#update-initramfs -u -k all
# you need sed to do this right!
# GRUB_CMDLINE_LINUX="root=ZFS=rpool/ROOT/ubuntu" <--- replace this
#update-grub
i = "0"
for f in " $disks " ; do
echo " grub-install --target=x86_64-efi --efi-directory=/boot/efi ${ i } --botloader-id=ubuntu --recheck --no-floppy "
echo " umount /boot/efi ${ i } "
i = $(( i + 1 ))
done
#zfs set mountpoint=legacy bpool/BOOT/ubuntu
#echo bpool/BOOT/ubuntu /boot zfs nodev,relatime,x-systemd.requires=zfs-import-bpool.service 0 0 >> /etc/fstab
#zfs set mountpoint=legacy rpool/var/log
#echo rpool/var/log /var/log zfs nodev,relatime 0 0 >> /etc/fstab
#zfs set mountpoint=legacy rpool/var/spool
#echo rpool/var/spool /var/spool zfs nodev,relatime 0 0 >> /etc/fstab
#zfs set mountpoint=legacy rpool/var/tmp
#echo rpool/var/tmp /var/tmp zfs nodev,relatime 0 0 >> /etc/fstab
#zfs set mountpoint=legacy rpool/tmp
#echo rpool/tmp /tmp zfs nodev,relatime 0 0 >> /etc/fstab
#systemctl enable openssh-server
# create parker user automated way
#zfs create rpool/home/YOURUSERNAME
# adduser YOURUSERNAME
# cp -a /etc/skel/.[!.]* /home/YOURUSERNAME
# chown -R YOURUSERNAME:YOURUSERNAME /home/YOURUSERNAME
# usermod -a -G adm,cdrom,dip,lpadmin,plugdev,sambashare,sudo YOURUSERNAME
#create swap space, too if necessary
#exit
#mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | xargs -i{} umount -lf {}
#zpool export -a
#reboot