Skip to content

Instantly share code, notes, and snippets.

@GluTbl
Created October 6, 2025 06:08
Show Gist options
  • Save GluTbl/a563d45303c26fdb9aaead6d4fc59f8f to your computer and use it in GitHub Desktop.
Save GluTbl/a563d45303c26fdb9aaead6d4fc59f8f to your computer and use it in GitHub Desktop.
[Formating partion by label] #bash
#!/bin/bash
LABEL="OVERLAY_ROOT"
FSTYPE="ext4"
# Find all devices with the label
DEVICES=($(blkid | grep "LABEL=\"$LABEL\"" | cut -d: -f1))
COUNT=${#DEVICES[@]}
# Check match count
if [ "$COUNT" -eq 0 ]; then
echo "❌ No partition found with label '$LABEL'."
exit 1
elif [ "$COUNT" -gt 1 ]; then
echo "⚠️ More than one partition found with label '$LABEL':"
for dev in "${DEVICES[@]}"; do
echo " - $dev"
done
exit 2
else
DEVICE="${DEVICES[0]}"
echo "✅ Found exactly one device with label '$LABEL': $DEVICE"
# Confirm before formatting (optional safety prompt)
echo "⚠️ WARNING: This will erase all data on $DEVICE"
read -p "Are you sure you want to format $DEVICE to $FSTYPE? (yes/[no]): " CONFIRM
if [[ "$CONFIRM" != "y" && "$CONFIRM" != "yes" ]]; then
echo "❎ Format aborted."
exit 3
fi
# Format the device to ext4
echo "🔧 Formatting $DEVICE to $FSTYPE..."
sudo mkfs.ext4 -F -L "$LABEL" "$DEVICE"
if [ $? -eq 0 ]; then
echo "✅ Successfully formatted $DEVICE as $FSTYPE with label '$LABEL'."
else
echo "❌ Formatting failed."
exit 4
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment