Compare commits

79 Commits
Author SHA1 Message Date
sean e655a8de82 tweaks 2019-07-12 16:15:38 -07:00
sean ecf1868cd1 testing 2019-07-12 16:12:16 -07:00
sean c483ef23f2 tweaks 2019-07-12 13:41:18 -07:00
sean 2fca3e6dd4 tweaks 2019-07-12 13:39:05 -07:00
sean 0f14bed2d3 tweaks 2019-07-12 13:31:06 -07:00
sean 9981f01ceb tweaks 2019-07-12 13:29:23 -07:00
Sean 1c607bd09a tweaks 2019-07-10 18:12:50 -07:00
Sean cadf53b85c tweaks 2019-07-10 16:43:47 -07:00
sean 7f1fbd3039 tweaks 2019-07-10 15:54:27 -07:00
sean 5c4982b1c7 tweaks 2019-07-10 15:29:58 -07:00
sean 197bbbe770 tweaks 2019-07-10 15:17:06 -07:00
sean 7b7853ecb8 tweaks 2019-07-10 15:04:14 -07:00
sean c3c45019bc tweaks 2019-07-10 14:57:16 -07:00
sean 9289844c99 tweaks 2019-07-10 14:50:54 -07:00
sean de7b8b3acd tweaks 2019-07-10 14:38:17 -07:00
sean 9ec289c136 tweaks 2019-07-10 14:25:44 -07:00
sean 6617d2f84d tweaks 2019-07-10 14:24:25 -07:00
sean 2ba9cb07da test tweaks 2019-07-10 14:02:59 -07:00
sean fac4a088ae tweaks 2019-07-10 12:31:44 -07:00
sean 0b33dd6be9 tweaks 2019-07-10 12:12:54 -07:00
sean a3c89343ac bad line endings? 2019-07-10 12:10:16 -07:00
sean def54b1a4f tweaks 2019-07-10 12:08:10 -07:00
sean b75109612f tweaks 2019-07-10 11:54:15 -07:00
sean 9e4fc3b35c tweaks 2019-07-10 01:33:29 -07:00
sean f212017cb4 tweaks 2019-07-10 01:22:10 -07:00
sean 71c1d92ed9 tweaks 2019-07-10 01:19:21 -07:00
sean 4d4d715fc1 tweaks 2019-07-09 21:13:20 -07:00
sean ebd0d89da9 tweaks 2019-07-09 20:42:13 -07:00
sean 394b8999df tweaks 2019-07-09 20:35:43 -07:00
sean 0fc329a922 tweaks 2019-07-09 20:30:26 -07:00
sean 791c10684f tweaks 2019-07-09 19:49:59 -07:00
sean a3de013ecb tweaks 2019-07-09 19:09:59 -07:00
sean 9ceac41631 tweaks 2019-07-09 18:54:12 -07:00
sean 63656fb07a tweaks 2019-07-09 18:45:18 -07:00
sean 19281f3ee6 tweaks 2019-07-09 18:40:44 -07:00
sean 2a91f9e118 tweaks 2019-07-09 18:24:54 -07:00
sean 1579bf11e8 tweaks 2019-07-09 18:17:26 -07:00
sean a0bc8b8ed1 tweaks 2019-07-09 18:02:57 -07:00
sean 8f6ae4707d tweaks 2019-07-09 17:59:06 -07:00
sean a896b8b888 tweaks 2019-07-09 17:47:42 -07:00
sean 7c12d73eec new tweaks 2019-07-09 17:38:33 -07:00
sean 19d8d7ce33 tweaks 2019-07-09 17:19:48 -07:00
sean c8f3c3d62b Tweaks 2019-07-09 17:05:43 -07:00
sean e26290c9e8 testing 2019-07-09 16:27:22 -07:00
sean 74a71e86c4 this was the problem 2019-07-09 16:08:07 -07:00
sean 474075ff87 test tweak 2019-07-09 15:08:13 -07:00
sean 74054d9634 tweaks 2019-07-09 14:57:20 -07:00
sean 45b20879eb tweaks 2019-07-09 14:46:10 -07:00
sean 74985dfc81 tweaks 2019-07-09 14:36:36 -07:00
sean dcd2fc35eb tweaks 2019-07-09 14:26:33 -07:00
sean 422e5f71fe testing another tweak 2019-07-09 14:19:54 -07:00
sean d95da849f8 tweaks 2019-07-09 14:15:33 -07:00
sean bf368d0d25 tweaks but not really 2019-07-09 14:11:20 -07:00
sean 5e45529a0d tweaks 2019-07-09 14:08:44 -07:00
sean c58ae06775 tweaks 2019-07-09 14:05:15 -07:00
sean 386b7a00e4 tweaks 2019-07-09 14:02:04 -07:00
sean 563a7328f4 tweaks 2019-07-09 13:58:40 -07:00
sean c0ac3d7a12 irritating tweaks 2019-07-09 13:54:52 -07:00
sean 17ebeaa3f7 tweaks 2019-07-09 13:51:52 -07:00
sean 6863e39c9c test 2019-07-09 13:51:05 -07:00
sean a55e02e1e0 tweak 2019-07-09 13:49:46 -07:00
sean 27ea4b4411 tweaks 2019-07-09 13:44:40 -07:00
sean 059249eed2 tweaks and tests 2019-07-09 13:42:04 -07:00
sean c0b5434f44 tweaks 2019-07-09 13:37:47 -07:00
sean edf841a06e tweaks 2019-07-09 13:33:36 -07:00
sean dc0f7796b2 tweaks 2019-07-09 13:31:34 -07:00
sean 9438c07e25 tweaks test 2019-07-09 13:29:16 -07:00
sean f08d10d009 test tweak 2019-07-09 13:27:46 -07:00
sean f369fb70a7 tweaks 2019-07-09 13:24:26 -07:00
sean a02cf785e5 tweaks 2019-07-09 13:21:23 -07:00
sean 5e83fbf3d2 tweaks 2019-07-09 13:14:26 -07:00
sean 8ca216cae2 tweaks 2019-07-09 12:57:42 -07:00
sean 43bea157fe tweaks 2019-07-09 12:50:21 -07:00
sean 96fa360d27 tweak 2019-07-09 12:47:45 -07:00
sean 9c3bc33373 tweaks 2019-07-09 12:43:07 -07:00
sean 0d60a61c39 tweaks 2019-07-09 12:40:55 -07:00
sean 5857371570 Fixed line endings, utf8 encoding 2019-07-09 12:32:40 -07:00
sean bc0d29539d Untested - unfinished, just saving state 2019-07-08 19:24:45 -07:00
sean d4d0debe30 mods 2019-07-08 17:48:59 -07:00
5 changed files with 328 additions and 429 deletions
+1 -4
View File
@@ -1,8 +1,5 @@
Server Setup Script Server Setup Script
===================== =====================
#Setup# #Use#
wget -qO - https://code.totosearch.org/Sean/ServerSetup/raw/branch/master/setup.sh | sudo bash wget -qO - https://code.totosearch.org/Sean/ServerSetup/raw/branch/master/setup.sh | sudo bash
#Maintenance#
wget -qO - https://code.totosearch.org/Sean/ServerSetup/raw/branch/master/maintenance.sh | sudo bash
-67
View File
@@ -1,67 +0,0 @@
#! /bin/bash
echo ""
echo "Updating local packages..."
errors=`apt-add-repository universe >/dev/null 2>/dev/null && apt-get update 2>&1 >/dev/null && apt-get install -y zfs-initramfs`
if [ "$?" = "0" ]; then
echo "Done."
else
echo "Failed to add universal repository, update apt repository - $errors"
exit 1
fi
echo ""
echo "Importing boot pool and main OS pool..."
errors=$(zpool export -a 2>&1 1>/dev/null && zpool import -N -R /mnt rpool 2>&1 1>/dev/null && zpool import -N -R /mnt bpool 2>&1 1>/dev/null && zfs mount rpool/ROOT/ubuntu 2>&1 1>/dev/null)
# && zfs mount -a 2>&1 1>/dev/null
if [ "$?" = "0" ]; then
echo "Done."
else
echo "Failed to mount bpool or rpool - $errors"
exit 2
fi
echo ""
echo "Binding mounts..."
errors=`mount --rbind /dev /mnt/dev 2>&1 1>/dev/null && mount --rbind /proc /mnt/proc 2>&1 1>/dev/null && mount --rbind /sys /mnt/sys 2>&1 1>/dev/null`
if ! [ "$?" = "0" ]; then
echo "Failed to bind mounts - $errors"
exit 4
fi
echo "Done."
echo ""
echo "Entering chroot, type \"exit\" to leave..."
echo ""
echo "#!/bin/bash" > /mnt/maintenance.sh
chmod +x /mnt/maintenance.sh
echo "zfs mount -a && mount /boot" >> /mnt/maintenance.sh
echo "/bin/bash" >> /mnt/maintenance.sh
echo "umount /boot" >> /mnt/maintenance.sh
echo "zfs umount -a" >> /mnt/maintenance.sh
chroot /mnt /maintenance.sh --login < /dev/tty
rm /mnt/maintenance.sh
if ! [ "$?" = "0" ]; then
echo "Chroot failed with an error"
exit $?
fi
echo ""
echo "Unbinding mounts..."
errors=`mount | grep -v zfs | tac | awk '/\\/mnt/ {print \$3}' | xargs -i{} umount -lf \{\} 2>&1 1>/dev/null && zpool export -a 2>&1 1>/dev/null`
if ! [ "$?" = "0" ]; then
echo "Failed to unbind mounts - $errors"
exit 4
fi
echo "Done."
echo ""
exit 0
+86
View File
@@ -0,0 +1,86 @@
#! /bin/bash
echo ""
echo "Mounting /proc/self/mounts..."
ln -s /proc/self/mounts /etc/mtab
echo "Done."
echo ""
echo "Updating apt repositories inside chroot..."
errors=`apt update 2>&1 1>/dev/null`
if ! [ "$?" = "0" ]; then
echo "Failed to update apt repositories inside chroot - $errors"
exit 1
fi
echo "Done."
echo ""
echo "Setting locale..."
echo -e 'LANG="en_US.UTF-8"\nLANGUAGE="en_US:en"\n' > /etc/default/locale
echo "Done."
#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 0
+102 -358
View File
@@ -2,7 +2,7 @@
echo "" echo ""
echo "Updating local packages..." echo "Updating local packages..."
errors=`apt-add-repository universe >/dev/null 2>&1 1>/dev/null && apt update 2>&1 >/dev/null` errors=`apt-add-repository universe >/dev/null 2>/dev/null && apt update 2>&1 >/dev/null`
if [ "$?" = "0" ]; then if [ "$?" = "0" ]; then
echo "Done." echo "Done."
else else
@@ -12,11 +12,11 @@ fi
echo "" echo ""
echo "Installing required setup configuration utilities..." echo "Installing required setup configuration utilities..."
errors=$(apt install -y dialog net-tools gdisk zfs-initramfs debootstrap >/dev/null 2>/dev/null) apt install -y dialog net-tools gdisk zfs-initramfs debootstrap >/dev/null 2>/dev/null
if [ "$?" = "0" ]; then if [ "$?" = "0" ]; then
echo "Done." echo "Done."
else else
echo "Failed to install net-tools, gdisk, zfs-initramfs, or debootstrap - $errors" echo "Failed to install net-tools, gdisk, zfs-initramfs, or debootstrap."
exit 2 exit 2
fi fi
@@ -25,16 +25,9 @@ width=`tput cols`
height=`tput lines` height=`tput lines`
window=$((height - 5)) window=$((height - 5))
if [ -d "/sys/firmware/efi" ]; then efi="1"; echo -e "\nUEFI detected..." else efi="0"; echo -e "\nBIOS detected..."; fi
# gather input at the start # gather input at the start
devices=""; for device in $(ls /dev/disk/by-id | grep -v part); do devices="$devices $device off"; done 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` disks=`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)
echo "alldisks: $alldisks"
diskcount=`echo "$disks" | wc -l` diskcount=`echo "$disks" | wc -l`
diskoptions="12 35 5 raidz off raidz2 off raidz3 off mirror off none off" diskoptions="12 35 5 raidz off raidz2 off raidz3 off mirror off none off"
if [ "$diskcount" = "0" ] ; then echo "No disks found to use for boot device"; exit 112; fi if [ "$diskcount" = "0" ] ; then echo "No disks found to use for boot device"; exit 112; fi
@@ -48,146 +41,41 @@ if [ "$diskcount" -gt "6" ]; then diskoptions="11 35 4 raidz2 off raidz3 off mir
if [ "$diskcount" -gt "11" ]; then diskoptions="10 35 3 raidz3 off mirror off none off"; fi if [ "$diskcount" -gt "11" ]; then diskoptions="10 35 3 raidz3 off mirror off none off"; fi
raidtype=`dialog --no-items --no-cancel --title "Root pool ZFS RAID" --radiolist "Select root pool ZFS RAID type:" $diskoptions 2>&1 1>/dev/tty` raidtype=`dialog --no-items --no-cancel --title "Root pool ZFS RAID" --radiolist "Select root pool ZFS RAID type:" $diskoptions 2>&1 1>/dev/tty`
hostname=""; while [ -z "$hostname" ]; do hostname=`dialog --no-cancel --inputbox "Hostname:" 8 40 2>&1 >/dev/tty`; done hostname=""; while [ -z "$hostname" ]; do hostname=`dialog --no-cancel --inputbox "Hostname:" 8 40 2>&1 >/dev/tty`; done
domainname=""; while [ -z "$domainname" ]; do domainname=`dialog --no-cancel --inputbox "Domain name root (for fully qualified domain, e.g. company.com):" 8 40 2>&1 >/dev/tty`; done
fqdn="${hostname}.${domainname}"
nicdevices=""; for nic in $(ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo'); do nicdevices="$nicdevices $nic off"; done nicdevices=""; for nic in $(ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo'); do nicdevices="$nicdevices $nic off"; done
totalram=`cat /proc/meminfo | grep -e "^MemTotal" | sed 's/MemTotal: *\(.*\) kB *$/\1/g'` nics=`dialog --separate-output --no-cancel --no-items --title "Bridged network devices" --checklist "Select the network devices to be bridged to br0:" $height $width ${window}$nicdevices 2>&1 1>/dev/tty`
swapspace=$(($totalram / 5))
swapspace=$(($swapspace / `getconf PAGESIZE`))
swapspace=$(($swapspace * `getconf PAGESIZE`))
# 20% swap space with 2G minimum
if [ "$swapspace" -lt "2048000" ]; then swapspace="2048000" ; fi
nics=`dialog --separate-output --no-cancel --no-items --title "Bridged network devices" --checklist "Select the network devices to be bridged to bridge - select none for no bridge:" $height $width ${window}$nicdevices 2>&1 1>/dev/tty`
if [ -z "$nics" ]; then
bridgedevice=`dialog --no-cancel --no-items --title "Configure network devices" --radiolist "NO BRIDGE MODE!\nSelect the network device to configure:" $height $width ${window}$nicdevices 2>&1 1>/dev/tty`
else
bridgedevice="br0"
fi
networktype=`dialog --no-items --no-cancel --title "Network type" --radiolist "Select the network type:" 9 40 2 dhcp off static off 2>&1 1>/dev/tty`
if [ "$networktype" = "static" ]; then
address=""; while [ -z "$address" ]; do address=`dialog --no-cancel --inputbox "IP Address:" 8 40 2>&1 >/dev/tty`; done
subnet=""; while [ -z "$subnet" ]; do subnet=`dialog --no-cancel --inputbox "Subnet mask:" 8 40 2>&1 >/dev/tty`; done
gateway=""; while [ -z "$gateway" ]; do gateway=`dialog --no-cancel --inputbox "Gateway:" 8 40 2>&1 >/dev/tty`; done
dns1=""; while [ -z "$dns1" ]; do dns1=`dialog --no-cancel --inputbox "Primary DNS server:" 8 40 2>&1 >/dev/tty`; done
dns2=`dialog --no-cancel --inputbox "Secondary DNS server:" 8 40 2>&1 >/dev/tty`
fi
admin=""; while [ -z "$admin" ]; do admin=`dialog --no-cancel --inputbox "Admin user:" 8 40 2>&1 >/dev/tty`; done admin=""; while [ -z "$admin" ]; do admin=`dialog --no-cancel --inputbox "Admin user:" 8 40 2>&1 >/dev/tty`; done
rootpassword="" rootpassword=`dialog --no-cancel --title "Root password" --insecure --passwordbox "Enter root password:" 8 40 2>&1 1>/dev/tty`
while [ "$rootpassword" = "" ]; do confirmpassword=`dialog --no-cancel --title "Root password confirmation" --insecure --passwordbox "Re-enter root password:" 8 40 2>&1 1>/dev/tty`
rootpassword=`dialog --no-cancel --title "Root password" --insecure --passwordbox "Enter root password:" 8 40 2>&1 1>/dev/tty` admins=`dialog --no-items --no-cancel --title "Root pool ZFS RAID" --inputbox "List administrator e-mail addresses separated by comma:" 10 40 2>&1 1>/dev/tty`
confirmpassword=`dialog --no-cancel --title "Root password confirmation" --insecure --passwordbox "Re-enter root password:" 8 40 2>&1 1>/dev/tty` if ! [ "$rootpassword" = "$confirmpassword" ]; then
if ! [ "$rootpassword" = "$confirmpassword" ]; then echo "Password does not match confirmation - please retry the setup"
echo "Password does not match confirmation - please retry the setup" exit 10
rootpassword=""
fi
done
admins=`dialog --no-items --no-cancel --title "Administrators" --inputbox "List administrator e-mail addresses separated by comma:" 10 40 2>&1 1>/dev/tty`
a=""; for x in `timedatectl list-timezones`; do a="$a $x" ; done
smtp=""; while [ -z "$smtp" ]; do smtp=`dialog --no-cancel --inputbox "Outoing e-mail SMTP proxy server:" 8 40 2>&1 >/dev/tty`; done
email=""; while [ -z "$email" ]; do email=`dialog --no-cancel --inputbox "Outgoing mail username:" 8 40 2>&1 >/dev/tty`; done
emailpassword=""
while [ "$emailpassword" = "" ]; do
emailpassword=`dialog --no-cancel --title "Outgoing e-mail SMTP mail password" --insecure --passwordbox "Enter outgoing e-mail SMTP password:" 8 40 2>&1 1>/dev/tty`
confirmpassword=`dialog --no-cancel --title "Outgoing e-mail SMTP mail password" --insecure --passwordbox "Re-enter outgoing e-mail SMTP password:" 8 40 2>&1 1>/dev/tty`
if ! [ "$emailpassword" = "$confirmpassword" ]; then
echo "Password does not match confirmation - please retry the setup"
emailpassword=""
fi
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
if [ "$efi" = "1" ]; then
for disk in `echo "$boots"`; do
echo ""
echo "Creating boot partitions for device $disk..."
errors=`sgdisk -n1:1M:+512M -t1:EF00 /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 "$boots"`; do
echo ""
echo "Creating boot partitions for device $disk..."
errors=`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
else
for disk in `echo "$boots"`; do
echo ""
echo "Creating boot partitions for device $disk..."
errors=`sgdisk -a1 -n1:24K:+1000K -t1:EF02 /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
fi fi
timezone=`dialog --no-cancel --title "Timezone" --inputbox "Time zone:" 8 40 "America/Los_Angeles" 2>&1 1>/dev/tty`
for disk in `echo "$disks"`; do for disk in `echo "$disks"`; do
echo "" echo ""
echo "Creating main storage for device $disk..." echo "Partitioning disk $disk..."
errors=`sgdisk -n4:0:0 -t4:BF01 /dev/disk/by-id/$disk 2>&1 1>/dev/null` 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 if ! [ "$?" = "0" ]; then
echo "Failed to create main storage for device $disk - $errors" echo "Failed to partition disk $disk - $errors"
exit 3 exit 3
fi fi
echo "Done." echo "Done."
done done
# create boot mirror list and root list # 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
count="0"; bootmirror=""; for disk in `echo "$boots"`; do bootmirror="$bootmirror /dev/disk/by-id/${disk}-part3"; count=$((count + 1)); done; if [ "$count" -gt "1" ]; then bootmirror="mirror $bootmirror"; fi
rootraidz=""; for disk in `echo "$disks"`; do rootraidz="$rootraidz /dev/disk/by-id/${disk}-part4"; done
# refresh drives or there are missing partitions # refresh drives or there are missing partitions
partprobe 2>/dev/null 1>/dev/null partprobe 2>/dev/null 1>/dev/null
# wait for the partitions to show up # wait for the partitions to show up
for disk in `echo "$disks"`; do
for disk in `echo "$boots"`; do
pending="0" pending="0"
while ! [ -e "/dev/disk/by-id/${disk}-part3" ]; do while ! [ -e "/dev/disk/by-id/${disk}-part3" ]; do
partprobe 2>/dev/null 1>/dev/null partprobe 2>/dev/null 1>/dev/null
if [ "$pending" = "0" ]; then echo "" && echo "Waiting for ${disk} boot pool partition to update on device ${disk}..."; pending="1"; fi if [ "$pending" = "0" ]; then echo "" && echo "Waiting for ${disk} partition 3 to update..."; pending="1"; fi
sleep 3
done
if [ "$pending" = "1" ]; then echo "Done."; fi
done
for disk in `echo "$boots"`; do
pending="0"
while ! [ -e "/dev/disk/by-id/${disk}-part1" ]; do
partprobe 2>/dev/null 1>/dev/null
if [ "$pending" = "0" ]; then echo "" && echo "Waiting for ${disk} boot partition to update on device ${disk}..."; pending="1"; fi
sleep 3 sleep 3
done done
if [ "$pending" = "1" ]; then echo "Done."; fi if [ "$pending" = "1" ]; then echo "Done."; fi
@@ -195,15 +83,13 @@ done
echo "" echo ""
echo "Creating boot zpool..." echo "Creating boot zpool..."
errors=`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 $bootmirror 2>&1 1>/dev/null` errors=`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 if ! [ "$?" = "0" ]; then
echo "Failed to create boot pool - $errors" echo "Failed to create boot pool - $errors"
exit 4 exit 4
fi fi
echo "Done." echo "Done."
# no striped mirror support yet
# special exception for none raid - this just operates the disks like a stripe, for 1 or more disk - *not* recommended # special exception for none raid - this just operates the disks like a stripe, for 1 or more disk - *not* recommended
if [ "$raidtype" = "none" ]; then if [ "$raidtype" = "none" ]; then
raidtype="" raidtype=""
@@ -211,17 +97,6 @@ else
raidtype=" $raidtype" raidtype=" $raidtype"
fi fi
# wait for the partitions to show up
for disk in `echo "$disks"`; do
pending="0"
while ! [ -e "/dev/disk/by-id/${disk}-part4" ]; do
partprobe 2>/dev/null 1>/dev/null
if [ "$pending" = "0" ]; then echo "" && echo "Waiting for ${disk} storage partition to update..."; pending="1"; fi
sleep 3
done
if [ "$pending" = "1" ]; then echo "Done."; fi
done
echo "" echo ""
echo "Creating main zpool..." 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` 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`
@@ -260,12 +135,9 @@ fi
echo "Done." echo "Done."
echo "" echo ""
echo "Setting hostname and e-mail admins..." echo "Setting hostname..."
echo "$hostname" > /mnt/etc/hostname echo "$hostname" > /mnt/etc/hostname
echo "127.0.0.1 $hostname $fqdn" > /mnt/etc/hosts
echo "127.0.1.1 $hostname" >> /mnt/etc/hosts echo "127.0.1.1 $hostname" >> /mnt/etc/hosts
echo "${admins}" > /mnt/admins
chmod o+r /mnt/admins
echo "Done." echo "Done."
echo "" echo ""
@@ -278,6 +150,31 @@ echo "deb http://archive.ubuntu.com/ubuntu bionic-updates main universe" >> /mnt
echo "deb-src http://archive.ubuntu.com/ubuntu bionic-updates main universe" >> /mnt/etc/apt/sources.list echo "deb-src http://archive.ubuntu.com/ubuntu bionic-updates main universe" >> /mnt/etc/apt/sources.list
echo "Done." echo "Done."
# network configuration should have optional static configuration - not just DHCP, it's very important
echo ""
echo "Writing netplan file..."
macaddr=$(echo $hostname|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
echo "network:" > /mnt/etc/netplan/bridge.yaml
echo " version: 2" >> /mnt/etc/netplan/bridge.yaml
echo " renderer: networkd" >> /mnt/etc/netplan/bridge.yaml
echo " ethernets:" >> /mnt/etc/netplan/bridge.yaml
for nic in "$nics"; do
echo " ${nic}:" >> /mnt/etc/netplan/bridge.yaml
echo " dhcp4: no" >> /mnt/etc/netplan/bridge.yaml
done
echo " bridges:" >> /mnt/etc/netplan/bridge.yaml
echo " br0:" >> /mnt/etc/netplan/bridge.yaml
echo " macaddress: ${macaddr}" >> /mnt/etc/netplan/bridge.yaml
echo " dhcp4: yes" >> /mnt/etc/netplan/bridge.yaml
echo " parameters:" >> /mnt/etc/netplan/bridge.yaml
echo " stp: true" >> /mnt/etc/netplan/bridge.yaml
echo " interfaces:" >> /mnt/etc/netplan/bridge.yaml
for nic in `echo "$nics"`; do
echo " - ${nic}" >> /mnt/etc/netplan/bridge.yaml
done
echo "Done."
echo "" echo ""
echo "Setting up boot pool import service..." echo "Setting up boot pool import service..."
echo "[Unit]"> /mnt/etc/systemd/system/zfs-import-bpool.service echo "[Unit]"> /mnt/etc/systemd/system/zfs-import-bpool.service
@@ -293,6 +190,7 @@ echo "[Install]" >> /mnt/etc/systemd/system/zfs-import-bpool.service
echo " WantedBy=zfs-import.target " >> /mnt/etc/systemd/system/zfs-import-bpool.service echo " WantedBy=zfs-import.target " >> /mnt/etc/systemd/system/zfs-import-bpool.service
echo "Done." echo "Done."
echo "" echo ""
echo "Creating home directory..." echo "Creating home directory..."
errors=`zfs create rpool/home/${admin} 2>&1 1>/dev/null` errors=`zfs create rpool/home/${admin} 2>&1 1>/dev/null`
@@ -300,7 +198,6 @@ if ! [ "$?" = "0" ]; then
echo "Unable to create home directory - $errors" echo "Unable to create home directory - $errors"
exit 9 exit 9
fi fi
echo "Done."
echo "" echo ""
echo "Mounting chroot mounts..." echo "Mounting chroot mounts..."
@@ -312,7 +209,6 @@ echo "Done."
echo "" echo ""
echo "Entering chroot..." echo "Entering chroot..."
echo "#!/bin/bash" > /mnt/setup-chroot.sh echo "#!/bin/bash" > /mnt/setup-chroot.sh
echo "HOSTNAME=\"$hostname\"" >> /mnt/setup-chroot.sh
echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"\"" >> /mnt/setup-chroot.sh
echo "echo \"Mounting /proc/self/mounts...\"" >> /mnt/setup-chroot.sh echo "echo \"Mounting /proc/self/mounts...\"" >> /mnt/setup-chroot.sh
@@ -321,7 +217,7 @@ echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
echo "echo \"\"" >> /mnt/setup-chroot.sh echo "echo \"\"" >> /mnt/setup-chroot.sh
echo "echo \"Updating apt repositories on root pool...\"" >> /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 '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 ' echo "Failed to update apt repositories on root pool - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
@@ -329,7 +225,7 @@ echo "echo \"Done.\"">> /mnt/setup-chroot.sh
echo "echo \"\"">> /mnt/setup-chroot.sh echo "echo \"\"">> /mnt/setup-chroot.sh
echo "echo \"Setting locale...\"" >> /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 '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 ' echo "Failed to set the locale to en_US.UTF-8 - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
@@ -340,50 +236,43 @@ echo "cp /usr/share/zoneinfo/$timezone /etc/localtime" >> /mnt/setup-chroot.sh
echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
echo "echo \"\"" >> /mnt/setup-chroot.sh 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 "echo \"Installing linux image, bash, screen, tmux, zfs, vim, sharutils, docker, dosfstools, mailutils, openssh, ufw...\"" >> /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 "errors=\$(apt install -y --no-install-recommends linux-image-generic 2>&1 1>/dev/null && apt install -y vim bash screen tmux zfs-initramfs dosfstools openssh-server ufw mailutils 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 ' echo "Failed to install preliminary software - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
echo "echo \"Done.\"" >> /mnt/setup-chroot.sh echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
# network configuration should have optional static configuration - not just DHCP, it's very important
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 ' echo "Failed to enable networking service - $errors"' >> /mnt/setup-chroot.sh
echo " exit 118">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh
echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
# configure docker storage to use zfs
mkdir -p /mnt/etc/docker
echo "{\"storage-driver\":\"zfs\"}" > /mnt/etc/docker/daemon.json
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /mnt/etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /mnt/etc/sysctl.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /mnt/etc/sysctl.conf
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Creating EFI partition..."' >> /mnt/setup-chroot.sh echo 'echo "Creating EFI partition..."' >> /mnt/setup-chroot.sh
firstdisk=`echo "$boots" | head -n1` firstdisk=`echo "$disks" | head -n1`
echo "error=\$(mkdosfs -F 32 -s 1 -n EFI /dev/disk/by-id/${firstdisk}-part1 2>&1 1>/dev/null && mkdir /boot/efi 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh 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 ' echo "Failed to create dos file system for EFI partition - $errors"' >> /mnt/setup-chroot.sh
echo " exit 100">> /mnt/setup-chroot.sh echo " exit 100">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
echo "echo PARTUUID=$(blkid -s PARTUUID -o value /dev/disk/by-id/${firstdisk}-part1) /boot/efi vfat nofail,x-systemd.device-timeout=0 0 1 >> /etc/fstab" >> /mnt/setup-chroot.sh echo "echo PARTUUID=$(blkid -s PARTUUID -o value /dev/disk/by-id/${firstdisk}-part2) /boot/efi vfat nofail,x-systemd.device-timeout=0 0 1 >> /etc/fstab" >> /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 "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 ' echo "Failed to mount EFI partition or install grub-EFI - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
echo 'echo "Done."' >> /mnt/setup-chroot.sh echo 'echo "Done."' >> /mnt/setup-chroot.sh
if ! [ "$disks" = "" ]; then
echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Copying EFI partition to other boot disks..."' >> /mnt/setup-chroot.sh
i="2"
for disk in `echo "$disks" | 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
echo "echo ''" >> /mnt/setup-chroot.sh echo "echo ''" >> /mnt/setup-chroot.sh
echo 'echo "Setting root password..."' >> /mnt/setup-chroot.sh echo 'echo "Setting root password..."' >> /mnt/setup-chroot.sh
echo "errors=\$(echo 'root:${rootpassword}' | chpasswd 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh echo "errors=\$(echo 'root:${rootpassword}' | chpasswd 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh
@@ -393,12 +282,21 @@ echo 'echo "Done."'>> /mnt/setup-chroot.sh
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Enabling boot pool import service..."'>> /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 "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 ' echo "Failed to enable boot pool import service - $errors"' >> /mnt/setup-chroot.sh
echo " exit 102">> /mnt/setup-chroot.sh echo " exit 102">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
echo 'echo "Done."'>> /mnt/setup-chroot.sh echo 'echo "Done."'>> /mnt/setup-chroot.sh
#echo 'echo ""'>> /mnt/setup-chroot.sh
#echo 'echo "Enabling tmp file system mounting..."'>> /mnt/setup-chroot.sh
#echo "errors=\$(cp /usr/sharesystemd/tmp.mount /etc/systemd/system/ 2>&1 1>/dev/null && systemctl enable tmp.mount 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh
#echo "if ! [ \"\$?\" = \"0\" ]; then echo \"Failed to enable tmp file system mounting - \$errors\"; exit 103; fi" >> /mnt/setup-chroot.sh
#echo 'echo "Done."'>> /mnt/setup-chroot.sh
#addgroup --system lpadmin
#addgroup --system sambashare
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo "echo \"Checking ZFS root...\"" >> /mnt/setup-chroot.sh echo "echo \"Checking ZFS root...\"" >> /mnt/setup-chroot.sh
echo 'zfscheck=$(grub-probe /boot 2>&1 1>/dev/null)' >> /mnt/setup-chroot.sh echo 'zfscheck=$(grub-probe /boot 2>&1 1>/dev/null)' >> /mnt/setup-chroot.sh
@@ -407,9 +305,8 @@ echo "echo \"Success.\"" >> /mnt/setup-chroot.sh
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Updating initramfs..."' >> /mnt/setup-chroot.sh echo 'echo "Updating initramfs..."' >> /mnt/setup-chroot.sh
echo "echo \"vmd\" >> /etc/initramfs-tools/modules" >> /mnt/setup-chroot.sh
echo 'errors=$(update-initramfs -u -k all 2>&1 1>/dev/null)' >> /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 ' echo "Failed to update initramfs - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
@@ -418,14 +315,14 @@ echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
# you need sed to do this right! # you need sed to do this right!
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Modifying grub for ZFS root..."' >> /mnt/setup-chroot.sh echo 'echo "Modifying grub for ZFS root..."' >> /mnt/setup-chroot.sh
echo "errors=\$(sed -ir 's/quiet splash//g' /etc/default/grub 2>&1 1>/dev/null && sed -ir 's/GRUB_CMDLINE_LINUX=\".*\"/GRUB_CMDLINE_LINUX=\"root=ZFS=rpool\/ROOT\/ubuntu rootdelay=10 noresume\"/g' /etc/default/grub 2>&1 1>/dev/null && sed -ir 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/g' /etc/default/grub 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh echo "errors=\$(sed -ir 's/GRUB_CMDLINE_LINUX=\".*\"/GRUB_CMDLINE_LINUX=\"root=ZFS=rpool\/ROOT\/ubuntu\"/g' /etc/default/grub 2>&1 1>/dev/null)" >> /mnt/setup-chroot.sh
echo "if ! [ \"\$?\" = \"0\" ]; then echo \"Failed to set grub ZFS root - \$errors\"; exit 104; fi" >> /mnt/setup-chroot.sh echo "if ! [ \"\$?\" = \"0\" ]; then echo \"Failed to set grub ZFS root - \$errors\"; exit 104; fi" >> /mnt/setup-chroot.sh
echo 'echo "Done."' >> /mnt/setup-chroot.sh echo 'echo "Done."' >> /mnt/setup-chroot.sh
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Updating grub..."' >> /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 '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 ' echo "Failed to update grub - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
@@ -437,22 +334,6 @@ 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 "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 echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
if ! [ "$boots" = "" ]; then
if [ "$efi" = "1" ]; then
echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Cloning EFI boot partition to all boot devices..."' >> /mnt/setup-chroot.sh
i="2"
for disk in `echo "$boots" | tail -n+2`; do
echo "errors=\$(dd if=/dev/disk/by-id/${firstdisk}-part1 of=/dev/disk/by-id/${disk}-part1 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
fi
# error check this as one big block # error check this as one big block
echo "zfs set mountpoint=legacy bpool/BOOT/ubuntu" >> /mnt/setup-chroot.sh echo "zfs set mountpoint=legacy bpool/BOOT/ubuntu" >> /mnt/setup-chroot.sh
echo "echo \"bpool/BOOT/ubuntu /boot zfs nodev,relatime,x-systemd.requires=zfs-import-bpool.service 0 0\" >> /etc/fstab" >> /mnt/setup-chroot.sh echo "echo \"bpool/BOOT/ubuntu /boot zfs nodev,relatime,x-systemd.requires=zfs-import-bpool.service 0 0\" >> /etc/fstab" >> /mnt/setup-chroot.sh
@@ -467,13 +348,9 @@ echo "echo \"rpool/tmp /tmp zfs nodev,relatime 0 0\" >> /etc/fstab" >> /mnt/setu
echo 'echo ""' >> /mnt/setup-chroot.sh echo 'echo ""' >> /mnt/setup-chroot.sh
echo 'echo "Enabling SSH..."'>> /mnt/setup-chroot.sh echo 'echo "Enabling SSH..."'>> /mnt/setup-chroot.sh
echo "sed -ir 's/^ *#? *ChallengeResponseAuthentication.*/ChallengeResponseAuthentication no/g' /etc/ssh/sshd_config" >> /mnt/setup-chroot.sh
echo "sed -ir 's/^#PasswordAuthentication.*/PasswordAuthentication no/g' /etc/ssh/sshd_config" >> /mnt/setup-chroot.sh
echo "sed -ir 's/^UsePAM.*/UsePAM no/g' /etc/ssh/sshd_config" >> /mnt/setup-chroot.sh
echo "sed -ir 's/^#PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config" >> /mnt/setup-chroot.sh
echo "errors=\$(systemctl enable ssh 2>&1 1>/dev/null)">> /mnt/setup-chroot.sh 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 #&& 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 ' echo "Failed to enable SSH - $errors"' >> /mnt/setup-chroot.sh
echo " exit 101">> /mnt/setup-chroot.sh echo " exit 101">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh echo "fi">> /mnt/setup-chroot.sh
@@ -485,18 +362,26 @@ echo 'echo "Creating admin user..."'>> /mnt/setup-chroot.sh
echo "useradd $admin" >> /mnt/setup-chroot.sh echo "useradd $admin" >> /mnt/setup-chroot.sh
echo "cp -a /etc/skel/.[!.]* \"/home/$admin\"" >> /mnt/setup-chroot.sh echo "cp -a /etc/skel/.[!.]* \"/home/$admin\"" >> /mnt/setup-chroot.sh
echo "chown -R ${admin}:${admin} \"/home/$admin\"" >> /mnt/setup-chroot.sh echo "chown -R ${admin}:${admin} \"/home/$admin\"" >> /mnt/setup-chroot.sh
echo "usermod -a -G adm,cdrom,dip,plugdev,sudo -s /bin/bash \"$admin\"" >> /mnt/setup-chroot.sh echo "usermod -a -G adm,cdrom,dip,plugdev,sudo \"$admin\"" >> /mnt/setup-chroot.sh
echo "echo '${admin}:${rootpassword}' | chpasswd" >> /mnt/setup-chroot.sh echo "echo '${admin}:${rootpassword}' | chpasswd" >> /mnt/setup-chroot.sh
echo 'echo "Done."'>> /mnt/setup-chroot.sh echo 'echo "Done."'>> /mnt/setup-chroot.sh
#create swap space, too if necessary
#sendemail -t to@example.com -m "Here is the file." -a attachmentFile
echo "mkdir \"/home/${admin}/.ssh\"" >> /mnt/setup-chroot.sh echo "mkdir \"/home/${admin}/.ssh\"" >> /mnt/setup-chroot.sh
echo "ssh-keygen -b 4096 -t rsa -q -f \"/home/${admin}/.ssh/id_rsa\" -N '$rootpassword'" >> /mnt/setup-chroot.sh echo "ssh-keygen -b 4096 -t rsa -q -f \"/home/${admin}/.ssh/id_rsa\" -N '$rootpassword'" >> /mnt/setup-chroot.sh
echo "cat /home/${admin}/.ssh/id_rsa.pub > \"/home/${admin}/.ssh/authorized_keys\"" >> /mnt/setup-chroot.sh
#echo "chmod 600 \"/home/${admin}/.ssh/id_rsa\"" >> /mnt/setup-chroot.sh #echo "chmod 600 \"/home/${admin}/.ssh/id_rsa\"" >> /mnt/setup-chroot.sh
echo "chown -R ${admin}:${admin} \"/home/${admin}\"/.ssh" >> /mnt/setup-chroot.sh echo "chown -R ${admin}:${admin} \"/home/${admin}\"" >> /mnt/setup-chroot.sh
echo "HOME=\"/root\"" >> /mnt/setup-chroot.sh echo "echo \"admin: ${admins}\" >> /etc/mail/aliases" >> /mnt/setup-chroot.sh
echo "HOSTNAME=\"$hostname\"" >> /mnt/setup-chroot.sh echo "newaliases" >> /mnt/setup-chroot.sh
echo "systemctl enable sendmail" >> /mnt/setup-chroot.sh
echo "service sendmail start" >> /mnt/setup-chroot.sh
echo "cat \"/home/${admin}/.ssh/id_rsa\" | mail -s \"$hostname SSH key\" -A \"/home/${admin}/.ssh/id_rsa\" admin" >> /mnt/setup-chroot.sh
# uuencode test.csv test.csv | mail -v -s "Subject test" -r no-reply@mydomain.co.uk recepient@mydomain.co.uk
echo "exit 0" >> /mnt/setup-chroot.sh
errors=`chmod +x /mnt/setup-chroot.sh 2>&1 1>/dev/null` errors=`chmod +x /mnt/setup-chroot.sh 2>&1 1>/dev/null`
if ! [ "$?" = "0" ]; then if ! [ "$?" = "0" ]; then
@@ -504,159 +389,18 @@ if ! [ "$?" = "0" ]; then
exit 45 exit 45
fi 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 ' echo "Failed to enable UFW - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh
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 ' 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 ' echo "Failed to enable cron service - $errors"' >> /mnt/setup-chroot.sh
echo " exit 1">> /mnt/setup-chroot.sh
echo "fi">> /mnt/setup-chroot.sh
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 ' 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
echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
echo "echo \"\"" >> /mnt/setup-chroot.sh
echo "echo \"Sending SSH key via e-mail...\"" >> /mnt/setup-chroot.sh
if [ "$networktype" = "dhcp" ]; then
connection="`hostname -I` (`wget -qO - ifconfig.me`)"
else
connection="$address"
fi
echo "echo \"FromLineOverride=YES\" > /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"root=admin\" >> /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"hostname=${fqdn}\" >> /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"AuthUser=${email}\" >> /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"AuthPass=${emailpassword}\" >> /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"mailhub=${smtp}\" >> /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"UseSTARTTLS=YES\" >> /etc/ssmtp/ssmtp.conf" >> /mnt/setup-chroot.sh
echo "echo \"root:${admins}\" >> /etc/ssmtp/revaliases" >> /mnt/setup-chroot.sh
echo "chfn -f '${email}' root" >> /mnt/setup-chroot.sh
echo "serverinfo=\`cat \"/home/${admin}/.ssh/id_rsa\"\`" >> /mnt/setup-chroot.sh
echo "serverinfo=\"Server available at $connection\\n\\n\$serverinfo\"; echo -e \"\$serverinfo\" | mail -s \"$hostname SSH key\" -r \"${email}\" \"\`cat /admins\`\"" >> /mnt/setup-chroot.sh
echo "echo \"Done.\"" >> /mnt/setup-chroot.sh
echo "exit 0" >> /mnt/setup-chroot.sh
chroot /mnt /setup-chroot.sh chroot /mnt /setup-chroot.sh
if ! [ "$?" = "0" ]; then if ! [ "$?" = "0" ]; then
exit $? exit $?
fi fi
echo "" exit 0
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 && (while ! [ -e "/dev/zvol/rpool/swap" ]; do sleep 1; done) && 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 "Done."
echo ""
echo "Writing network interfaces file..."
macaddr=$(echo $hostname|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:\1:\2:\3:\4:\5/')
mkdir -p /mnt/etc/network && mkdir -p /mnt/etc/network/interfaces.d
echo "auto lo $bridgedevice" > /mnt/etc/network/interfaces.d/$bridgedevice
echo "iface lo inet loopback" >> /mnt/etc/network/interfaces.d/$bridgedevice
niclist=""
for nic in `echo "$nics"`; do
echo "iface ${nic} inet manual" >> /mnt/etc/network/interfaces.d/$bridgedevice
niclist="$niclist $nic"
done
if [ "$networktype" = "dhcp" ]; then
echo "iface $bridgedevice inet dhcp" >> /mnt/etc/network/interfaces.d/$bridgedevice
else
echo "iface $bridgedevice inet static" >> /mnt/etc/network/interfaces.d/$bridgedevice
echo " address $address" >> /mnt/etc/network/interfaces.d/$bridgedevice
echo " netmask $subnet" >> /mnt/etc/network/interfaces.d/$bridgedevice
echo " gateway $gateway" >> /mnt/etc/network/interfaces.d/$bridgedevice
sed -ir "s/^#DNS=.*/DNS=${dns1}/g" /mnt/etc/systemd/resolved.conf
if ! [ -z "$dns2" ]; then sed -ir "s/^#FallbackDNS=.*/FallbackDNS=${dns2}/g" /mnt/etc/systemd/resolved.conf ; fi
fi
#echo " bridge_hw $macaddr" >> /mnt/etc/network/interfaces.d/br0
echo " dns-nameservers 127.0.0.53" >> /mnt/etc/network/interfaces.d/$bridgedevice
if [ "$bridgedevice" = "br0" ]; then
echo " bridge_waitport 0" >> /mnt/etc/network/interfaces.d/$bridgedevice
echo " bridge_fd 0" >> /mnt/etc/network/interfaces.d/$bridgedevice
echo " bridge_ports${niclist}" >> /mnt/etc/network/interfaces.d/$bridgedevice
echo " bridge_stp on" >> /mnt/etc/network/interfaces.d/$bridgedevice
fi
echo "source-directory /etc/network/interfaces.d" > /mnt/etc/network/interfaces
echo "Done."
# add the firewall rule for SSH, but there may already be an exception for this, test the network in the live environment
echo ""
echo "Opening SSH port on firewall..."
sed -ir 's/### RULES ###/### RULES ###\n-A ufw-user-input -p tcp --dport 22 -j ACCEPT/g' /mnt/etc/ufw/user.rules 2>&1 1>/dev/null
echo "Done."
# commented out for debugging # commented out for debugging
echo "" echo ""
echo "Clearing chroot configuration script..." echo "Clearing chroot configuration script..."
rm /mnt/setup-chroot.sh rm /mnt/setup-chroot.sh
echo "Done." echo "Done."
echo "#! /bin/bash" > /mnt/zfshealth.sh
echo "problems=0; emailSubject=\"\`hostname\` - ZFS pool - HEALTH check\"; emailMessage=\"\"" >> /mnt/zfshealth.sh
echo "condition=\$(/sbin/zpool status | grep -e \"^ *state:\" | egrep -i '(DEGRADED|FAULTED|OFFLINE|UNAVAIL|REMOVED|FAIL|DESTROYED|corrupt|cannot|unrecover)')" >> /mnt/zfshealth.sh
echo "if [ \"\${condition}\" ]; then emailSubject=\"\$emailSubject - fault\"; problems=1; fi" >> /mnt/zfshealth.sh
echo "maxCapacity=80" >> /mnt/zfshealth.sh
echo "if [ \${problems} -eq 0 ]; then" >> /mnt/zfshealth.sh
echo " capacity=\$(/sbin/zpool list -H -o capacity)" >> /mnt/zfshealth.sh
echo " for line in \${capacity//%/}" >> /mnt/zfshealth.sh
echo " do" >> /mnt/zfshealth.sh
echo " if [ \$line -ge \$maxCapacity ]; then emailSubject=\"\$emailSubject - Capacity Exceeded\"; problems=1; fi" >> /mnt/zfshealth.sh
echo " done" >> /mnt/zfshealth.sh
echo "fi" >> /mnt/zfshealth.sh
echo "if [ \${problems} -eq 0 ]; then" >> /mnt/zfshealth.sh
echo " errors=\$(/sbin/zpool status | grep ONLINE | grep -v state | awk '{print \$3 \$4 \$5}' | grep -v 000)" >> /mnt/zfshealth.sh
echo " if [ \"\${errors}\" ]; then emailSubject=\"\$emailSubject - Drive Errors\"; problems=1; fi" >> /mnt/zfshealth.sh
echo "fi" >> /mnt/zfshealth.sh
echo "scrubExpire=691200" >> /mnt/zfshealth.sh
echo "if [ \${problems} -eq 0 ]; then" >> /mnt/zfshealth.sh
echo " currentDate=\$(date +%s)" >> /mnt/zfshealth.sh
echo " zfsVolumes=\$(/sbin/zpool list -H -o name)" >> /mnt/zfshealth.sh
echo " for volume in \${zfsVolumes}" >> /mnt/zfshealth.sh
echo " do" >> /mnt/zfshealth.sh
echo " if [ \$(/sbin/zpool status \$volume | egrep -c \"none requested\") -ge 1 ]; then echo \"ERROR: You need to run \\\"zpool scrub \$volume\\\" before this script can monitor the scrub expiration time.\"; break; fi" >> /mnt/zfshealth.sh
echo " if [ \$(/sbin/zpool status \$volume | egrep -c \"scrub in progress|resilver\") -ge 1 ]; then break; fi" >> /mnt/zfshealth.sh
echo " scrubRawDate=\$(/sbin/zpool status \$volume | grep scrub | awk '{print \$15 \$12 \$13}' | sed 's/\([0-9]\+\)\([A-Za-z]*\)\([0-9]\+\)/\\2 \\3, \\1/g')" >> /mnt/zfshealth.sh
echo " scrubDate=\$(date -d \"\$scrubRawDate\" +%s)" >> /mnt/zfshealth.sh
echo " if [ \$((\$currentDate - \$scrubDate)) -ge \$scrubExpire ]; then" >> /mnt/zfshealth.sh
echo " if [ \${problems} -eq 0 ]; then emailSubject=\"\$emailSubject - Scrub Time Expired. Scrub Needed on Volume(s)\"; fi" >> /mnt/zfshealth.sh
echo " problems=1" >> /mnt/zfshealth.sh
echo " emailMessage=\"\${emailMessage}Pool: \$volume needs scrub \n\"" >> /mnt/zfshealth.sh
echo " fi" >> /mnt/zfshealth.sh
echo " done" >> /mnt/zfshealth.sh
echo "fi" >> /mnt/zfshealth.sh
echo "echo -e \"\$emailMessage \n\n\n \`/sbin/zpool list\` \n\n\n \`/sbin/zpool status\`\" | mail -r \"${email}\" -s \"\$emailSubject\" \"\`cat /admins\`\"" >> /mnt/zfshealth.sh
echo "if [ \"\$problems\" -ne 0 ]; then logger \$emailSubject; fi" >> /mnt/zfshealth.sh
chmod +x /mnt/zfshealth.sh
echo "" echo ""
echo "Unmounting chroot mounts..." echo "Unmounting chroot mounts..."
mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | xargs -i{} umount -lf {} mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | xargs -i{} umount -lf {}
@@ -671,7 +415,7 @@ if ! [ "$?" = "0" ]; then
fi fi
echo "Done." 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." exit 0
echo ""
exit 0 #sed -i -r "s/(^|[^#y])(compress)/\1#\2/" "$file"
+139
View File
@@ -0,0 +1,139 @@
#! /bin/bash
#
# Calomel.org
# https://calomel.org/zfs_health_check_script.html
# FreeBSD 9.1 ZFS Health Check script
# zfs_health.sh @ Version 0.15
# Check health of ZFS volumes and drives. On any faults send email. In FreeBSD
# 10 there is supposed to be a ZFSd daemon to monitor the health of the ZFS
# pools. For now, in FreeBSD 9, we will make our own checks and run this script
# through cron a few times a day.
# Changelog
# Peter van der Does - Always send an email, even if there is no problem.
# I prefer to know a script has run even when there is no problem.
# June 24, 2015
# Peter van der Does - When a scrub is needed the email subject line only has to inform us once.
# 99 problems but ZFS ain't one
problems=0
emailSubject="`hostname` - ZFS pool - HEALTH check"
emailMessage=""
# Health - Check if all zfs volumes are in good condition. We are looking for
# any keyword signifying a degraded or broken array.
condition=$(/sbin/zpool status | egrep -i '(DEGRADED|FAULTED|OFFLINE|UNAVAIL|REMOVED|FAIL|DESTROYED|corrupt|cannot|unrecover)')
if [ "${condition}" ]; then
emailSubject="$emailSubject - fault"
problems=1
fi
# Capacity - Make sure pool capacities are below 80% for best performance. The
# percentage really depends on how large your volume is. If you have a 128GB
# SSD then 80% is reasonable. If you have a 60TB raid-z2 array then you can
# probably set the warning closer to 95%.
#
# ZFS uses a copy-on-write scheme. The file system writes new data to
# sequential free blocks first and when the uberblock has been updated the new
# inode pointers become valid. This method is true only when the pool has
# enough free sequential blocks. If the pool is at capacity and space limited,
# ZFS will be have to randomly write blocks. This means ZFS can not create an
# optimal set of sequential writes and write performance is severely impacted.
maxCapacity=80
if [ ${problems} -eq 0 ]; then
capacity=$(/sbin/zpool list -H -o capacity)
for line in ${capacity//%/}
do
if [ $line -ge $maxCapacity ]; then
emailSubject="$emailSubject - Capacity Exceeded"
problems=1
fi
done
fi
# Errors - Check the columns for READ, WRITE and CKSUM (checksum) drive errors
# on all volumes and all drives using "zpool status". If any non-zero errors
# are reported an email will be sent out. You should then look to replace the
# faulty drive and run "zpool scrub" on the affected volume after resilvering.
if [ ${problems} -eq 0 ]; then
errors=$(/sbin/zpool status | grep ONLINE | grep -v state | awk '{print $3 $4 $5}' | grep -v 000)
if [ "${errors}" ]; then
emailSubject="$emailSubject - Drive Errors"
problems=1
fi
fi
# Scrub Expired - Check if all volumes have been scrubbed in at least the last
# 8 days. The general guide is to scrub volumes on desktop quality drives once
# a week and volumes on enterprise class drives once a month. You can always
# use cron to schedule "zpool scrub" in off hours. We scrub our volumes every
# Sunday morning for example.
#
# Scrubbing traverses all the data in the pool once and verifies all blocks can
# be read. Scrubbing proceeds as fast as the devices allows, though the
# priority of any I/O remains below that of normal calls. This operation might
# negatively impact performance, but the file system will remain usable and
# responsive while scrubbing occurs. To initiate an explicit scrub, use the
# "zpool scrub" command.
#
# The scrubExpire variable is in seconds. So for 8 days we calculate 8 days
# times 24 hours times 3600 seconds to equal 691200 seconds.
scrubExpire=691200
if [ ${problems} -eq 0 ]; then
currentDate=$(date +%s)
zfsVolumes=$(/sbin/zpool list -H -o name)
for volume in ${zfsVolumes}
do
if [ $(/sbin/zpool status $volume | egrep -c "none requested") -ge 1 ]; then
echo "ERROR: You need to run \"zpool scrub $volume\" before this script can monitor the scrub expiration time."
break
fi
if [ $(/sbin/zpool status $volume | egrep -c "scrub in progress|resilver") -ge 1 ]; then
break
fi
### FreeBSD with *nix supported date format
scrubRawDate=$(/sbin/zpool status $volume | grep scrub | awk '{print $15 $12 $13}')
scrubDate=$(date -j -f '%Y%b%e-%H%M%S' $scrubRawDate'-000000' +%s)
### Ubuntu with GNU supported date format
#scrubRawDate=$(/sbin/zpool status $volume | grep scrub | awk '{print $11" "$12" " $13" " $14" "$15}')
#scrubDate=$(date -d "$scrubRawDate" +%s)
if [ $(($currentDate - $scrubDate)) -ge $scrubExpire ]; then
if [ ${problems} -eq 0 ]; then
emailSubject="$emailSubject - Scrub Time Expired. Scrub Needed on Volume(s)"
fi
problems=1
emailMessage="${emailMessage}Pool: $volume needs scrub \n"
fi
done
fi
# Notifications - On any problems send email with drive status information and
# capacities including a helpful subject line to root. Also use logger to write
# the email subject to the local logs. This is the place you may want to put
# any other notifications like:
#
# + Update an anonymous twitter account with your ZFS status (https://twitter.com/zfsmonitor)
# + Playing a sound file or beep the internal speaker
# + Update Nagios, Cacti, Zabbix, Munin or even BigBrother
echo -e "$emailMessage \n\n\n `/sbin/zpool list` \n\n\n `/sbin/zpool status`" | mail -s "$emailSubject" root
if [ "$problems" -ne 0 ]; then
logger $emailSubject
fi
### EOF ###