Scripteable GPT partitions using parted

44,664

Solution 1

It's correct in principle but you might consider reducing it to a single parted call.

parted --script /device \
    mklabel gpt \
    mkpart primary 1MiB 100MiB \
    mkpart primary 100MiB 200MiB \
    ...

Your alignment issue is probably because you use MB instead of MiB. You should not need an actual align-check command when creating partitions on MiB boundaries / on a known device.

Solution 2

I know this is old and a pretty good answer in that you can use MiB, but I'd like to throw another option out there for other folks.

Within the script call (--script or -s for the short version), you can add the -a option, which tells it to align and pass the option "optimal" when creating the partitions. Something like this:

sudo parted -s -a optimal -- /dev/sdX mkpart primary 1MiB -2048s

this is just an example of starting at the 1st Mebibyte and ending at the end of the disk minus the last Mebibyte to leave the GPT table in place.

Share:
44,664

Related videos on Youtube

ART
Author by

ART

Updated on September 18, 2022

Comments

  • ART
    ART over 1 year

    I am partitioning eMMC using following commands in the script,

    parted /dev/mmcblk0 --script mklabel gpt
    parted /dev/mmcblk0 --script mkpart primary ext4 32MB 132MB
    parted /dev/mmcblk0 --script mkpart primary ext4 233MB 433MB
    parted /dev/mmcblk0 --script mkpart primary ext4 433MB 533MB
    parted /dev/mmcblk0 --script mkpart primary ext4 533MB 593MB
    parted /dev/mmcblk0 --script mkpart primary ext4 593MB 793MB
    parted /dev/mmcblk0 --script mkpart primary ext4 793MB 3800MB
    parted /dev/mmcblk0 --script align-check min 1
    
    1. Is it the correct way to create partition in the script ? Is there any better way ?
    2. After creating first partition i am getting following warning

      Warning: The resulting partition is not properly aligned for best performance.

    Do i need to worry about it ? I tried parted /dev/mmcblk0 --script align-check min 1 but not sure that's the solution. Any pointers for that?
    I am going through this link meanwhile any other suggestions ?

    Edit: Just a quick reference for frostschutz reply,

    MiB = Mebibyte = 1024 KiB
    KiB = Kibibyte = 1024 Bytes
    MB = Megabyte = 1,000 KB
    KB = Kilobyte = 1,000 Bytes
    
    • grenix
      grenix about 2 years
      It may be (at least didactically) worth to mention that parted /dev/mmcblk0 --script mklabel gpt deletes all existing partitions. For me it was parted /dev/mmcblk0 --script mklabel msdos
  • ART
    ART almost 9 years
    Thank you for reply, Let me test it. I will update the result.
  • ART
    ART almost 9 years
    I don't get that warning now, Thank you :)
  • ART
    ART over 8 years
  • RabT
    RabT about 7 years
    I have tried to modify your code for use in a different context. Are you willing to advise? Here is the link: unix.stackexchange.com/questions/349833/…
  • niry
    niry almost 6 years
    I'd add '--' between the device and the commands - this way you can use '-1' for max size.
  • plugwash
    plugwash about 5 years
    At least on my system the "-2048s" doesn't seem to be accepted as a notation for the "end" parameter, it seems to be intepreted as an option.
  • plugwash
    plugwash about 5 years
    Ok, after trying many different google searches it seems the lone dash in the middle of the OPs command needs to be a double dash. I would edit the answer to correct these issues but I am running into stack exchanges minimum edit size rule.
  • Asmadeus
    Asmadeus over 4 years
    FWIW, parted is smart enough to not cobbler the GPT backup label if you end the partition with -0 (now checked), thus saving one MiB. One might also want to change the primary here to an arbitrary partition name.
  • Khrystoph
    Khrystoph over 4 years
    @plugwash, you have to have the "--" in the command, otherwise it complains about the "-" in the "-2048s" as the minus indicates how far to go back from the end. If the "--" is not present, bash interprets it as a cli arg, and there is no -2 arg in parted. this was likely an error in copy to M$ word or something, then into the response box. iirc, older versions of parted didn't like or don't accept setting the second parameter to -1MiB, which is why I used sector count, because that WAS allowed. asmadeus: I'm not sure parted has always behaved this way, so it's better safe than sorry.