Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- #
- # CONSTANTS
- RED=$(tput setaf 1)
- GREEN=$(tput setaf 2)
- NORMAL=$(tput sgr0)
- MSG_STATUS_OKAY="[ OK ]"
- MSG_STATUS_FAIL="[ FAIL ]"
- COLUMNS=80
- display_status() {
- local command_exit_value="$1";
- local exit_message="$2"
- local string="$3";
- if [[ ${command_exit_value} = "0" ]]; then
- printf '%s%*s%s\n' "${GREEN}" "$(( COLUMNS - ${#line} ))" "${MSG_STATUS_OKAY}" "${NORMAL}";
- else
- printf '%s%*s%s\n' "${RED}" "$(( COLUMNS - ${#line} ))" "${MSG_STATUS_FAIL}" "${NORMAL}";
- printf "%s\n\n" "${exit_message}";
- exit 2;
- fi
- }
- main() {
- local args="$@"
- local arg_hostname="$1"
- local arg_disk1="$2"
- local arg_disk2="$3"
- local disk_ids
- # this is currently set to the hostname minus the FQDN
- # currently, we could use this value for parts in the script requiring
- # the hostname, but we want to keep them seperate in case we add support
- # for custom zpool names later
- local ZFS_pool_root=$(printf "${arg_hostname}" | awk -F"." '{print $1}')
- local ZFS_pool_os=${ZFS_pool_root}/ROOT/ubuntu
- local chroot_script=/var/tmp/setup.sh
- local aptserver="http://archive.ubuntu.com/ubuntu";
- local array_status;
- local line;
- local counter;
- printf "\n";
- for device in ${arg_disk1} ${arg_disk2}; do
- line="Getting disk ID for ${device}...";
- printf '%s' "${line}";
- for id in /dev/disk/by-id/*; do
- temp=$(printf "${id} "; readlink -f ${id});
- disk_temp=$(printf "${temp}\n" | grep "${device}\$" | awk '{print $1}');
- if [[ ! -z ${disk_temp} ]] &&
- [[ ! $(basename ${disk_temp}) =~ ^wwn.* ]]; then
- disk_id=$disk_temp;
- fi
- done
- disk_ids="${disk_ids} ${disk_id}";
- display_status "0" '' "${line}";
- done
- printf "\n";
- line="Installing required packages...";
- printf '%s' "${line}";
- apt update > /dev/null 2>&1
- exit_message="FATAL ERROR: debootstrap failed. Exiting.";
- apt install --yes debootstrap gdisk zfsutils-linux zfs-initramfs > /dev/null 2>&1;
- display_status "$?" "${exit_message}" "${line}";
- printf "\n";
- for disk in ${disk_ids}; do
- counter=0;
- line="Partitioning ${disk}..."
- printf '%s' "${line}";
- sgdisk --zap-all ${disk} > /dev/null 2>&1;
- array_status[$counter]="${?}:Failed to wipe disk ${disk}";
- counter=$((counter+1));
- sgdisk -a1 -n2:34:2047 -t2:EF02 ${disk} > /dev/null 2>&1;
- array_status[$counter]="${?}:Failed to write partition 2 on ${disk}";
- counter=$((counter+1));
- sgdisk -n9:-8M:0 -t9:BF07 ${disk} > /dev/null 2>&1;
- array_status[$counter]="${?}:Failed to write partition 9 on ${disk}";
- counter=$((counter+1));
- sgdisk -n1:0:0 -t1:BF01 ${disk} > /dev/null 2>&1;
- array_status[$counter]="${?}:Failed to write partition 1 on ${disk}";
- counter=$((counter+1));
- for i in "${array_status[@]}"; do
- if [[ $(echo ${i} | awk -F":" '{print $1}') = "0" ]]; then
- command_status="0";
- else
- command_status="1";
- exit_message=$(echo ${i} | awk -F":" '{print $2}');
- fi
- done
- display_status "${command_status}" "${exit_message}" "${line}";
- unset array_status;
- unset counter;
- done
- printf "\n";
- line="Waiting for udev to do it's thing...";
- printf "%s" "${line}";
- sleep 3;
- display_status "$?" "${exit_message}" "${line}";
- disk_install=$(printf "${disk_ids}\n" | awk '{print $1}');
- disk_mirror=$(printf "${disk_ids}\n" | awk '{print $2}');
- printf "\n";
- line="Creating zpool...";
- printf "%s" "${line}";
- exit_message="FATAL ERROR: Failed to create zpool. Exiting.";
- zpool create -o ashift=12 \
- -O atime=off -O canmount=off -O compression=lz4 -O normalization=formD \
- -O mountpoint=/ \
- -R /mnt \
- ${ZFS_pool_root} mirror ${disk_install}-part1 ${disk_mirror}-part1 -f;
- display_status "$?" "${exit_message}" "${line}";
- #printf "\n";
- #line="Creating "
- zfs create -o canmount=off -o mountpoint=none ${ZFS_pool_root}/ROOT;
- zfs create -o canmount=noauto -o mountpoint=/ ${ZFS_pool_os};
- zfs mount ${ZFS_pool_os};
- zfs create -o setuid=off ${ZFS_pool_root}/home;
- zfs create -o mountpoint=/root ${ZFS_pool_root}/home/root;
- zfs create -o canmount=off -o setuid=off -o exec=off ${ZFS_pool_root}/var;
- zfs create -o com.sun:auto-snapshot=false ${ZFS_pool_root}/var/cache;
- zfs create ${ZFS_pool_root}/var/log;
- zfs create ${ZFS_pool_root}/var/spool;
- zfs create -o com.sun:auto-snapshot=false -o exec=on ${ZFS_pool_root}/var/tmp;
- zfs create ${ZFS_pool_root}/opt;
- zfs create ${ZFS_pool_root}/srv;
- zfs create -o com.sun:auto-snapshot=false \
- -o mountpoint=/var/lib/nfs ${ZFS_pool_root}/var/nfs;
- zfs create -V 4G -b $(getconf PAGESIZE) -o compression=zle \
- -o logbias=throughput -o sync=always \
- -o primarycache=metadata -o secondarycache=none \
- -o com.sun:auto-snapshot=false ${ZFS_pool_root}/swap;
- chmod 1777 /mnt/var/tmp;
- line="Bootstrapping (this will take a while)...";
- printf "%s" "${line}";
- exit_message="FATAL ERROR: debootstrap failed. Exiting.";
- debootstrap xenial /mnt > /dev/null 2>&1
- display_status "$?" "${exit_message}" "${line}";
- zfs set devices=off ${ZFS_pool_root};
- printf "${arg_hostname}\n" > /mnt/etc/hostname;
- printf "127.0.1.1 $(printf ${arg_hostname} | awk -F"." '{print $1}') ${arg_hostname}" >> /mnt/etc/hosts;
- printf "Etc/UTC\n" > /mnt/etc/timezone;
- ln -fs /mnt/usr/share/zoneinfo/${TIMEZONE} /mnt/etc/localtime;
- INTERFACE=$(ip -o addr show scope global | awk '{gsub(/\/.*/,"",$2); print $2}');
- printf "auto $INTERFACE\niface $INTERFACE inet dhcp\n" > /mnt/etc/network/interfaces.d/$INTERFACE;
- cat /dev/null > /mnt/etc/apt/sources.list;
- distrib_codename=$(cat /etc/*release | grep );
- cat >>/mnt/etc/apt/sources.list<<EOF
- deb ${aptserver} xenial main universe
- deb-src ${aptserver} xenial main universe
- deb ${aptserver} xenial-security main universe
- deb-src ${aptserver} xenial-security main universe
- deb ${aptserver} xenial-updates main universe
- deb-src ${aptserver} xenial-updates main universe
- EOF
- mount --rbind /dev /mnt/dev;
- mount --rbind /proc /mnt/proc;
- mount --rbind /sys /mnt/sys;
- printf "%s\n" "#/etc/fstab" > /mnt/etc/fstab;
- cat >>/mnt${chroot_script}<<EOF
- #!/bin/bash
- locale-gen en_US.UTF-8
- printf 'LANG="en_US.UTF-8"\n' > /etc/default/locale
- ln -s /proc/self/mounts /etc/mtab
- dpkg-reconfigure --frontend noninteractive tzdata
- apt update
- DEBIAN_FRONTEND=noninteractive apt dist-upgrade --yes
- apt install --yes --no-install-recommends linux-image-generic
- apt install --yes ubuntu-minimal vim zfs-initramfs zfsutils-linux openssh-server curl
- DEBIAN_FRONTEND=noninteractive apt install --yes grub-pc
- grub-probe /
- update-initramfs -c -k all
- sed -e "s/\(GRUB_HIDDEN_TIMEOUT=0\)/#\1/" -e 's/\"quiet splash\"/\"\"/' -e 's/.*\(GRUB_TERMINAL=console\).*/\1/' -i "/etc/default/grub"
- update-grub
- grub-install ${disk_install}
- grub-install ${disk_mirror}
- printf "Pausing to exit script if there are any errors..."
- sleep 5
- #password="\$(sed -e s'/[A-Za-z]\{6,\}//g' /usr/share/dict/words | grep -v "'s" | sed '/^\s*$/d' | shuf -n 1)\$(shuf -i 0-9 -n 3 | tr '\n' ' ' | tr -d ' ')"
- password=\$(curl "http://www.passwordrandom.com/query?command=password&count=1&format=plain&scheme=CvvCVN")
- printf "root:\${password}" | chpasswd
- printf "\n\nThe root password has been set to ${GREEN}\${password}${NORMAL}\n\n"
- sed -ie 's/\(^PermitRootLogin\).*/\1 yes/' /etc/ssh/sshd_config
- systemctl enable ssh
- mkswap -f /dev/zvol/${ZFS_pool_root}/swap
- printf "%s\n" "/dev/zvol/${ZFS_pool_root}/swap none swap defaults 0 0" >> /etc/fstab
- zfs snapshot ${ZFS_pool_os}@install
- EOF
- chmod +x /mnt${chroot_script}
- chroot /mnt ${chroot_script}
- rm -f ${chroot_script}
- mount | grep -v zfs | tac | awk '/\/mnt/ {print $3}' | xargs -i{} umount -lf {}
- zpool export ${ZFS_pool_root}
- printf "%s\n" "Done. Ready to reboot\n\n"
- exit 0
- }
- main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement