Skip to content

Commit

Permalink
Reload partitions after growing them
Browse files Browse the repository at this point in the history
Othersise os level tools may not be aware of size changes and dont run
as expected

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Oct 16, 2023
1 parent 0c7838c commit b21b01a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
27 changes: 24 additions & 3 deletions pkg/plugins/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,29 @@ func (dev *Disk) ExpandLastPartition(l logger.Interface, size uint, console Cons
}

part := dev.Parts[len(dev.Parts)-1]
l.Debugf("Expanding partition %d up to %d sectors", part.Number, size)

// Grow partition
out, err := console.Run(fmt.Sprintf("growpart %s %d", dev.Device, part.Number))
if err != nil {
l.Errorf("Failed growing partition: %s\n%s", out, err)
return out, err
}
l.Debugf("Output of growpart: %s", out)

// Expand FS
fullDevice, err := dev.findFullPartName(console, part.Number)
if err != nil {
return fullDevice, err
}

out, err = dev.expandFilesystem(fullDevice, console)
// Reload partition table info so the os is aware of size changes
err = dev.ReloadPartitionTable(l, console)
if err != nil {
return "", err
}

out, err = dev.expandFilesystem(fullDevice, console, l)
if err != nil {
return out, err
}
Expand Down Expand Up @@ -497,23 +506,35 @@ func (dev Disk) findFullPartName(console Console, partNum int) (string, error) {
return "", errors.New("no partition found")
}

func (dev Disk) expandFilesystem(device string, console Console) (string, error) {
func (dev Disk) expandFilesystem(device string, console Console, l logger.Interface) (string, error) {
var out string
var err error

fs, _ := console.Run(fmt.Sprintf("blkid %s -s TYPE -o value", device))
l.Debugf("Trying to resize filesystem for device %s", device)

fs, err := console.Run(fmt.Sprintf("blkid %s -s TYPE -o value", device))
if err != nil {
l.Errorf("error getting filesystem type: %s", err.Error())
return out, err
}

l.Debugf("Found filesystem %s for device %s", fs, device)

switch strings.TrimSpace(fs) {
case "ext2", "ext3", "ext4":
out, err = console.Run(fmt.Sprintf("e2fsck -fy %s", device))
if err != nil {
l.Errorf("error running e2fsck: %s", err.Error())
return out, err
}
l.Debugf("Output from running e2fsck %s", out)
out, err = console.Run(fmt.Sprintf("resize2fs %s", device))

if err != nil {
l.Errorf("error running resize2fs: %s", err.Error())
return out, err
}
l.Debugf("Output from running resize2fs %s", out)
case "xfs":
// to grow an xfs fs it needs to be mounted :/
tmpDir, err := os.MkdirTemp("", "yip")
Expand Down
12 changes: 11 additions & 1 deletion pkg/plugins/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ var CmdsAddPartByDevPath []console.CmdMock = []console.CmdMock{
{Cmd: "sgdisk -P -n=5:0:+2097152 -t=5:8300 /some/device"},
{Cmd: "sgdisk -n=5:0:+2097152 -t=5:8300 /some/device"}, pTable,
{Cmd: "udevadm settle"},
{Cmd: "partprobe /some/device"}, sync,
{Cmd: "partprobe /some/device"},
sync,
{Cmd: "udevadm settle"},
lsblkTypes,
{Cmd: "mkfs.ext2 -L MYLABEL /some/device5"},
Expand Down Expand Up @@ -135,6 +136,9 @@ var CmdsExpandPart []console.CmdMock = []console.CmdMock{
{Cmd: "sgdisk -e /some/device"}, pTable,
{Cmd: "growpart /some/device 4"},
{Cmd: "lsblk -ltnpo name /some/device", Output: "/some/device3\n/some/device4"},
{Cmd: "udevadm settle"},
{Cmd: "partprobe /some/device"},
sync,
{Cmd: "blkid /some/device4 -s TYPE -o value", Output: "ext4"},
{Cmd: "e2fsck -fy /some/device4"},
{Cmd: "resize2fs /some/device4"}, pTable,
Expand Down Expand Up @@ -163,6 +167,9 @@ var CmdsAddAndExpandPart []console.CmdMock = []console.CmdMock{
{Cmd: "mkfs.ext2 -L MYLABEL /some/device1"},
{Cmd: "growpart /some/device 1"},
{Cmd: "lsblk -ltnpo name /some/device", Output: "/some/device1\n/some/device4"},
{Cmd: "udevadm settle"},
{Cmd: "partprobe /some/device"},
sync,
{Cmd: "blkid /some/device1 -s TYPE -o value", Output: "ext2"},
{Cmd: "e2fsck -fy /some/device1"},
{Cmd: "resize2fs /some/device1"},
Expand All @@ -181,6 +188,9 @@ var CmdsExpandPartXfs []console.CmdMock = []console.CmdMock{
{Cmd: "sgdisk -e /some/device"}, pTable,
{Cmd: "growpart /some/device 4"},
{Cmd: "lsblk -ltnpo name /some/device", Output: "/some/device3\n/some/device4"},
{Cmd: "udevadm settle"},
{Cmd: "partprobe /some/device"},
sync,
{Cmd: "blkid /some/device4 -s TYPE -o value", Output: "xfs"},
{Cmd: "mount -t xfs /some/device4 /tmp/*", UseRegexp: true},
{Cmd: "xfs_growfs /tmp/*", UseRegexp: true},
Expand Down

0 comments on commit b21b01a

Please sign in to comment.