Subject: Don't interactively ask questions in script mode

From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 15:30:29 +0530

tags 24215 + patch
tags 22167 + patch

Hello,

The attached patches should fix these issues where interactive prompts
are shown when one tries to perform 'rm' or 'resizepart' operations with
mounted partitions.  This avoid workarounds[1].

The proposal is to not ask for confirmation during 'rm' and 'resizepart'
operations when run in script mode.  The answer is assumed to be 'yes'
in both cases.

Links:

1) https://bugs.launchpad.net/ubuntu/+source/parted/+bug/1270203

-- 
Sunil
From 73b874f1c21f89623f0fc13f5ea0484b4bd02639 Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 14:31:43 +0530
Subject: [PATCH 1/6] parted: Don't warn partition busy in script mode

Script mode promises not to ask for input interactively.  However,
during 'rm' and 'resizepart' operations, then a partition is mounted,
parted asks for confirmation on the terminal.  Since upon confirmation
parted allows the actual operations to proceed allow scripts to do
this as well by assuming 'yes' in both the cases.

Fixes the following issues:
 - #22167
 - #24215
 - lpbug #1270203

Signed-off-by: Sunil Mohan Adapa <[email-masked]>
---
 parted/parted.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/parted/parted.c b/parted/parted.c
index 7e6c266..27e3615 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1549,7 +1549,7 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
 
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error;
-        if (!_partition_warn_busy (part))
+        if (!opt_script_mode && !_partition_warn_busy (part))
                 goto error;
 
         start = part->geom.start;
@@ -1597,7 +1597,7 @@ do_rm (PedDevice** dev, PedDisk** diskp)
 
         if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
                 goto error;
-        if (!_partition_warn_busy (part))
+        if (!opt_script_mode && !_partition_warn_busy (part))
                 goto error;
 
         ped_disk_delete_partition (*diskp, part);
-- 
2.11.0

From 416e2fd8b2c4fa11d1d9d3673857ac80e53bc573 Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 14:57:09 +0530
Subject: [PATCH 2/6] tests: t1701-rescue-fs.sh: Wait for device to appear

Wait for device to appear before trying to create a filesystem in a
freshly created partition.

Signed-off-by: Sunil Mohan Adapa <[email-masked]>
---
 tests/t1701-rescue-fs.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tests/t1701-rescue-fs.sh b/tests/t1701-rescue-fs.sh
index 9b726cf..4e1c981 100644
--- a/tests/t1701-rescue-fs.sh
+++ b/tests/t1701-rescue-fs.sh
@@ -30,6 +30,7 @@ scsi_dev=$(cat dev-name)
     || { warn_ "$ME: no ext4 support"; Exit $fail; }
 
 parted -s $scsi_dev mklabel msdos mkpart primary ext2 1m 100%
+wait_for_dev_to_appear_ ${scsi_dev}1 || fail_ ${scsi_dev}1 did not appear
 mkfs.ext4 ${scsi_dev}1 || { warn_ $ME: mkfs.ext4 failed; fail=1; Exit $fail; }
 
 # remove the partition
-- 
2.11.0

From cfb13f6b5a2a6e3fff84f664ce2396e411249b51 Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 15:00:43 +0530
Subject: [PATCH 3/6] tests: t1101-busy-partition.sh: Update tests for script
 operations

* Add tests for 'resizepart' operations.

* Don't expect 'rm' and 'resizepart' operations to fail when run in
  script mode.

Signed-off-by: Sunil Mohan Adapa <[email-masked]>
---
 tests/t1101-busy-partition.sh | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/tests/t1101-busy-partition.sh b/tests/t1101-busy-partition.sh
index ad091f7..7ed480c 100755
--- a/tests/t1101-busy-partition.sh
+++ b/tests/t1101-busy-partition.sh
@@ -24,14 +24,10 @@ require_root_
 require_scsi_debug_module_
 
 # create memory-backed device
-scsi_debug_setup_ dev_size_mb=80 > dev-name ||
+scsi_debug_setup_ dev_size_mb=100 > dev-name ||
   skip_ 'failed to create scsi_debug device'
 dev=$(cat dev-name)
 
-cat <<EOF > exp-error || framework_failure
-Warning: Partition ${dev}2 is being used. Are you sure you want to continue?
-EOF
-
 parted -s "$dev" mklabel msdos > out 2>&1 || fail=1
 
 # expect no output
@@ -40,7 +36,7 @@ compare /dev/null out || fail=1
 parted -s "$dev" mkpart primary fat32 1 40 > out 2>&1 || fail=1
 compare /dev/null out || fail=1
 
-parted -s "$dev" mkpart primary fat32 40 80 > out 2>&1 || fail=1
+parted -s "$dev" mkpart primary fat32 50 90 > out 2>&1 || fail=1
 compare /dev/null out || fail=1
 
 # wait for new partition device to appear
@@ -49,20 +45,44 @@ wait_for_dev_to_appear_ ${dev}2 || fail_ ${dev}2 did not appear
 mkfs.vfat -F 32 ${dev}2 || skip_ mkfs.vfat failed
 
 # be sure to unmount upon interrupt, failure, etc.
-cleanup_fn_() { umount "${dev}2" > /dev/null 2>&1; }
+cleanup_fn_() { umount "$mount_point" > /dev/null 2>&1; }
 
 mount_point=$(pwd)/mnt
 
 mkdir $mount_point || fail=1
 mount "${dev}2" "$mount_point" || fail=1
 
