how do I get over parted confirmation request in a script

4.7k Views Asked by At

I wonder if anybody has done this sort of stuff:

I need to use parted in a script doing a bit of a dodgy operation. I am trying to modify an extended partition that contains the partition I have been booted on so I am getting a confirmation request that leads to an error exit in script mode.

When I do this manually it works.

So I tried to pipe my input into parted to get around this with the following script:

#!/usr/bin/env bash
parted /dev/sda << EOT
resizepart
4
y
33593343s
Yes
I
EOT

This does not work unfortunately. I get

root@19912ac:/tmp# ./test.sh 
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) resizepart                                                       
Partition number? 4                                                       
Warning: Partition /dev/sda4 is being used. Are you sure you want to continue?
(parted) y                                                              
  align-check TYPE N                     
help [COMMAND]                           print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END     make a partition
name NUMBER NAME                         name partition NUMBER as NAME
print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
quit                                     exit program
rescue START END                         rescue a lost partition near START and END
resizepart NUMBER END                    resize partition NUMBER
rm NUMBER                                delete partition NUMBER
...

It looks like all goes well until I am asked to confirm the modification of /dev/sda4. My next input (y) shows up on the command line but after that I only get help infos.

Anybody tried this before ?

2

There are 2 best solutions below

1
On

another way without EOF is exist:

echo -e "resizepart\n1\nYes\n100%\nprint free\nquit" | sudo parted /dev/vda ---pretend-input-tty
1
On

There's a hidden flag for parted ---pretend-input-tty (I guess the --- is to indicate that it's for advanced usage or something!). I've found that using this in a script similar to yours works:

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"