Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix part name construction for devices like mmcblk0p1 and nvme0n1p1 #110

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions pkg/plugins/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (dev *Disk) ExpandLastPartition(l logger.Interface, size uint, console Cons
}
freeS := dev.computeFreeSpaceWithoutLast()
if size > freeS {
return "", errors.New(fmt.Sprintf("Not enough free space for to expand last partition up to %d sectors", size))
return "", fmt.Errorf("Not enough free space for to expand last partition up to %d sectors", size)
}
}

Expand All @@ -461,7 +461,11 @@ func (dev *Disk) ExpandLastPartition(l logger.Interface, size uint, console Cons
}

// Expand FS
fullDevice := fmt.Sprintf("%s%d", dev.Device, part.Number)
fullDevice, err := dev.findFullPartName(console, part.Number)
if err != nil {
return fullDevice, err
}

out, err = dev.expandFilesystem(fullDevice, console)
if err != nil {
return out, err
Expand All @@ -475,6 +479,24 @@ func (dev *Disk) ExpandLastPartition(l logger.Interface, size uint, console Cons
return out, nil
}

func (dev Disk) findFullPartName(console Console, partNum int) (string, error) {
allParts, err := console.Run(fmt.Sprintf("lsblk -ltnpo name %s", dev.Device))
if err != nil {
return allParts, fmt.Errorf("listing partitions %w", err)
}

for _, part := range strings.Split(allParts, "\n") {
matches, err := regexp.MatchString(fmt.Sprintf("%s.*%d", dev.Device, partNum), part)
if err != nil {
return "", err
}
if matches {
return part, nil
}
}
return "", errors.New("no partition found")
}

func (dev Disk) expandFilesystem(device string, console Console) (string, error) {
var out string
var err error
Expand Down
3 changes: 3 additions & 0 deletions pkg/plugins/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var CmdsExpandPart []console.CmdMock = []console.CmdMock{
{Cmd: "sgdisk -P -e /some/device"},
{Cmd: "sgdisk -e /some/device"}, pTable,
{Cmd: "growpart /some/device 4"},
{Cmd: "lsblk -ltnpo name /some/device", Output: "/some/device3\n/some/device4"},
{Cmd: "blkid /some/device4 -s TYPE -o value", Output: "ext4"},
{Cmd: "e2fsck -fy /some/device4"},
{Cmd: "resize2fs /some/device4"}, pTable,
Expand Down Expand Up @@ -161,6 +162,7 @@ var CmdsAddAndExpandPart []console.CmdMock = []console.CmdMock{
/some/device1 part`},
{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: "blkid /some/device1 -s TYPE -o value", Output: "ext2"},
{Cmd: "e2fsck -fy /some/device1"},
{Cmd: "resize2fs /some/device1"},
Expand All @@ -178,6 +180,7 @@ var CmdsExpandPartXfs []console.CmdMock = []console.CmdMock{
{Cmd: "sgdisk -P -e /some/device"},
{Cmd: "sgdisk -e /some/device"}, pTable,
{Cmd: "growpart /some/device 4"},
{Cmd: "lsblk -ltnpo name /some/device", Output: "/some/device3\n/some/device4"},
{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