Sample Header Ad - 728x90

Parted in scripted mode can increase but not decrease partition size

3 votes
1 answer
197 views
I am trying to use parted in scripted mode to resize a partition. When I use the command to increase the partition size it works but when I use the same command but simply change the sector size to reduce the partition size it does not work. If however I enter those details in interactive mode everything works fine whether increasing or decreasing the size of the partition. I test with a .img file with one partition containing a btrfs file system. I loop mount the .img file to /dev/loop and partition 1 is at /dev/loop0p1. Partition 1 has an end sector at 48878656. In the tests that follow I never try to increase the size of the partition beyond the maximum space available on the .img "disk" or below the size of the file system the partition contains. Here I use parted in script mode to give information about the partition: [flex@thinkpad ncp-pi]$ sudo parted -s -a opt /dev/loop0 "unit s print" Model: Loopback device (loopback) Disk /dev/loop0: 120933888s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 48878656s 48876609s btrfs I use parted in scripted mode to INCREASE the size of the partition by increasing the position of the end sector from sector 48878656 to sector 49078656 and this works as expected: [flex@thinkpad ncp-pi]$ sudo parted -s -a opt /dev/loop0 "unit s print resizepart 1 49078656 print" Model: Loopback device (loopback) Disk /dev/loop0: 120933888s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 48878656s 48876609s btrfs Model: Loopback device (loopback) Disk /dev/loop0: 120933888s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 49078656s 49076609s btrfs Now using the same command I try to reduce the size of the partition by reducing the position of the end sector from sector 49078656 back to sector 48878656 however this does not work, the script wants me to answer 'Yes/No' even though I am in scripted mode? [flex@thinkpad ncp-pi]$ sudo parted -s -a opt /dev/loop0 "unit s print resizepart 1 48878656 print" Model: Loopback device (loopback) Disk /dev/loop0: 120933888s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 49078656s 49076609s btrfs Warning: Shrinking a partition can cause data loss, are you sure you want to continue? [flex@thinkpad ncp-pi]$ I check the partition size again and nothing has changed, so that last command did not complete: [flex@thinkpad ncp-pi]$ sudo parted -s -a opt /dev/loop0 "unit s print" Model: Loopback device (loopback) Disk /dev/loop0: 120933888s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 49078656s 49076609s btrfs If instead I enter those details in interactive mode everything works fine. There are some other questions on stackoverflow and other sites about parted in scripted mode. Functionality differs a little across distros and there may be a bug in it. Other questions about parted in scripted mode: - How to use parted in script mode? - GNU parted resizepart in script - no way to use resizepart non-interactively on busy partition (Bug Report) None of these sites have answered my question. How come my command to use parted in scripted mode can increase the size of the partition but not decrease it? How can I debug or find out what's going wrong?
Asked by FlexMcMurphy (433 rep)
Jun 24, 2024, 08:46 PM
Last activity: Jul 21, 2024, 10:37 PM