8

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

6 Answers 6

7

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.

3
  • This worked, thank you - though I put Yes after the partition number
    – nictrix
    Feb 1, 2018 at 20:54
  • 1
    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.
    – Reid
    Nov 27, 2018 at 0:09
  • In my case the original command (before Reid's edit) worked, so I added that as an alternative.
    – Selcuk
    Nov 17, 2021 at 5:19
5

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. ;)

1
  • This is by far the best option. I was already parsing parted print output anyway to compute the end position.
    – Xion345
    Mar 15, 2015 at 20:15
4

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
1
  • 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. Oct 16, 2016 at 1:18
4

Resizing with GNU parted can be done using command below:

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

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.

-1

If you don't already know the end sector, you can do:

parted  /dev/sda 'resizepart 1 yes -0'
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.