diff --git a/setup.sh b/setup.sh index 2290c2b..977da70 100644 --- a/setup.sh +++ b/setup.sh @@ -28,9 +28,9 @@ window=$((height - 5)) # gather input at the start 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 root OS disks:" $height $width ${window}$devices 2>&1 1>/dev/tty` -boots=`dialog --separate-output --no-cancel --no-items --title "Root devices" --checklist "Select boot drives:" $height $width ${window}$devices 2>&1 1>/dev/tty` +boots=`dialog --separate-output --no-cancel --no-items --title "Boot devices" --checklist "Select boot devices:" $height $width ${window}$devices 2>&1 1>/dev/tty` -alldisks=`(for d in `echo "$disks"`; do echo "$d"; done; for d in `echo "$boots"`; do echo "$d"; done) | sort -u` +alldisks=$( (for d in `echo "$disks"`; do echo "$d"; done; for d in `echo "$boots"`; do echo "$d"; done) | sort -u) echo "alldisks: $alldisks" diskcount=`echo "$disks" | wc -l` @@ -95,12 +95,34 @@ done timezone=`dialog --no-items --no-cancel --menu "Select time zone:" $height 40 ${window}${a} 2>&1 1>/dev/tty` +for disk in `echo "$alldisks"`; do + echo "" + echo "Erasing partition table for device $disk..." + errors=`sgdisk --zap-all /dev/disk/by-id/$disk 2>&1 1>/dev/null` + if ! [ "$?" = "0" ]; then + echo "Failed to erase partition table for device $disk - $errors" + exit 3 + fi + echo "Done." +done + +for disk in `echo "$boots"`; do + echo "" + echo "Creating boot partitions for device $disk..." + errors=`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` + if ! [ "$?" = "0" ]; then + echo "Failed to create boot partition for device $disk - $errors" + exit 3 + fi + echo "Done." +done + for disk in `echo "$disks"`; do echo "" - echo "Partitioning disk $disk..." - 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` + echo "Creating main storage for device $disk..." + errors=`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" + echo "Failed to create main storage for device $disk - $errors" exit 3 fi echo "Done." @@ -113,7 +135,7 @@ bootmirror=""; rootraidz=""; for disk in `echo "$disks"`; do bootmirror="$bootmi partprobe 2>/dev/null 1>/dev/null # wait for the partitions to show up -for disk in `echo "$disks"`; do +for disk in `echo "$boots"`; do pending="0" while ! [ -e "/dev/disk/by-id/${disk}-part3" ]; do partprobe 2>/dev/null 1>/dev/null @@ -264,7 +286,7 @@ echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"Updating apt repositories on root pool...\"" >> /mnt/setup-chroot.sh echo 'errors=`apt update 2>&1 1>/dev/null`' >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to update apt repositories on root pool - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -272,7 +294,7 @@ echo "echo \"Done.\"">> /mnt/setup-chroot.sh echo "echo \"\"">> /mnt/setup-chroot.sh echo "echo \"Setting locale...\"" >> /mnt/setup-chroot.sh echo 'errors=`locale-gen en_US.UTF-8 2>&1 1>/dev/null`' >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to set the locale to en_US.UTF-8 - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -286,7 +308,7 @@ echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"Installing linux image, ifupdown dnsutils nfs-kernel-server apparmor-profiles vim, libvirt-bin, bridge-utils, net-tools, bash, screen, tmux, zfs-initramfs, dosfstools, mailutils, ssmtp, openssh-server, ufw, docker.io, sharutils...\"" >> /mnt/setup-chroot.sh echo "errors=\$(DEBCONF_FRONTEND='noninteractive' apt install -y --no-install-recommends linux-image-generic 2>&1 1>/dev/null && apt purge -y netplan 2>&1 1>/dev/null && apt autoremove -y 2>&1 1>/dev/null && DEBCONF_FRONTEND='noninteractive' apt install -y ifupdown efibootmgr htop iotop smartmontools dnsutils nfs-kernel-server apparmor-profiles vim libvirt-bin bridge-utils net-tools bash screen tmux zfs-initramfs dosfstools mailutils ssmtp openssh-server ufw docker.io sharutils 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to install preliminary software - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -297,7 +319,7 @@ echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"Enabling networking service...\"" >> /mnt/setup-chroot.sh echo "error=\$(systemctl unmask networking 2>&1 1>/dev/null && systemctl enable networking 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to enable networking service - $errors"' >> /mnt/setup-chroot.sh echo " exit 118">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -313,9 +335,9 @@ echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /mnt/etc/sysctl.conf echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo "Creating EFI partition..."' >> /mnt/setup-chroot.sh -firstdisk=`echo "$disks" | head -n1` +firstdisk=`echo "$boots" | head -n1` echo "error=\$(mkdosfs -F 32 -s 1 -n EFI /dev/disk/by-id/${firstdisk}-part2 2>&1 1>/dev/null && mkdir /boot/efi 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to create dos file system for EFI partition - $errors"' >> /mnt/setup-chroot.sh echo " exit 100">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -329,7 +351,7 @@ echo "echo PARTUUID=$(blkid -s PARTUUID -o value /dev/disk/by-id/${firstdisk}-pa #echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "errors=\$(mount /boot/efi 2>&1 1>/dev/null && apt install -y grub-efi-amd64-signed shim-signed 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to mount EFI partition or install grub-EFI - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -344,7 +366,7 @@ echo 'echo "Done."'>> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo "Enabling boot pool import service..."'>> /mnt/setup-chroot.sh echo "errors=\$(systemctl enable zfs-import-bpool.service 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to enable boot pool import service - $errors"' >> /mnt/setup-chroot.sh echo " exit 102">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -368,7 +390,7 @@ echo "echo \"Success.\"" >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo "Updating initramfs..."' >> /mnt/setup-chroot.sh echo 'errors=$(update-initramfs -u -k all 2>&1 1>/dev/null)' >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to update initramfs - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -385,7 +407,7 @@ echo 'echo "Done."' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo "Updating grub..."' >> /mnt/setup-chroot.sh echo 'errors=$(update-grub 2>&1 1>/dev/null)'>> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to update grub - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -397,15 +419,16 @@ echo "errors=\$(grub-install --target=x86_64-efi --efi-directory=/boot/efi --boo echo "if ! [ \"\$?\" = \"0\" ]; then echo \"Failed to install grub UEFI on plex1 - \$errors\"; exit 104; fi" >> /mnt/setup-chroot.sh echo "echo \"Done.\"" >> /mnt/setup-chroot.sh -if ! [ "$disks" = "" ]; then +if ! [ "$boots" = "" ]; then echo 'echo ""' >> /mnt/setup-chroot.sh - echo 'echo "Copying EFI partition to other boot disks..."' >> /mnt/setup-chroot.sh + echo 'echo "Cloning EFI boot partition to all boot devices..."' >> /mnt/setup-chroot.sh i="2" - for disk in `echo "$disks" | tail -n+2`; do + for disk in `echo "$boots" | tail -n+2`; do echo "errors=\$(dd if=/dev/disk/by-id/${firstdisk}-part2 of=/dev/disk/by-id/${disk}-part2 2>&1 1>/dev/null && efibootmgr -c -g -d /dev/disk/by-id/${disk} -p 3 -L "ubuntu-$i" -l '\EFI\ubuntu\grubx64.efi' 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh echo "if ! [ \"\$?\" = \"0\" ]; then echo \"EFI copy failed - \$errors\"; exit 20; fi" >> /mnt/setup-chroot.sh i=$((i + 1)) done + echo 'echo "Done."' >> /mnt/setup-chroot.sh fi @@ -433,7 +456,7 @@ echo "sed -ir 's/^#PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config" echo "errors=\$(systemctl enable ssh 2>&1 1>/dev/null)">> /mnt/setup-chroot.sh #&& ufw allow in on any from any to any port 22 proto tcp 2>&1 1>/dev/null -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to enable SSH - $errors"' >> /mnt/setup-chroot.sh echo " exit 101">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -498,7 +521,7 @@ fi echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"Enabling UFW and apparmor on boot...\"" >> /mnt/setup-chroot.sh echo "errors=\$(systemctl enable ufw 2>&1 1>/dev/null && systemctl enable apparmor 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to enable UFW - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -507,12 +530,12 @@ echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"Enabling cron, smartd, and adding ZFS health script...\"" >> /mnt/setup-chroot.sh echo "errors=\$(echo \"0 0 * * 1 /zfshealth.sh\" | crontab 1>&2 2>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to add zfshealth.sh to crontab - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh echo "errors=\$(systemctl enable cron 2>&1 1>/dev/null && systemctl enable smartd 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to enable cron service - $errors"' >> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -521,7 +544,7 @@ echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"Upgrading OS and installing standard command line system...\"" >> /mnt/setup-chroot.sh echo "errors=\$(apt dist-upgrade --yes 2>&1 1>/dev/null && apt install --yes ubuntu-standard 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh -echo 'if ! [ "$?" = "0" ]; then '>> /mnt/setup-chroot.sh +echo 'if ! [ \"$?\" = \"0\" ]; then '>> /mnt/setup-chroot.sh echo ' echo "Failed to upgrade OS or install command line basics - $errors"' >> /mnt/setup-chroot.sh echo " exit 122">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh @@ -548,10 +571,17 @@ if ! [ "$?" = "0" ]; then exit $? fi -zfs create -V ${swapspace}K -b $(getconf PAGESIZE) -o compression=zle -o logbias=throughput -o sync=always -o primarycache=metadata -o secondarycache=none -o com.sun:auto-snapshot=false rpool/swap -mkswap -f /dev/zvol/rpool/swap +echo "" +echo "Creating swap space..." +errors=$(zfs create -V ${swapspace}K -b $(getconf PAGESIZE) -o compression=zle -o logbias=throughput -o sync=always -o primarycache=metadata -o secondarycache=none -o com.sun:auto-snapshot=false rpool/swap 2>&1 1>/dev/null && mkswap -f /dev/zvol/rpool/swap 2>&1 1>/dev/null) +if ! [ "$?" = "0" ]; then + echo "Failed to create swap space - $errors" + exit 132 +fi echo "/dev/zvol/rpool/swap none swap discard 0 0" >> /mnt/etc/fstab -echo "RESUME=none" > /mnt/etc/initramfs.tools/conf.d/resume +echo "Done." + +ececho "RESUME=none" > /mnt/etc/initramfs.tools/conf.d/resume echo "" echo "Writing network interfaces file..." @@ -659,7 +689,7 @@ fi echo "Done." echo "" -echo "Congratulations! The install was successful. Please reboot and set your boot device using UEFI in the BIOS." +echo "Congratulations! The install was successful. Please reboot and set your boot device using UEFI in the BIOS. You should receive an e-mail with the server's SSH private key shortly." echo "" exit 0