11

Can you create a partition in a usb disk using fdisk command in a single line.

fdisk command is interactive in nature, But I want to automate partition creation in a single line using fdisk command.

kumar
  • 481

6 Answers6

23

Trying to automate fdisk is possible, but it is not easy to maintain. As other answers note, either parted or sfdisk are designed to do what you want and are easier to automate.

parted

To create a partition in one line with parted:

parted -a optimal /dev/usb mkpart primary 0% 4096MB

as seen in this UNIX SE post. Each of the parts is pretty self-explanatory, but just in case here is how mkpart is defined:

mkpart [part-type fs-type name] start end

where things in square brackets are optional, but you probably want primary for your part-type, start at 0% and end at 4096MB or however large your USB stick is.

chicks
  • 3,915
  • 10
  • 29
  • 37
8

sfdisk

This possibility was previously mentioned by Phil Hollenback by I wanted to give an example. To erase everything and create a single partition with it you can:

dev='/dev/sdb'
sudo umount "$dev"
echo 'type=83' | sudo sfdisk "$dev"
sudo mkfs.ext4 "${dev}1"

where 83 is the "Linux file system" partition type, suitable for creating a filesystem in Linux as I showed.

You could also get some inspiration of what it can do with:

sudo sfdisk -d "$dev"

which in my current system outputs:

label: gpt
label-id: EAC5F401-FA48-41C4-BB27-509F9AEE2369
device: /dev/nvme0n1
unit: sectors
first-lba: 2048
last-lba: 4000797326
sector-size: 512

/dev/nvme0n1p1 : start= 2048, size= 2201600, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=55DBDE7A-290B-4A78-8CE2-35FFCE4C3BAD /dev/nvme0n1p2 : start= 2203648, size= 4194304, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=6AED9F41-3D8B-40C2-A13B-3B461C65893D /dev/nvme0n1p3 : start= 6397952, size= 3994396672, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=528580E8-EA7C-4BEE-A67B-648FAA51FA16

One cool thing about that format is that it is identical to the sfdisk input, thus allowing to backup and restore your current partition setup.

See also: How to create and format a partition using a Bash script? on Super User

fdisk

If you still want to do it with fdisk for some reason, this also works:

dev='/dev/sdb'
sudo umount "$dev"
printf "o\nn\np\n1\n\n\nw\n" | sudo fdisk "$dev"
sudo mkfs.ext4 "${dev}1"

but it is much less clean as it relies on the order of interactive questions fdisk asks you, which might in principle change one day (though it would likely break a bunch of old scripts in the process if it did so).

2

You probably need to use the parted command instead of fdisk.

2

sfdisk also has a non-interactive mode that reads in partition information from stdin. parted is more flexible, though.

justarobert
  • 1,889
2

Use sfdisk instead.

The sfdisk man page is a little confusing, here's some specific examples of how to automate partition setup with sfdisk. One example is you can save the partition info from one drive via sfdisk -l and then dump that directly on to a new drive.

1

For GPT tables you can use sgdisk:

sgdisk -n 0:0:0 /dev/sde

-n, --new=partnum:start:end Create a new partition. You enter a partition number, starting sector, and an ending sector. Both start and end sectors can be specified in absolute terms as sector numbers or as positions measured in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P); for instance, 40M specifies a position 40MiB from the start of the disk. You can specify locations relative to the start or end of the specified default range by preceding the number by a '+' or '-' symbol, as in +2G to specify a point 2GiB after the default start sector, or -200M to specify a point 200MiB before the last available sector. A start or end value of 0 specifies the default value, which is the start of the largest available block for the start sector and the end of the same block for the end sector. A partnum value of 0 causes the program to use the first available partition number.