+# Resize of unmounted partition must succeed.
+parted -s "$dev" resizepart 1 50 > out 2>&1 || fail=1
+compare /dev/null out || fail=1
+
 # Removal of unmounted partition must succeed.
 parted -s "$dev" rm 1 > out 2>&1 || fail=1
+compare /dev/null out || fail=1
 
-# Removal of mounted partition must fail.
-parted -s "$dev" rm 2 > out 2>&1 && fail=1
+# Resize of mounted partition must fail in non-script mode.
+parted "$dev" resizepart 2 100 < /dev/null > out 2>&1 && fail=1
+echo -n "Warning: Partition ${dev}2 is being used. Are you sure you want to continue?
+
                                                                          
" \
+     > exp-error || framework_failure
+compare exp-error out || fail=1
 
-# expect error
+# Resize of mounted partition must succeed in script mode.
+parted -s "$dev" resizepart 2 100 > out 2>&1 || fail=1
+compare /dev/null out || fail=1
+
+# Removal of mounted partition must fail in non-script mode.
+parted "$dev" rm 2 < /dev/null > out 2>&1 && fail=1
+echo -n "Warning: Partition ${dev}2 is being used. Are you sure you want to continue?
+
                                                                          
" \
+     > exp-error || framework_failure
+compare exp-error out || fail=1
+
+# Removal of mounted partition must succeed in script mode.
+parted -s "$dev" rm 2 > out 2>&1 || fail=1
+cat <<EOF > exp-error || framework_failure
+Error: Partition(s) 2 on /dev/sdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
+EOF
 compare exp-error out || fail=1
 
 Exit $fail
-- 
2.11.0

From 286ea7b717eeaa49059dc2c8fd35e0157c2eec6a Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 15:05:27 +0530
Subject: [PATCH 4/6] tests: t6002: Fix expectation of 'rm' on mounted
 partitions

Removing a mounted partition only fails in non-script mode.

Signed-off-by: Sunil Mohan Adapa <[email-masked]>
---
 tests/t6002-dm-busy.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/t6002-dm-busy.sh b/tests/t6002-dm-busy.sh
index 723dd30..5dfb9d4 100644
--- a/tests/t6002-dm-busy.sh
+++ b/tests/t6002-dm-busy.sh
@@ -75,7 +75,7 @@ mount "${dev}p2" "$mount_point" || fail=1
 parted -s "$dev" rm 1 > /dev/null 2>&1 || fail=1
 
 # Removal of mounted partition must fail.
-parted -s "$dev" rm 2 > /dev/null 2>&1 && fail=1
+parted "$dev" rm 2 < /dev/null > /dev/null 2>&1 && fail=1
 
 parted -m -s "$dev" u s print > out 2>&1 || fail=1
 sed "s,^$dev,DEV," out > k; mv k out
-- 
2.11.0

From edcc631220c7c8e244820d2651f537ff1f829c7a Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 15:08:27 +0530
Subject: [PATCH 5/6] tests: t9041: Fix expectation of 'rm' on mounted
 partitions

Removing a mounted partition only fails in non-script mode.

Signed-off-by: Sunil Mohan Adapa <[email-masked]>
---
 tests/t9041-undetected-in-use-16th-partition.sh | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tests/t9041-undetected-in-use-16th-partition.sh b/tests/t9041-undetected-in-use-16th-partition.sh
index 673e508..8ba3fe0 100644
--- a/tests/t9041-undetected-in-use-16th-partition.sh
+++ b/tests/t9041-undetected-in-use-16th-partition.sh
@@ -85,10 +85,11 @@ for part_dev in $partitions; do
   mount "$part_dev" "$mount_point" || fail=1
 
   # Removal of mounted partition must fail.
-  parted -s $scsi_dev rm $n > out 2>&1 && fail=1
+  parted $scsi_dev rm $n < /dev/null > out 2>&1 && fail=1
 
-  echo "Warning: Partition $part_dev is being used." \
-          'Are you sure you want to continue?' \
+  echo -n "Warning: Partition $part_dev is being used." \
+       'Are you sure you want to continue?
+
                                                                          
' \
     > exp-error || framework_failure_
 
   # expect error
-- 
2.11.0

From fc25c1b7e2a823236b0a705a1e05ce51a1f72cb8 Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa <[email-masked]>
Date: Mon, 3 Apr 2017 15:10:11 +0530
Subject: [PATCH 6/6] tests: t1102: Fix expectation of 'rm' on mounted
 partitions

Removing a mounted partition only fails in non-script mode.

Signed-off-by: Sunil Mohan Adapa <[email-masked]>
---
 tests/t1102-loop-label.sh | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tests/t1102-loop-label.sh b/tests/t1102-loop-label.sh
index 68b9af4..f9b33ee 100644
--- a/tests/t1102-loop-label.sh
+++ b/tests/t1102-loop-label.sh
@@ -65,9 +65,10 @@ echo "Error: Partition(s) on $dev are being used." > exp
 compare exp out || fail=1
 
 # make sure partition busy check works ( mklabel checks whole disk )
-parted -s "$dev" rm 1 > out 2>&1; test $? = 1 || fail=1
+parted "$dev" rm 1 < /dev/null > out 2>&1; test $? = 1 || fail=1
 # create expected output file
-echo "Warning: Partition ${dev} is being used. Are you sure you want to continue?" > exp
+echo -n "Warning: Partition ${dev} is being used. Are you sure you want to continue?
+
                                                                          
" > exp
 compare exp out || fail=1
 
 umount "$mount_point"
-- 
2.11.0