6

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
sizeur
  • 113
  • 1
  • 10

3 Answers3

5
printf "fix\n" | parted ---pretend-input-tty /dev/vda print
lisaac
  • 51
  • 1
  • 2
  • 2
    Hey lisaac, welcome to stackoverflow. Even if your answer works, would you care to elaborate what it is doing and why this would help? – Stefan M Apr 01 '20 at 08:10
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/25744970) – double-beep Apr 01 '20 at 10:37
  • Gotta love undocumented options. The documented `--script` makes `parted` answer `no` to all. This lets you actually override safety checks like you'd expect from a script. – Perkins Feb 17 '21 at 21:33
3

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

sizeur
  • 113
  • 1
  • 10
0

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
张馆长
  • 1,321
  • 10
  • 11