|
#!/bin/bash |
|
# Made by Nicholas Walditama |
|
# Easily partition additional disk inserted into Google Cloud VM Instance... |
|
# ... to directory |
|
# For example: |
|
# sdb into directory /newdisk |
|
|
|
# This command is get from GCP Documentation: |
|
# https://cloud.google.com/compute/docs/disks/add-persistent-disk#expandable-2 |
|
|
|
# This is just the automation from commands in that documentation |
|
|
|
#=====Intro Message=====# |
|
cat << "EOF" |
|
===================================================================== |
|
_ _ _ _ _ _ |
|
__ ____ _| | __| (_) |_ __ _ _ __ ___ __ _ _ __ (_) | _____ |
|
\ \ /\ / / _` | |/ _` | | __/ _` | '_ ` _ \ / _` | '_ \| | |/ / _ \ |
|
\ V V / (_| | | (_| | | || (_| | | | | | | (_| | | | | | < (_) | |
|
\_/\_/ \__,_|_|\__,_|_|\__\__,_|_| |_| |_|\__,_|_| |_|_|_|\_\___/ |
|
===================================================================== |
|
Made by Nicholas Walditama |
|
# Easily partition additional disk inserted into Google Cloud VM Instance... |
|
# ... to directory |
|
# For example: |
|
# sdb into directory /newdisk |
|
# This can be used in Linux Ubuntu 16.04 |
|
===================================================================== |
|
EOF |
|
#=====Intro Message=====# |
|
|
|
|
|
# 1. First, find what device ID that inserted: |
|
cat << "EOF" |
|
1. First, find what device ID that inserted: |
|
EOF |
|
|
|
sudo lsblk |
|
|
|
cat << "EOF" |
|
===================================================================== |
|
EOF |
|
|
|
# 2. Let user input what device ID that want to be partitioned (normally sdb in GCP): |
|
echo "What the device ID that you want to partitioned? (Normally sdb)"; |
|
read -p "Type your answer here: " deviceid; |
|
|
|
# 3. Make EXT4 partition based on device ID that user input |
|
eval sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/$deviceid |
|
|
|
# 4. Ask user what directory will be use for mountpoint |
|
cat << "EOF" |
|
===================================================================== |
|
EOF |
|
echo "What directory that you want to be mountpoint for ${deviceid}?"; |
|
echo "Example: ${deviceid} is mounted as /folder"; |
|
echo "Don't forget include slash (/) sign in your answer!"; |
|
cat << "EOF" |
|
===================================================================== |
|
EOF |
|
read -p "Type your answer here: " mountpoint; |
|
|
|
# 5. Mounted disk to folder |
|
sudo mkdir -p $mountpoint; |
|
sudo mount -o discard,defaults /dev/$deviceid $mountpoint; |
|
echo "Succesfully mount ${deviceid} as ${mountpoint}"; |
|
|
|
# 6. Make backup for /etc/fstab with additional timestamp |
|
timestamp=$(date +%s); #This is function to get timestamp and stored to variable |
|
sudo cp /etc/fstab /etc/fstab.backup-$timestamp; |
|
echo "Succesfully create backup for fstab"; |
|
|
|
# 7. Configure the /etc/fstab for Set Up Auto Mount on Restart |
|
echo UUID=`sudo blkid -s UUID -o value /dev/${deviceid}` $mountpoint ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab |
|
|
|
# 8. Finish. |
|
echo "Sucesfully run all steps. Now running sudo lsblk to see if the disk has been paritioned..."; |
|
sudo lsblk; |