Skip to content

Instantly share code, notes, and snippets.

@Xeckt
Last active May 25, 2021 16:35
Show Gist options
  • Save Xeckt/66241be9ebb18028979b6a61fd250db0 to your computer and use it in GitHub Desktop.
Save Xeckt/66241be9ebb18028979b6a61fd250db0 to your computer and use it in GitHub Desktop.
Ubuntu desktop raid installer
#!/bin/bash
# Device naming
dev_one=""
dev_two=""
raid_device=""
raid_uuid=$(uuidgen)
efi_sysname=""
install_name=""
# Volumes
physical_vol="/dev/md0p1"
vol_group="vg0"
vol_root="root"
vol_tmp="tmp"
vol_var="var"
vol_varlib="varlib"
vol_home="home"
# Partition sizing defaults
efi_size="512M"
root_size="25GB"
home_size="200GB"
var_size="5GB"
varlib_size="10GB"
tmp_size="10GB"
# Colours :D
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
off=$(tput sgr0)
main() {
clear
if [[ $EUID -ne 0 ]]; then
echo "Please run script as root or with sudo."
exit 1
fi
read -r -p "NOTE: THIS INSTALLER WILL ONLY CONFIGURE RAID 1 ON TWO DEVICES AND SET THEM UP FOR AN UBUNTU INSTALLATION, DO YOU WISH TO PROCEED? [y/n] >> " answer
case $answer in
[Yy]* ) echo "Continuing with storage configuration";;
[Nn]*) echo "No longer proceeding, exiting" ; exit 1;;
*) echo "Please answer y or n";;
esac
get_requisites
read -r -p "${white}Are the above details correct? [y/n] >> " answer
case $answer in
[Yy]* )
echo "${green}Zeroing drives before beginning to create RAID"
zero_drives $dev_one $dev_two
if [ $? -eq 0 ]; then
echo "${white}Previous command fulfilled, partioning drives ready for LVM..."
prompt_partition_sizes
partition_drives
echo "${white}Installing MDADM to create RAID 1"
apt update && apt install mdadm
if [ $? -eq 0 ]; then
echo "${white}Installation of MDADM package successful. Continuing."
mdadm_setup
echo "${white}Setting up final stage: LVM"
lvm_setup
if [ $? -eq 0 ]; then
post_install_steps
edit_efi_partition
fi
else
echo "${red}[ERROR] Couldn't install mdadm. Exiting.${off}"
exit 1
fi
else
echo "${red}[ERROR] Something went wrong zeroing drives. Exiting.${off}"
exit 1
fi
;;
[Nn]*) echo "Exiting" ; exit 1;;
*) echo "Please answer y or n";;
esac
}
get_requisites() {
echo -n "Specify the devices that will be used to make the RAID 1 seperated by spaces ${yellow}[e.g. /dev/sda /dev/sdb] >> ${off}" ; read dev_one dev_two
echo -n "Specify the name of the RAID as it will appear in /dev/ ${yellow}[e.g. /dev/md100] >> ${off}" ; read raid_name
echo -n "Specify the name you want your EFI boot flag to be called ${yellow}[string] >> ${off}" ; read efi_sysname
echo -n "Specify the name of your OS installation ${yellow}[string] >> ${off}" ; read install_name
echo
echo "${yellow}==== Device Naming ===="
echo "${yellow}Devices: ${cyan}$dev_one $dev_two"
echo "${yellow}RAID Device Name: ${cyan}$raid_name"
echo "${yellow}EFI Name: ${cyan}$efi_sysname"
echo "${yellow}OS Install Name: ${cyan}$install_name"
echo
}
partition_drives() {
echo "${green}Creating EFI partition on $dev_one with name $efi_sysname$ size ${yellow}$efi_size${off}"
sgdisk -n 1:0:+$efi_size -t 1:ef00 -c 1:$efi_sysname $dev_one
echo "${green}Creating RAID device for $dev_one with name $install_name"
sgdisk -n -n 2:0:0 -t 2:fd00 -c 2:$install_name $dev_one
echo "${green}Creating EFI partition on $dev_two with name $efi_sysname$ size ${yellow}$efi_size${off}"
sgdisk -n 1:0:+$efi_size -t 1:ef00 -c 1:$efi_sysname $dev_two
echo "${green}Creating RAID device for $dev_two with name $install_name"
sgdisk -n -n 2:0:0 -t 2:fd00 -c 2:$install_name $dev_two
}
get_partition_sizes() {
echo "${yellow}==== Default LVM volume sizes ===="
echo "${yellow}EFI: ${cyan}$efi_size"
echo "${yellow}/: ${cyan}$root_size"
echo "${yellow}/home: ${cyan}$home_size"
echo "${yellow}/var/: ${cyan}$var_size"
echo "${yellow}/var/lib: ${cyan}$varlib_size"
echo "${yellow}/tmp/: ${cyan}$tmp_size"
echo
}
get_lvm_defaults() {
echo "${yellow}==== Default LVM configuration ===="
echo "${yellow}Physical volume name: ${cyan}$physical_vol"
echo "${yellow}Volume group name: ${cyan}$vol_group"
echo "${yellow}Root volume name: ${cyan}$vol_root"
echo "${yellow}Temp volume name: ${cyan}$vol_tmp"
echo "${yellow}Variable volume name: ${cyan}$vol_var"
echo "${yellow}Variable Library volume name: ${cyan}$vol_varlib"
echo "${yellow}Home volume name: ${cyan}$vol_home"
echo
}
prompt_partition_sizes() {
get_partition_sizes
read -r -p "${white}Would you like to change these values? [y/n] >> " answer
case $answer in
[Yy]* ) echo -n "${green}Please enter ${red}ONLY ${green}the numbers in the following order with ${red}GB or M NO commas just SPACES${white}: efi root home var varlib tmp [e.g. 600M 20G...etc] >> ${white}"; read efi_size root_size home_size var_size varlib_size tmp_size ; prompt_partition_sizes;;
[Nn]*) echo "${green}Continuing!";;
*) echo "Please input correct argument";;
esac
}
zero_drives() {
sgdisk -Z $1
sgdisk -Z $2
}
mdadm_setup() {
echo "${green}Setting up RAID 1 with $dev_one and $dev_two on $raid_device"
dev_one+="2" # Append 2 to make /dev/sda /dev/sda2 partition from sgdisk
dev_two+="2"
mdadm --create $raid_device --bitmap=internal --level=1 --raid-disks=2 $dev_one 2 $dev_two
if [ $? -eq 0 ]; then
echo "${green}[INFO] RAID creation successful, zeroing new RAID device ${yellow}$raid_device ${off}"
sgdisk -Z $raid_device
echo "${green}[INFO] $raid_device zeroed, partioning and assigning UUID ${yellow}$raid_uuid${off}"
sgdisk -n 1:0:0 -t 1:$raid_uuid -c 1:$install_name $raid_device
echo "${green}[SUCCESS] Done.${off}"
dev_one=${dev_one/2} # Removing trailing two for editing later on in the file
dev_two=${dev_two/2}
else
echo "${red}[ERROR] Couldn't create RAID! Bailing.${off}"
exit 1
fi
}
lvm_setup() {
get_lvm_defaults
read -r -p "${green} Before proceeding, would you like to change the default values of the LVM setup? [y/n] >> " answer
case $answer in
[Yy]* ) echo -n "${white}Specify the volume names in the same order as the list. [i.e. name1 name2 name3] >> "; read physical_vol vol_group vol_root vol_tmp vol_var vol_varlib vol_home ; lvm_setup ;;
[Nn]*) echo "${green}[INFO] Continuing with defaults";;
*) echo "Please input correct argument";;
esac
}
post_install_steps() {
clear
echo "${yellow}Drive setup successful! Please follow the following steps (KEEP THIS SCRIPT RUNNING):"
echo "1. Run the Ubuntu installer"
echo "2. when getting on the installation type, select 'something else' for manual partitioning"
echo "3. Map each partition (home,root,tmp etc) as an ext4 filesystem and choose the appropriate mounting point (/ for root /home for home, etc)"
echo "4. Select your main device i.e. /dev/sda for boot loader"
echo "5. Proceed with install AND DO NOT EXIT THE LIVE BOOT! THERE ARE MORE STEPS"
echo
echo -n "Once the above is done successfully, please type 'continue' to proceed with the final steps" ; read answer
case $answer in
"continue" ) post_install_setup ;;
*) echo "Please input correct argument";;
esac
}
post_install_setup() {
echo "${green}mounting new partitions"
mount /dev/mapper/$vol_group-$vol_home /target/home
mount /dev/mapper/$vol_group-$vol_tmp /target/tmp
mount /dev/mapper/$vol_group-$vol_var /target/var
mount /dev/mapper/$vol_group-$vol_varlib /target/var/lib
echo "${green}All volumes mounted, pre-binding for chroot..."
cd target ; mount --bind /dev dev ; mount --bind /proc proc ; mount --bind /sys sys
echo "${green}Binding done, chrooting..."
chroot .
echo "${green}Adding nameserver to /etc/resolv.conf"
echo "nameserver 1.1.1.1" >> /etc/resolv.conf
cat /etc/resolv.conf
echo "Installing mdadm requisite"
apt install mdadm > /dev/null #otherwise you get pipe leak warnings that mean nothing
echo "Fixing mdadm config"
sed -i 's/name.*//g' /etc/mdadm/mdadm.conf
echo "Add RAID 1 to module list"
echo raid1 >> /etc/modules
echo "Updating ramdisk"
update-initramfs -u
echo "done, exiting chroot"
exit
}
edit_efi_partition() {
echo "Cloning EFI partition"
dev_one+="1"
dev_two+="1"
dd if=$dev_one of=$dev_two bs=4096
dev_one=${dev_one/1}
dev_two=${dev_one/1}
echo "Checking UUID's match"
blkid /dev/sd[ab]1 # Need to add check for UUID=
echo "Install EFI partition on the second disk"
apt install efibootmgr
efibootmgr -v
echo
read -p "Please input the shimx64.efi FULL path of ubuntu boot [i.e. \EFI\ubuntu\shimx64.efi ] >> " efi_file
echo "Duplicating efi entry into secondary drive $dev_two"
efibootmgr -c -d $dev_two -p 1 -L "ubuntu_secondary" -l efi_file
efibootmgr -v
echo "check everything is fine and reboot if the syncing on the RAID 1 is done"
cat /proc/mdstat
exit
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment