9

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.

1
14

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.

4
  • FWIW, the options specified here are no longer supported by sfdisk and results in an error for the above snippet.
    – logidelic
    Apr 2 '19 at 14:02
  • @chicks How is sfdisk any easier to script? Input for sfdisk is just as messy as it is fdisk.
    – Melab
    Aug 1 '20 at 16:23
  • I hope most people choose parted. I will remove the sfdisk section to reduce confusion.
    – chicks
    Aug 1 '20 at 19:33
  • 2
    As I used badblocks before, my whole disk was erased and I received an error like "Error: /dev/nvme1n1: unknown partition table" (in German: "Fehler: /dev/nvme1n1: unbekannte Partitionstabelle") when using the above command. I had to first create the partition table with parted /dev/nvme2n1 mklabel gpt and then create the partition with parted /dev/nvme2n1 mkpart primary ext4 0% 100% After that I formatted it with mkfs.ext4 /dev/nvme2n1p1. Oct 30 '20 at 7:33
5

Erase everything, and create a single partition:

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

See also: https://superuser.com/questions/332252/creating-and-formating-a-partition-using-a-bash-script

4
  • this works but using sfdisk or parted will be easier to get working and maintain
    – chicks
    Sep 11 '15 at 20:36
  • 1
    @chicks cool, I will have a look at it. Someone should have posted an example :-) Sep 11 '15 at 20:40
  • good point @Ciro, I will take care of that...
    – chicks
    Sep 12 '15 at 0:10
  • echo ",,;" | sfdisk /dev/sda In ,, the first parameter is the offset if you need to be sure to maintain it, the last one partition type is 8e for LVM on msdos and E6D6D379-F507-44C2-A23C-238F2A3DF928 for gpt. Check parameters --no-reread --no-tell-kernel if updating online and parameters --append and -N if you don't want to erase everything.
    – DustWolf
    Jul 29 at 15:37
2

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

1
  • Could you show an example of how to use the parted command? Jan 17 at 1:14
2

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

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
  • The link no longer works.
    – DustWolf
    Sep 20 '20 at 10:59
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.