How to Non-Interactively Create USB Partitions Using fdisk in One Command Line


4 views

Traditional fdisk usage requires manual interaction through prompts, making it unsuitable for scripting or automation scenarios. This becomes problematic when you need to:

  • Automate deployment scripts
  • Create partitions in CI/CD pipelines
  • Configure multiple identical storage devices
  • Perform bulk operations on USB drives

We can leverage fdisk's ability to accept commands via standard input. The secret lies in piping a sequence of commands to fdisk:

echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdX

Let's analyze what each part does:

n       # Create new partition
p       # Primary partition type
1       # Partition number (1)
[Enter] # Accept default first sector
[Enter] # Accept default last sector
w       # Write changes to disk

Basic Partition Creation

echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdb

Creating Specific Partition Types

# Create Linux filesystem (83) partition
echo -e "n\np\n1\n\n\nt\n83\nw" | sudo fdisk /dev/sdc

# Create FAT32 partition (type c)
echo -e "n\np\n1\n\n\nt\nc\nw" | sudo fdisk /dev/sdd

Advanced: Multiple Partitions

echo -e "n\np\n1\n\n+2G\nn\np\n2\n\n\nw" | sudo fdisk /dev/sde
  • Always replace /dev/sdX with your actual device
  • Use lsblk or fdisk -l to verify device names
  • The commands will destroy existing data on the device
  • Add partprobe to refresh partition tables
  • Consider sfdisk for more complex scripting needs

For more complex scenarios, consider:

# Using parted
sudo parted -s /dev/sdf mklabel gpt mkpart primary 0% 100%

# Using sfdisk
echo 'start=2048, type=83' | sudo sfdisk /dev/sdg

While fdisk is the standard tool for disk partitioning on Linux systems, its interactive nature poses challenges for automation and scripting. Many system administrators need to create partitions programmatically during:

  • Automated provisioning scripts
  • CI/CD pipelines for embedded systems
  • Mass USB drive preparation
  • Cloud initialization scripts

We can leverage printf to pipe commands directly into fdisk:

printf "n\np\n1\n\n\nw\n" | sudo fdisk /dev/sdX

Here's what each part does:

n    - Create new partition
p    - Primary partition type
1    - Partition number
\n\n - Accept default first and last sectors
w    - Write changes to disk

Basic FAT32 partition:

printf "n\np\n1\n\n\nt\nc\nw\n" | sudo fdisk /dev/sdX
sudo mkfs.vfat /dev/sdX1

GPT partition with ext4:

printf "g\nn\n\n\n+5G\nt\n29\nw\n" | sudo fdisk /dev/sdX
sudo mkfs.ext4 /dev/sdX1

For more complex scenarios, parted might be better suited:

sudo parted /dev/sdX --script mklabel gpt mkpart primary fat32 1MiB 100%

Always include verification steps:

# Check if device exists
if [ -b /dev/sdX ]; then
    printf "n\np\n1\n\n\nw\n" | sudo fdisk /dev/sdX
    partprobe /dev/sdX
    sudo mkfs.ext4 /dev/sdX1
else
    echo "Device not found!" >&2
    exit 1
fi
Tool Advantages Limitations
fdisk Universal, available everywhere Limited scripting capabilities
parted Better scripting support Slightly less universal
sfdisk Designed for scripting Different syntax

Here's a complete script for USB preparation:

#!/bin/bash

DEVICE="/dev/sdX"

# Verify device exists
if [ ! -b "$DEVICE" ]; then
    echo "Error: Device $DEVICE not found" >&2
    exit 1
fi

# Unmount any mounted partitions
for partition in $(lsblk -ln -o NAME "$DEVICE" | tail -n +2); do
    umount "/dev/$partition" 2>/dev/null
done

# Create new partition table and single partition
printf "o\nn\np\n1\n\n\nw\n" | fdisk "$DEVICE"

# Refresh partition table
partprobe "$DEVICE"

# Format as FAT32
mkfs.vfat -F32 "${DEVICE}1"

echo "Partitioning complete for ${DEVICE}1"