#!/usr/bin/env bash BOLD="" RED="" GREEN="" YELLOW="" BLUE="" MAGENTA="" CYAN="" WHITE="" RESET="" if [ -t 1 ]; then BOLD="$(tput bold)" 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)" RESET="$(tput sgr0)" fi : ${DSK="/dev/sdc"} (( $# > 0 )) && DSK="$1" echo "${RED}WARNING: The disk '$DSK' is going to be re-partitioned and formatted!${RESET}" sleep 1 echo "--------------------" fdisk -l "$DSK" echo "--------------------" echo "If this isn't the correct disk, hit CTRL-C or enter 'n' at the following prompt" sleep 2 echo sfmt="" read -p " Do you wish to continue with formatting '$DSK'? (y/N) > " sfmt if [[ "$sfmt" != "y" ]] && [[ "$sfmt" != "yes" ]] && [[ "$sfmt" != "Y" ]]; then echo " !! Cancelling." echo exit 1 fi echo "Please check below that there's no existing PV's, VG's, or LV's for the disk $DSK" sleep 1 echo "--------------------" echo -e "\n -> pvs" pvs echo -e "\n -> vgs" vgs echo -e "\n -> lvs" lvs echo "--------------------" echo "If you see any PV's, VG's or LV's on ${DSK}, please remove them with:" echo echo " vgremove -f myvolgroup # Delete the VG 'myvolgroup' and all LVs within it" echo " pvremove ${DSK}2 # replace ${DSK}2 with the correct PV partition" echo echo "If you don't see any PVs/VGs for disk $DSK - then you may continue. Otherwise type 'n'" echo "and remove the problematic VGs + PVs before running this script again." echo sfmt="" read -p " Do you wish to continue with formatting '$DSK'? (y/N) > " sfmt if [[ "$sfmt" != "y" ]] && [[ "$sfmt" != "yes" ]] && [[ "$sfmt" != "Y" ]]; then echo " !! Cancelling." echo exit 1 fi grn() { echo -e "${GREEN}$*${RESET}" } grn "\n >>> Disabling all activate LVM volumes\n" vgchange -a n grn "\n >>> Creating 2G boot partition + 2nd partition filling disk\n" sleep 2 # create a 2G boot partition, and a 2nd partition filling the disk for LVM echo $',+2G,L,*\n,,L,' | sfdisk "$DSK" # format boot partition with ext3 grn "\n >>> Formatting ${DSK}1 with Ext3 filesystem\n" mkfs.ext3 "${DSK}1" grn "\n >>> Forcing LVM re-scan to clear any old detected LVMs\n" pvscan pvscan --cache vgscan vgscan --cache lvscan lvscan --cache # create LVM vg0 grn "\n >>> Creating LVM vg0 on ${DSK}2\n" pvcreate "${DSK}2" vgcreate vg0 "${DSK}2" grn "\n >>> Creating 5G root LV\n" # 5G non-LVM rootfs lvcreate -L 5G -n root vg0 # 15G thinpool for /home /root /var/log and /usr grn "\n >>> Creating 15G thin pool 'lvthin'\n" lvcreate --type thin-pool -L 15G --thinpool lvthin vg0 lvextend --poolmetadatasize +300M vg0/lvthin grn "\n >>> Creating 2G lvthin/home\n" lvcreate -V 2G --thinpool lvthin -n home vg0 grn "\n >>> Creating 2G lvthin/roothome\n" lvcreate -V 2G --thinpool lvthin -n roothome vg0 grn "\n >>> Creating 2G lvthin/logs\n" lvcreate -V 2G --thinpool lvthin -n logs vg0 grn "\n >>> Creating 4G lvthin/usr\n" lvcreate -V 4G --thinpool lvthin -n usr vg0 grn "\n >>> Formatting lvthin/root with ext4\n" # ext4 for / - /root - /home mkfs.ext4 /dev/vg0/root grn "\n >>> Formatting lvthin/roothome with ext4\n" mkfs.ext4 /dev/vg0/roothome grn "\n >>> Formatting lvthin/home with ext4\n" mkfs.ext4 /dev/vg0/home # btrfs for /usr and /var/log - so we can compress them grn "\n >>> Formatting lvthin/logs with btrfs\n" mkfs.btrfs /dev/vg0/logs grn "\n >>> Formatting lvthin/usr with btrfs\n" mkfs.btrfs /dev/vg0/usr grn "\n >>> Mounting usr and logs\n" # create mount point for logs + usr mkdir -v -p /mnt/{logs,usr} # mount logs + usr mount -v /dev/vg0/logs /mnt/logs mount -v /dev/vg0/usr /mnt/usr # enable btrfs compression for logs + usr grn "\n >>> Setting compression flags\n" chattr +c /mnt/logs/ chattr +c /mnt/usr/ # verify that attribute 'c' shows for usr + logs grn "\n >>> Output of 'lsattr /mnt':\n" lsattr /mnt umount -v /mnt/usr umount -v /mnt/logs grn "\n [+++] FINISHED \n"