Skip to content

Instantly share code, notes, and snippets.

@walditamaniko
Last active August 23, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walditamaniko/9e25b655e8e3bcafda4e4d23dadcf2ae to your computer and use it in GitHub Desktop.
Save walditamaniko/9e25b655e8e3bcafda4e4d23dadcf2ae to your computer and use it in GitHub Desktop.
Easy to use Bash Script to Partition Disk on Ubuntu.

Intro

This is my bash script to automatically partitioning new disk on Ubuntu.

The new disk will be partitioning as EXT4 filesystem.

I'm made this to automatically partition disk on Google Cloud Platform Ubuntu VM. But of course you can use this to any Ubuntu VM.

The list of command is get from https://cloud.google.com/compute/docs/disks/add-persistent-disk#expandable-2.

So you don't need to enter a command one by one.

Installation

Just run this command in your Ubuntu terminal:

wget https://gist.github.com/walditamaniko/9e25b655e8e3bcafda4e4d23dadcf2ae/raw/a2e57f617ef15880a9453a8f4d468f1b350a0e80/partition-disk.sh;sudo chmod +x partition-disk.sh;sudo ./partition-disk.sh

Then follow the instruction in your terminal

#!/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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment