Skip to content

Instantly share code, notes, and snippets.

@tonyclemmey
Created October 18, 2022 23:07
Show Gist options
  • Save tonyclemmey/04969d7468cc007aa4e6be01c708bd9c to your computer and use it in GitHub Desktop.
Save tonyclemmey/04969d7468cc007aa4e6be01c708bd9c to your computer and use it in GitHub Desktop.
Ubuntu Disks Setup
#!/bin/bash
# ===================================
# ########## Bash Colours ###########
# ===================================
NC='\033[31;0m' # no colors or formatting
RED='\033[0;31;1m' # print text in bold Red
GRE='\033[0;32;1m' # print text in bold Green
YEL='\033[0;33;1m' # print text in bold Yellow
BLU='\033[0;34;1m' # print text in bold Blue
PUR='\033[0;35;1m' # print text in bold Purple
CYA='\033[0;36;1m' # print text in bold Cyan
GRA='\033[0;37;1m' # print text in bold Gray
# ===================================
# ######## ENV Variables ############
# ===================================
DISK_HOME='/dev/sdb'
DISK_DATA='/dev/sdc'
# ===================================
# ######## Require root user ########
# ===================================
if [[ $UID -ne 0 ]]; then
echo -e "${RED}This script needs to be run as root (with sudo).${NC}"
exit 1
fi
# ===================================
# ######## Setup Home Disk ##########
# ===================================
BACKUP_SUFFIX=$(date +"%Y-%m-%d_%H-%M-%S")
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Creating and formating the new home partition${NC}"
echo -e "${YEL}===================================${NC}"
# @link https://superuser.com/questions/332252/how-to-create-and-format-a-partition-using-a-bash-script
sudo /sbin/parted "${DISK_HOME}" mklabel gpt --script
sudo /sbin/parted "${DISK_HOME}" mkpart primary ext4 0% 100% --script
sudo /sbin/parted "${DISK_HOME}" print --script
echo "y" | sudo mkfs.ext4 "${DISK_HOME}1"
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Grabbing UUID for the new home partition${NC}"
echo -e "${YEL}===================================${NC}"
# @ link https://stackoverflow.com/questions/13565658/right-tool-to-filter-the-uuid-from-the-output-of-blkid-program-using-grep-cut
HOME_UUID=$(blkid -s UUID -o value "${DISK_HOME}1")
echo -e "${GRE}${HOME_UUID}${NC}"
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Updating fstab with the new home partition${NC}"
echo -e "${YEL}===================================${NC}"
sudo cp /etc/fstab /etc/fstab_backup-$BACKUP_SUFFIX
sudo tee -a /etc/fstab <<EOF
UUID=${HOME_UUID} /media/home ext4 defaults 0 2
EOF
echo -e "${GRE}See changes made to /etc/fstab${NC}"
sudo diff /etc/fstab /etc/fstab_backup-$BACKUP_SUFFIX
sudo mkdir -p /media/home
sudo mount -a
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Copying /home to the new home partition${NC}"
echo -e "${YEL}===================================${NC}"
sudo rsync -aXS --exclude='/*/.gvfs' /home/. /media/home/.
echo -e "${GRE}ls -al /media/home/${NC}"
sudo ls -al /media/home/
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Backing up original home directory${NC}"
echo -e "${YEL}===================================${NC}"
sudo mv /home /old_home && sudo mkdir /home
echo -e "${GRE}ls -al /old_home/${NC}"
sudo ls -al /old_home
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Updating fstab${NC}"
echo -e "${YEL}===================================${NC}"
sudo sed -ie '/^\UUID='"${HOME_UUID}"'/ s/\/media\/home/\/home/' /etc/fstab
echo -e "${GRE}See changes made to /etc/fstab${NC}"
sudo diff /etc/fstab /etc/fstab_backup-$BACKUP_SUFFIX
sudo mount -a
sudo ls -al /home/
# ===================================
# ######## Setup Data Disk ##########
# ===================================
BACKUP_SUFFIX=$(date +"%Y-%m-%d_%H-%M-%S")
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Creating and formating the new data partition${NC}"
echo -e "${YEL}===================================${NC}"
sudo /sbin/parted "${DISK_DATA}" mklabel gpt --script
sudo /sbin/parted "${DISK_DATA}" mkpart primary ext4 0% 100% --script
sudo /sbin/parted "${DISK_DATA}" print --script
echo "y" | sudo mkfs.ext4 "${DISK_DATA}1"
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Grabbing UUID for the new home partition${NC}"
echo -e "${YEL}===================================${NC}"
# @ link https://stackoverflow.com/questions/13565658/right-tool-to-filter-the-uuid-from-the-output-of-blkid-program-using-grep-cut
DATA_UUID=$(blkid -s UUID -o value "${DISK_DATA}1")
echo -e "${GRE}${DATA_UUID}${NC}"
echo -e "${YEL}===================================${NC}"
echo -e "${YEL}Updating fstab with the new data partition${NC}"
echo -e "${YEL}===================================${NC}"
sudo cp /etc/fstab /etc/fstab_backup-$BACKUP_SUFFIX
sudo diff /etc/fstab /etc/fstab_backup-$BACKUP_SUFFIX
sudo tee -a /etc/fstab <<EOF
UUID=${DATA_UUID} /data ext4 defaults 0 2
EOF
echo -e "${GRE}See changes made to /etc/fstab${NC}"
sudo diff /etc/fstab /etc/fstab_backup-$BACKUP_SUFFIX
sudo mkdir -p /data
sudo mount -a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment