11

This is disgusting to me, but it works:

echo -e "n\np\n1\n\n\nt\nc\na\n1\nw" | fdisk /dev/sdb

I would like to know a more prudent or elegant way to accomplish this. The entire boot record and partition table is being zeroed prior to this task, so we can trust that partition 1 is available.

andyortlieb
  • 1,052
  • 1
  • 12
  • 25
  • 1
    It's also possible to write that same 'echo' on multiple lines (if you're doing it in a bash script) - same level of 'disgusting', but slightly more readable. Better options in the answers below though :-) – Ralph Bolton Nov 08 '17 at 10:29

2 Answers2

6

Take a look at parted which can be used in command line mode for scripting. sfdisk can also be scripted but note the caveats about GPTs and partition sizes.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • 1
    I'm sure it's in here somewhere, but I'm having a hard time finding how you can specify to use all of the available space. – andyortlieb Oct 12 '11 at 14:31
4

sfdisk

echo 'start=2048, type=83' | sudo sfdisk /dev/sdX

should fill a disk with a single partition.

See: https://superuser.com/questions/332252/creating-and-formating-a-partition-using-a-bash-script/1132834#1132834 for a detailed explanation of the command syntax, including the type=83 part and more.