Created
February 24, 2026 12:54
-
-
Save maxju/2e9ef90ebcfba8a4ea9dc7e15de06dc4 to your computer and use it in GitHub Desktop.
Format device with two ext4 partitions of equal size
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 | |
| set -euo pipefail | |
| if [ -z "${1:-}" ]; then | |
| echo "Usage: $0 <device>" | |
| echo " e.g. $0 vdb" | |
| exit 1 | |
| fi | |
| DEVICE="$1" | |
| DISK="/dev/${DEVICE}" | |
| if [ ! -b "$DISK" ]; then | |
| echo "Error: $DISK is not a block device" | |
| exit 1 | |
| fi | |
| echo "Creating GPT label and two equal partitions on $DISK..." | |
| sudo /sbin/parted -s "$DISK" mklabel gpt | |
| sudo /sbin/parted -s "$DISK" mkpart primary ext4 0% 50% | |
| sudo /sbin/parted -s "$DISK" mkpart primary ext4 50% 100% | |
| sudo /sbin/partprobe "$DISK" | |
| for i in 1 2; do | |
| PART="${DISK}${i}" | |
| # Wait for partition node to appear | |
| for attempt in $(seq 1 10); do | |
| [ -b "$PART" ] && break | |
| sleep 1 | |
| done | |
| if [ ! -b "$PART" ]; then | |
| echo "Error: partition $PART did not appear" | |
| exit 1 | |
| fi | |
| LABEL="${DEVICE}${i}" | |
| MOUNTPOINT="/mnt/${LABEL}" | |
| echo "Formatting $PART as ext4 with label $LABEL..." | |
| sudo mkfs.ext4 -F -L "$LABEL" "$PART" | |
| sudo mkdir -p "$MOUNTPOINT" | |
| UUID="$(sudo blkid -s UUID -o value "$PART")" | |
| echo "UUID=${UUID} ${MOUNTPOINT} ext4 defaults,nofail 0 2" | sudo tee -a /etc/fstab | |
| echo "Mounting $PART at $MOUNTPOINT..." | |
| sudo mount "$PART" "$MOUNTPOINT" | |
| done | |
| sudo mount -a | |
| echo "Done. Both partitions of $DISK are mounted." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment