Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am specifically using this to partition and mount EBS volumes on Amazon EC2 instances, but really this should be generally applicable to initializing any new drive.

As the question says I want to script the creation of a single primary partition which uses all available space on the given device. I won't know ahead of time how big the device is and it could be very large (i.e. several TB).

In researching this it seems parted is the best command to use for scripting. I think something along the lines of parted /dev/sdf mkpart primary 0 END is what I want, but I'm having a hard time figuring out an elegant way to determine what END should be. Does anyone know an easy way to do this?

share|improve this question

3 Answers 3

You can do this like this :

parted /dev/sdf --script -- mkpart primary 0 -1
share|improve this answer

I ran into issues when I attempted to use the other answer. What worked for me wa to use the percentages approach like this instead:

$ parted /dev/sdf --script -- mkpart primary 0% 100%

There's an issue with parted and the way that it deals with the alignment of the partitions. These 2 articles discuss the issue a bit:

The math outlined in those articles simply didn't work in my scenarios. Buried in one of the comments on the second, link someone suggested using the percentages and that did work.

Incidentally you'll typically need to label the HDD as well. To do that via CLI with parted:

$ parted /dev/sde --script -- mklabel msdos


$ parted /dev/sde --script -- mklabel gpt

Depending on your HDD.

share|improve this answer
gdisk aligns correctly. In my opinion, parted is a mess. It does too much - and the things it does are only loosely connected. It's weird how it creates filesystems and partition tables together - the disk locarions for the data it writes are nothing like in the same neighborhood. I only began to understand how disks worked when I realized that the partition table is a little map that sits at its head, and it tells the os where it might look for a filesystem if it wants to. When that clicked, i understood superblocks and megaman and all of it. Well, i like to think so anyway. – mikeserv Jul 31 at 21:51
Thanks, the site of the tools in this space has always been a mess to me. I constantly mix up sfdisk, fdisk, cfdisk, parted, etc. I'll try fdisk and see if that's easier. Mainly looking for an easy to run from the cli option for inclusion in scripts. – slm Jul 31 at 23:59
Basically [gf]disk do not discriminate their stdin - they don't care if it's from a termInal. So any command sequence you would enter interactively to get what you want can be directly translated to a printf script. All of the newlines in my answer are just the (or press enter for default) options. It's pretty easy. When i play around w/ them I just go to /tmp and fallocate -l1g img and then just use my new /tmp/img file as my disk - they don't care. They just want to write a partition table. If you do the same, have a look at what happens w/ strings -1 img occasionally. – mikeserv Aug 1 at 0:05
But i was lying before - megaman remains mysterious to me. Maybe he always will. – mikeserv Aug 1 at 0:06

You can do...

printf 'n\n\n\n\n\nw\n' | fdisk /dev/disk

...for an msdos type. Practically the same thing works for gpt...

printf 'n\n\n\n\n\nw\ny\n' | gdisk /dev/disk

And you can label your disk during filesystem creation time.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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