How can I script the creation of a single partition that uses the entire device?

12,291

Solution 1

You can do this like this :

parted /dev/sdf --script -- mkpart primary 0 -1

Solution 2

I ran into issues when I attempted to use the other answer. What worked for me was 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

-or-

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

Depending on your HDD.

Solution 3

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:
12,291

Related videos on Youtube

Mike Deck
Author by

Mike Deck

Updated on September 18, 2022

Comments

  • Mike Deck
    Mike Deck over 1 year

    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?

  • mikeserv
    mikeserv over 8 years
    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.
  • slm
    slm over 8 years
    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.
  • mikeserv
    mikeserv over 8 years
    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
    mikeserv over 8 years
    But i was lying before - megaman remains mysterious to me. Maybe he always will.
  • nachoparker
    nachoparker over 6 years
    with parted /dev/sdf --script -- mkpart primary 0 -1 I got a warning for not aligned for best performance. It went fine with percentages