GNU parted resizepart in script

9,357

Solution 1

If resizepart does not work, you might have to resort to rm and mkpart to achieve the same thing.

Of course, this would require you to parse the partition table first in order to determine partition type and start offset. Unless you already know the necessary values. After all you had to get the 166016512B from somewhere too.

parted has the --machine option to produce easily parseable output. On the other hand, examples of actually parsing it are not easily found. ;)

Solution 2

In my case, I was using Parted 3.2 from within a tty-less SSH session. The "Yes" command hack didn't work, as the parted code has the following test:

    /* script-mode: don't handle the exception */
    if (opt_script_mode || (!isatty (0) && !pretend_input_tty))
            return PED_EXCEPTION_UNHANDLED;

Note that 'isatty' test, which will fail. The 'pretend_input_tty' is an undocumented command line option, which can be switched on via ---pretend-input-tty.

So, if you want to use parted from a script, my answer is the following:

/sbin/parted -a optimal /dev/loop1 ---pretend-input-tty resizepart 4 Yes 522239s

If that doesn't work, try moving Yes to the end:

/sbin/parted -a optimal /dev/loop1 ---pretend-input-tty resizepart 4 522239s Yes

Note the three dashes in front of pretend-input-tty. I think this is supposed to scare us away. Not sure though.

Solution 3

This bug is noted here: https://bugs.launchpad.net/ubuntu/+source/parted/+bug/1270203

As noted in the thread, there are two work arounds. The simplest is to simply append "Yes" to the command list:

parted --script /dev/sda unit B resizepart 2 1166016512B Yes

Solution 4

Resizing with GNU parted can be done using command below:

echo yes | parted /dev/sda ---pretend-input-tty resizepart 2 100GB

Solution 5

This script is a little brittle to variations in parted versions, but works for me and is particularly useful if rebuild the partition table as suggested by @frostschutz is complicated by resizing extended/multiple partitions:

Script

#!/bin/bash

echo "Resizing partition ${2} on ${1} with new end ${3}"

parted "${1}" ---pretend-input-tty <<EOF
resizepart
${2}
${3}
Yes
quit
EOF

echo "Done"

Usage example:

sudo ./resize_partition.sh /dev/loop0 2 15757970s

Note I'm using a partition end point specified in sectors, hence the s.

Share:
9,357

Related videos on Youtube

Xion345
Author by

Xion345

PhD Student in Computer Science

Updated on September 18, 2022

Comments

  • Xion345
    Xion345 over 1 year

    I would like to used the yes command so that GNU parted does not wait for user input :

    root@195-xxx-xxx-xxx:/proc# parted /dev/sda unit B resizepart 2 1166016512B
     Warning: Shrinking a partition can cause data loss, are you sure you want 
     to continue?
    Yes/No? y                                                                 
    Information: You may need to update /etc/fstab.
    root@195-xxx-xxx-xxx:/proc# echo $?
    0
    

    However using yes does not work here :

    root@195-xxx-xxx-xxx:/proc# yes | parted /dev/sda unit B resizepart 2 166016512B
     Warning: Shrinking a partition can cause data loss, are you sure you 
     want to continue?
    root@195-xxx-xxx-xxx:/proc# echo $?
    1
    

    Edit:

    The --script option does not work as well :

    root@195-xxx-xxx-xxx:/proc# parted --script /dev/sda unit B resizepart 2 1166016512B
     Warning: Shrinking a partition can cause data loss, are you sure you 
     want to continue?
    root@195-xxx-xxx-xxx:/proc# echo $?
    1
    
  • Xion345
    Xion345 about 9 years
    This is by far the best option. I was already parsing parted print output anyway to compute the end position.
  • David Lechner
    David Lechner over 7 years
    The command given does not work (in Debian Jessie). Correct command is parted /dev/sda unit B resizepart 2 Yes 1166016512B. This is also what is suggested in the bug report.
  • nictrix
    nictrix over 6 years
    This worked, thank you - though I put Yes after the partition number
  • Reid
    Reid over 5 years
    The original answer had yes after the size. Like @nictrix, this did not work for me. I proposed an edit to put it after the partition number, which did.
  • Selcuk
    Selcuk over 2 years
    In my case the original command (before Reid's edit) worked, so I added that as an alternative.