While piping echo commands to fdisk (like echo -e "n\\np\\n1\\n\\n\\nt\\nc\\a\\1\\nw" | fdisk /dev/sdb
) technically works for creating a single partition using all available disk space, it raises several concerns:
- Fragile string escaping that's prone to errors
- No proper error handling or validation
- Poor readability and maintainability
- Potential version compatibility issues across different fdisk implementations
Using sfdisk (Recommended Approach)
The sfdisk
utility is specifically designed for scripted partitioning:
# Wipe existing partition table
wipefs -a /dev/sdb
# Create single partition using all space
echo 'label: gpt
,,' | sfdisk /dev/sdb
This creates a GPT partition table with a single partition spanning the entire disk. For MBR:
echo 'label: dos
,,' | sfdisk /dev/sdb
Using parted
GNU parted offers another clean solution:
parted /dev/sdb --script \
mklabel gpt \
mkpart primary 0% 100%
Using sgdisk (for GPT specifically)
sgdisk -n 0:0:0 -t 0:8300 -c 0:"Linux Filesystem" /dev/sdb
For production scripts, add verification steps:
# Verify partition was created
if ! lsblk /dev/sdb1 > /dev/null 2>&1; then
echo "Partition creation failed" >&2
exit 1
fi
Complete the process by adding filesystem creation (example with ext4):
mkfs.ext4 /dev/sdb1
For XFS:
mkfs.xfs -f /dev/sdb1
Here's a complete script that handles all steps safely:
#!/bin/bash
DISK="/dev/sdb"
# Verify disk exists
if [ ! -b "$DISK" ]; then
echo "Error: Disk $DISK not found" >&2
exit 1
fi
# Wipe existing partitions
wipefs -a "$DISK"
# Create partition table and single partition
if ! echo 'label: gpt
,,' | sfdisk "$DISK"; then
echo "Partitioning failed" >&2
exit 1
fi
# Create filesystem
if ! mkfs.ext4 "${DISK}1"; then
echo "Filesystem creation failed" >&2
exit 1
fi
echo "Successfully prepared ${DISK}1"
When automating server provisioning or storage configuration, the traditional interactive fdisk
approach becomes problematic. The common workaround of piping commands via echo
feels hacky and lacks proper error handling. Here's why we need better solutions:
# The "echo pipe" method everyone uses but hates
echo -e "n\\np\\n1\\n\\n\\nt\\nc\\a\\1\\nw" | fdisk /dev/sdb
For Linux systems, we have several production-grade alternatives:
1. Using sfdisk (The Preferred Method)
sfdisk
is specifically designed for scripting and handles edge cases better:
# Clear existing partition table
wipefs -a /dev/sdb
# Create new partition using all space
echo 'type=c' | sfdisk /dev/sdb
# Alternative with explicit sector calculation
echo 'start=2048, size=, type=c' | sfdisk /dev/sdb
2. Using parted (For Advanced Filesystems)
When working with GPT or advanced filesystems, parted
offers better control:
parted /dev/sdb --script \
mklabel msdos \
mkpart primary 0% 100%
3. Using sgdisk (For GPT Partitions)
For UEFI systems or disks larger than 2TB:
sgdisk -n 0:0:0 -t 0:8300 -c 0:"Linux FS" /dev/sdb
Always include proper validation in your scripts:
#!/bin/bash
DEVICE="/dev/sdb"
# Verify block device exists
if [ ! -b "$DEVICE" ]; then
echo "Error: Device $DEVICE not found" >&2
exit 1
fi
# Partition with error checking
if ! echo 'type=83' | sfdisk "$DEVICE"; then
echo "Partitioning failed" >&2
exit 1
fi
# Verify partition creation
if [ ! -b "${DEVICE}1" ]; then
echo "Partition not created properly" >&2
exit 1
fi
After partitioning, you'll typically want to create a filesystem:
mkfs.ext4 -L "data_disk" /dev/sdb1
# For XFS
mkfs.xfs -f /dev/sdb1
# For btrfs
mkfs.btrfs -f /dev/sdb1