Bash scripting parted for GPT
Solution 1
Dru's approach will probably work, although I've not tested it. Two other approaches are to use a combination of sgdisk
and parted
, or to use sgdisk
in a more complex way. Specifically, you could do either of:
- Use
sgdisk -e
to move the backup partition table data to the end of the disk, followed byparted {device-file} resize...
to resize the partition. Using two utilities rather than one is a bit inelegant, but it should work. - Use
sgdisk -e {device-file}
to move the backup partition table data to the end of the disk, followed bysgdisk -d...
to delete the partition and thensgdisk -n...
to create a new partition in its place. You can probably combine these into just two or maybe even just one call tosgdisk
. One caveat is that this approach will change the GUID value for the partition. If it's important that it remain the same, you'd have to extract that data and reset the GUID value -- or given that you're starting from a known image, you could store the GUID value as part of your script and reset it without first extracting it.
Note that I've not tested either of these approaches, so I can't guarantee them any more than I can guarantee Dru's method.
Solution 2
You can run an expect
script from your Bash script parted
to answer the question.
#!/usr/bin/expect
eval spawn parted /dev/sda resize 2 0% 100%
expect "Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)? Fix/Ignore/Cancel? "
send "F\r"
expect eof
Related videos on Youtube
![John P. Fisher](https://lh3.googleusercontent.com/--EcopMayQpY/AAAAAAAAAAI/AAAAAAAAAJQ/xvLFXehaqE4/photo.jpg?sz=256)
John P. Fisher
Updated on September 18, 2022Comments
-
John P. Fisher almost 2 years
Using Bash & Python scripts in Ubuntu 12.04, we are delivering a disk image to customers as a file (generated by
dd
). Oncedd
’d to the new disk, we resize the partition to fit the disk. Now that we are using GPT disks, I need to provide a new non-interactive, non-X script.The problem is
parted
generates an interactive message:Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix, by moving the backup to the end (and removing the old backup)? Fix/Ignore/Cancel?
But using
-s
or-m
switch with parted I am unable to get it to resize the disk and fix the location of the backup GPT. I can’t echo an “F” into the command either:echo "F" > parted /dev/sda resize 2 0% 100% parted /dev/sda resize 2 0% 100% parted -m /dev/sda resize 2 100% parted -s /dev/sda resize 2 100%
And so forth.
gdisk
andsgdisk
don’t seem to be able to resize.gparted
works fine but it’s X of course and not allowed.From the CLI I can squelch the interactive message by running in backticks:
`parted -s /dev/sda rm 2` ( succeeded with error) `parted -s /dev/sda mkpart primary 100%` ( fails because the backup GPT was not moved)
So I need the special “move GPT backup” service offered in the interactive message, and only offered there, but I need to run non-interactively.
-
John P. Fisher about 9 yearsThats a good idea @Dru.
-
John P. Fisher about 9 yearsAlso a good idea @Rod Smith. I'll test these on Monday at work.
-
John P. Fisher about 9 yearsI am going to use this:
sgdisk -e /dev/sda ; sgdisk -d 2 /dev/sda ; sgdisk -N 2 /dev/sda