9

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
  • 749

8 Answers8

8

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.

Selcuk
  • 103
Jonah
  • 81
  • This worked, thank you - though I put Yes after the partition number – nictrix Feb 01 '18 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 '18 at 00:09
  • In my case the original command (before Reid's edit) worked, so I added that as an alternative. – Selcuk Nov 17 '21 at 05: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. ;)

frostschutz
  • 48,978
  • This is by far the best option. I was already parsing parted print output anyway to compute the end position. – Xion345 Mar 15 '15 at 20:15
5

Resizing with GNU parted can be done using command below:

echo yes | parted /dev/sda ---pretend-input-tty resizepart 2 100GB
FargolK
  • 1,667
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
tjdett
  • 141
  • 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. – David Lechner Oct 16 '16 at 01:18
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.

Benp44
  • 111
  • 2
0

Simple Answer:

printf 'yes\n100%%' | parted /dev/sda resizepart 2 ---pretend-input-tty

Explain:

Let's say we want to run parted /dev/sda resizepart 2 100%

We got:

Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?

We need input yes

So the hack command is

printf 'yes' | parted /dev/sda resizepart 2 100% ---pretend-input-tty

But some versions of parted will ignore resizepart ending location. We need input yes and 100%.

So the most compatible command is

printf 'yes\n100%%' | parted /dev/sda resizepart 2 ---pretend-input-tty

Note that % should write as %%

bin456789
  • 259
0

I've provided a general fully-automatic answer to this question here: Auto expand last partition to use all unallocated space, using parted in batch mode

These behaviors still aren't fixed in parted at this time of writing so in the above answer I've conjured some bash lines which cover re-scanning the block device's size, resizing the GUID Partition Table, resizing the partition and growing the target filesystem partition all automatically without requiring user input.

iPaq
  • 11
-1

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

parted  /dev/sda 'resizepart 1 yes -0'
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Thomas
  • 1
  • 1