How to script automatic task with parted (update gpt table)

8.1k Views Asked by At

I would like to script the extension of a existing partition on my Debian 9.9.

I can do it simply with the interactive mode of parted but i would like to automatize it. In the example below I just need to manually write 'Fix' but i want to script it.

root@localhost:~# parted -l
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/backup: 38.9GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  38.9GB  38.9GB  xfs


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/data: 38.9GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  38.9GB  38.9GB  xfs


Warning: Not all of the space available to /dev/vda appears to be used, you can
fix the GPT to use all of the space (an extra 20971520 blocks) or continue with
the current setting? 
Fix/Ignore?                    

I would like to know which command is executed when I type 'Fix'. From what i understood it may be just a reload of the GPT table, so I have try to execute the command partprobe:

root@localhost:~# partprobe 
Warning: Not all of the space available to /dev/vda appears to be used, you can fix the GPT to use all of the space (an extra 20971520 blocks) or continue with the current setting? 
root@localhost:~# 

But here no options are proposed. I have taken a look in the help of parted and there is a 'script mode', i have tried the following but it didn't work:

root@localhost:~# parted -s /dev/vda print Fix
Warning: Not all of the space available to /dev/vda appears to be used, you can fix the GPT to use all of the space (an extra 20971520 blocks) or continue with the current setting? 
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 118GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      17.4kB  1018kB  1000kB                     bios_grub
 2      1018kB  451MB   450MB   ext3
 3      451MB   10.5GB  10.0GB  xfs
 8      29.6GB  107GB   77.8GB

Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...]
Apply COMMANDs with PARAMETERS to DEVICE.  If no COMMAND(s) are given, run in
interactive mode.

OPTIONs:
  -h, --help                      displays this help message
  -l, --list                      lists partition layout on all block devices
  -m, --machine                   displays machine parseable output
  -s, --script                    never prompts for user intervention
  -v, --version                   displays the version
  -a, --align=[none|cyl|min|opt]  alignment for new partitions

COMMANDs:
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
[...]

I have also tried this bash script:

#!/bin/bash
(echo Fix; echo print list; echo quit) | parted /dev/vda print free

But it did not worked, i have also tried this but it didn't help nether:

**root@localhost:~# cat /tmp/2.sh** 

select /dev/vda
print 
Fix

And then pipe it into parted:

parted < /tmp/2.sh
3

There are 3 best solutions below

0
On

I used this script

        rm -f /tmp/parted_info
        parted --script /dev/$block_device p 2>&1 | tee -a /tmp/parted_info
        # gpt warning
        if grep -Eq 'fix the GPT' /tmp/parted_info;then
            echo -e "OK\nFix\n" | parted ---pretend-input-tty /dev/$block_device print 1>/dev/null
            rm -f /tmp/parted_info
        fi
3
On
printf "fix\n" | parted ---pretend-input-tty /dev/vda print
1
On

I found a solution, use sgdisk instead of parted, it looks much more convenient for scripting. In my case sgdisk /dev/vda -e did the trick