8

I've googled this, and almost all I could find is to answer a bash script and this doesn't work for me. The answers I found told to do:

echo "yes" | ./script

or

./script <<< yes

or something like that. It didn't work for me. My guess is that's because what I want to do is to auto answer an executable program, not a script. More specifically, I want to auto answer parted. Here

parted -a optimal /dev/sda mklabel msdos
Warning: The existing disk label on /dev/sda will be destroyed and
all data on this disk will be lost. Do you want to continue?
Yes/No? _

I try to do

echo "yes" | parted -a optimal /dev/sda mklabel msdos

and

parted -a optimal /dev/sda mklabel msdos <<< yes

Both methods didn't work. Those didn't answer yes to parted prompt.

So, how can I automatically answer that parted prompt without using a bash script?

muru
  • 207,970
Mas Bagol
  • 1,399

2 Answers2

11

With parted, you can just add the -s option:

parted -a optimal -s /dev/sda mklabel msdos

From the Trusty man page for parted:

[...]
       -s, --script
              never prompts for user intervention
[...]
kos
  • 41,378
  • never prompts ... does it means, yes to all? – Mas Bagol Apr 25 '15 at 11:40
  • 3
    @BagolDaplun Never ask in this case means that the default is used. – A.B. Apr 25 '15 at 12:09
  • @BagolDaplun As A.B. said -s makes parted use the default (which is yes). Also I corrected the command because here you can't combine options since -a needs an argument. – kos Apr 25 '15 at 17:09
  • 1
    As of Parted 3.* this advice is no longer true. For instance if the aim is to shrink a partition by running Parted 3.* in script-mode it will prompt for user intervention and then exit. This is intentional according to a Parted developer. – FlexMcMurphy Jul 21 '24 at 21:13
  • @FlexMcMurphy I have tested this specifically with mklabel (sudo parted -s /dev/sdb mklabel gpt); that still works. However yes, it doesn't work with resizepart. Weird how one destructive action would default to "I'll just go with it" and another destructive action would default to "nope, not gonna do it". However it could've been like so even back then. I guess expect it is in that case, too bad nobody provided a way to do shrink a partition using expect. – kos Aug 04 '24 at 03:55
0

As of Parted 3.* the accepted answer is no longer true.

Parted is primarily designed to be used in interactive mode where a human will provide information to parted and answer its questions in real-time.

There is also an option to run parted in script-mode parted -s where commands can be fed to parted all in one go without having to interact step by step with parted.

As of Parted 3.* if the aim is to shrink a partition in script-mode parted will prompt for user intervention and then exit. This is intentional, for safety purposes, according to feedback in this bug report which also provides a patch that works around that limitation.

Parted has an undocumented feature ---pretend-input-tty that behaves like script-mode as it also allows for parameters to be fed to it, all together, except it runs parted in interactive mode.

In this code snippet a disk with one partition is mounted at /dev/loop0 and parted will increase and shrink the size of a partition without objection:

echo -e "resizepart 1 46280703s\nyes\nunit s\nprint\nquit" | sudo parted /dev/loop0 ---pretend-input-tty

The expect command could also be used to automate providing information and answers to parted when it is running in interactive mode. This script creates a 100MiB disk image file, creates one partition and formats it as ext4.

#!/bin/bash
#
# Script to make a disk image file
#

Create a 100MiB disk file, when partitioned it will have 204800 sectors

cd ~ dd if=/dev/zero of=disk.img bs=1M count=100

loopdevice=$(losetup --show --find --partscan disk.img)

echo $loopdevice echo $loopdevice"p1"

parted -s $loopdevice mklabel gpt parted -s $loopdevice mkpart primary ext4 2048s 100% mkfs -t ext4 $loopdevice"p1"

Now running this next script will call parted and use expect to feed in answers to increase or decrease that partition without problem:

#!/usr/bin/expect
#
# Script that uses the 'expect' command to feed answers to 'parted' command
# when it is running in interactive mode.
#

spawn sudo parted disk.img expect "(parted)" {send "p\n"} expect "(parted)" {send "resizepart 1 150000s\n"} expect "Yes/No?" {send "y\n"} expect "(parted)" {send "p\n"} expect "(parted)" {send "q\n"}