2

Since using fdisk in this case is fairly complicated due to the fact that the non-interactive usage is probably not possible or at leasr very complicated (using printf), i want to use parted resizepart for resizing a partition to it's maximum size.

This can be utilized in scenarios like actual disk-resize in you hypervisor / cloud and then you need to adjust your logical volume / pv to the new size (LVM case) or you want to adjust the partition size of a normal partition to it's maximum.

Let's assume i want to resize partition /dev/sda1 on obvisouly disk /dev/sda1 to its maximum possible size - how would i do this without getting asked any questions at all.

Eventhough parted /dev/sda resizepart 1 exists, it needs me to calculate and enter the maximum disk size - and that is the actual clue I working on here

1
  • as a side note, the whole story is debian`s / partman lack of being able to use a disk rather then a partition for a PV while doing a preseed, like github.com/EugenMayer/packer-vagrant/blob/master/resources/http/… .. so you end up having a PV over a partition which makes resizing it according to the new hypervisor disk size a lot more effort. Thats where this journey actually started – Eugen Mayer Aug 26 '17 at 17:49
3

HINT: this script is compatible with parted v3+ OOTB, if you have parted 2, you need to change parted resizepart to parted resize

lets put this into a script, the acualy command is a onliner, we just add a lot more to ensure the first 2 parameters are set:

#!/bin/bash
set -e

if [[ $# -eq 0 ]] ; then
    echo 'please tell me the device to resize as the first parameter, like /dev/sda'
    exit 1
fi


if [[ $# -eq 1 ]] ; then
    echo 'please tell me the partition number to resize as the second parameter, like 1 in case you mean /dev/sda1 or 4, if you mean /dev/sda2'
    exit 1
fi

DEVICE=$1
PARTNR=$2
APPLY=$3

fdisk -l $DEVICE$PARTNR >> /dev/null 2>&1 || (echo "could not find device $DEVICE$PARTNR - please check the name" && exit 1)

CURRENTSIZEB=`fdisk -l $DEVICE$PARTNR | grep "Disk $DEVICE$PARTNR" | cut -d' ' -f5`
CURRENTSIZE=`expr $CURRENTSIZEB / 1024 / 1024`
# So get the disk-informations of our device in question printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda we use printf %s\\n 'unit MB print list' to ensure the units are displayed as MB, since otherwise it will vary by disk size ( MB, G, T ) and there is no better way to do this with parted 3 or 4 yet
# then use the 3rd column of the output (disk size) cut -d' ' -f3 (divided by space)
# and finally cut off the unit 'MB' with blanc using tr -d MB
MAXSIZEMB=`printf %s\\n 'unit MB print list' | parted | grep "Disk ${DEVICE}" | cut -d' ' -f3 | tr -d MB`

echo "[ok] would/will resize to from ${CURRENTSIZE}MB to ${MAXSIZEMB}MB "

if [[ "$APPLY" == "apply" ]] ; then
  echo "[ok] applying resize operation.."
  parted ${DEVICE} resizepart ${PARTNR} ${MAXSIZEMB}
  echo "[done]"
else
  echo "[WARNING]!: Sandbox mode, i did not size!. Use 'apply' as the 3d parameter to apply the changes"
fi

usage

Save the script above as resize.sh and make it executable

# resize the fourth partition to the maximum size, so /dev/sda4
# this is no the sandbox mode, so no changes are done
./resize.sh /dev/sda 4

# apply those changes
./resize.sh /dev/sda 4 apply

For example, in case you have a vg vgdata with a lv 'data' on /dev/sdb1 while using LVM, the whole story would look like

./resize.sh /dev/sdb 1 apply
pvresize /dev/sdb1
lvextend -r /dev/mapper/vgdata-data -l 100%FREE

Thats it, resized logical volume including resized filesystem ( -r ) - all done, check it with df -h :)

Explanation

What we use to find the disk size is

resizepart ${PARTNR} `parted -l | grep ${DEVICE} | cut -d' ' -f3 | tr -d MB

a) So get the disk-informations of our device in question printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda we use printf %s\\n 'unit MB print list' to ensure the units are displayed as MB, since otherwise it will vary by disk size ( MB, G, T ) and there is no better way to do this with parted 3 or 4 yet

b) then use the 3rd column of the output (disk size) cut -d' ' -f3 (divided by space)

c) and finally cut off the unit 'MB' with blanc using tr -d MB

Follow ups

I published the script on https://github.com/EugenMayer/parted-auto-resize so if anything is to improved feature wise, use pull requests there ( anything not in scope of this question )

6
  • I bet you never tested it on anything else but /dev/sda - you (accidentally) left it hard-coded in one place in the script. – Andrew Savinykh Oct 16 '18 at 2:21
  • Also, great work! – Andrew Savinykh Oct 16 '18 at 2:23
  • I am working on the script on guthub, not here. did you try that one? – Eugen Mayer Oct 16 '18 at 6:28
  • Yeah, after I wrote the comment. I think there are a few issues with how it calculates sizes, at least on Ubuntu 18 where I tried it. I don't have time to make it generic so gist.github.com/andrewsav-datacom/… – Andrew Savinykh Oct 16 '18 at 6:39
  • Feel free to contribute it anytime – Eugen Mayer Oct 16 '18 at 6:53
1

It seems that different versions of parted behave quite differently, so this is applicable to the parted version 3.*, that comes at least with CentOS7 and CentOS8.

  1. Make sure that OS is aware of the disk size increase since the last scan(reboot):
# echo 1 > /sys/block/sda/device/rescan
  1. Check that there is free space available after the last partition:
# parted -s -a opt /dev/sda "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  32.2GB  31.1GB  primary               lvm
        32.2GB  53.7GB  21.5GB           Free Space
  1. Resize last partition to the maximum available disk space:
# parted -s -a opt /dev/sda "resizepart 2 100%"
# echo $?
0

This operation doesn't have any indication of it's success except the exit code.

  1. You can verify that partition size was increased by repeating:
# parted -s -a opt /dev/sda "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm

As a bonus you can fit all this into one command line(don't miss step 0):

# parted -s -a opt /dev/sda "print free" "resizepart 2 100%" "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  32.2GB  31.1GB  primary               lvm
        32.2GB  53.7GB  21.5GB           Free Space

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.