Created
June 15, 2020 17:58
-
-
Save basilmusa/eb7c972b07f4403de340f324fb692a43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################ | |
# This script simply formats a block device and mounts it to the data | |
# directory in a very safe manner by checking that the block device is | |
# completely empty | |
# | |
############################################################################ | |
set -eu | |
if [ $# -ne 2 ] | |
then | |
echo ""; | |
echo " Usage: ./${0} <block_device> <mount_directory>"; | |
echo; | |
echo " Example: ./${0} /dev/vdb /data"; | |
echo; | |
echo " To figure out the block devices on the machine use lsblk command"; | |
echo ""; | |
exit 1; | |
fi | |
set -x | |
BLOCK_DEVICE_PATH="${1}" | |
MOUNT_TO_DIR="${2}" | |
## SAFETY CHECK ########################################################### | |
# Check if ${BLOCK_DEVICE_PATH} is mounted, if yes, then exit | |
if [[ $(/bin/mount | grep -q "${BLOCK_DEVICE_PATH}") ]]; then | |
echo "BLOCK DEVICE ${BLOCK_DEVICE_PATH} ALREADY MOUNTED" | |
exit 1; | |
fi | |
## SAFETY CHECK ########################################################### | |
if [[ $(/sbin/blkid ${BLOCK_DEVICE_PATH}) ]]; then | |
echo "BLOCK DEVICE ALREADY INITIALIZED, WILL NOT PROCEED WITH SCRIPT"; | |
exit 1; | |
fi | |
## CREATE PARTITION TABLE AND CREATE PARTITION | |
parted --script ${BLOCK_DEVICE_PATH} mklabel gpt | |
parted --script ${BLOCK_DEVICE_PATH} mkpart primary ext4 0% 100% | |
## NEEDED FOR lsblk TO REFRESH | |
echo "Sleeping 5 seconds" | |
sleep 5; | |
## PARTITIONNAME WITHOUR '/dev' | |
PARTITION_NAME=`lsblk -l ${BLOCK_DEVICE_PATH} | tail -1 | awk '{print $1}'` | |
## SAFETY CHECK ########################################################### | |
if [[ ${#PARTITION_NAME} -ne 4 ]]; then | |
echo "EXITING SINCE [$PARTITION_NAME] DOES NOT CONTAIN 4 CHARACTERS"; | |
exit 1; | |
fi; | |
## Format it as ext4 | |
mkfs.ext4 "/dev/$PARTITION_NAME" | |
## Create a mount directory at /data if does not exist | |
mkdir -p "${MOUNT_TO_DIR}" | |
# Mount it in /etc/fstab | |
UUID_STRING=`blkid -o export /dev/$PARTITION_NAME | grep "^UUID"` | |
echo -e "\n$UUID_STRING ${MOUNT_TO_DIR} ext4 defaults,nofail 0 0" >> /etc/fstab | |
# Run mount -a | |
mount -a | |
# TO UNDO WHAT THE SCRIPT HAS DONE | |
# PLEASE DO NOT COMMENT THIS OUT, ONLY SERVES FOR DOCUMENTATION | |
# USE ONLY WHEN YOU ARE SURE WHAT YOU ARE DOING | |
# umount /dev/vdb1 | |
# wipefs -a /dev/vdb1 | |
# parted /dev/vdb rm 1 | |
# wipefs -a /dev/vdb |
Yeah this is quite nice!
Thanks for the nice article 👍
This script fails when the device name has more than 3 characters, such as nvme0 instead of sda or sdb:
/dev/nvme0n1
The safety check needs to be revised
EXITING SINCE [nvme0n1p1] DOES NOT CONTAIN 4 CHARACTERS
P.S.: Thanks for the script and article!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great great script !!!!