Skip to content

Commit

Permalink
Initial swap partition support (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdesaintleger authored Oct 4, 2024
1 parent 0558c50 commit 8db2dfb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/plugins/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ func (dev Disk) expandFilesystem(device string, console Console, l logger.Interf
if err != nil {
return out, err
}
case "swap":
return "", errors.New(fmt.Sprintf("swap resizing is not supported (device: %s)", device))
default:
return "", errors.New(fmt.Sprintf("Could not find filesystem for %s, not resizing the filesystem", device))
}
Expand Down Expand Up @@ -819,7 +821,7 @@ func (gd *GdiskCall) ExpandPTable() {
func (mkfs MkfsCall) buildOptions() ([]string, error) {
opts := []string{}

linuxFS, _ := regexp.MatchString("ext[2-4]|xfs|btrfs", mkfs.part.FileSystem)
linuxFS, _ := regexp.MatchString("ext[2-4]|xfs|btrfs|swap", mkfs.part.FileSystem)
fatFS, _ := regexp.MatchString("fat|vfat", mkfs.part.FileSystem)

switch {
Expand Down Expand Up @@ -852,7 +854,15 @@ func (mkfs MkfsCall) Apply(console Console) (string, error) {
if err != nil {
return "", err
}
tool := fmt.Sprintf("mkfs.%s", mkfs.part.FileSystem)

var tool string

if (mkfs.part.FileSystem == "swap") {
tool = "mkswap"
} else {
tool = fmt.Sprintf("mkfs.%s", mkfs.part.FileSystem)
}

command := fmt.Sprintf("%s %s", tool, strings.Join(opts[:], " "))
return console.Run(command)
}
Expand Down
49 changes: 49 additions & 0 deletions pkg/plugins/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,36 @@ var CmdsAddAndExpandPart []console.CmdMock = []console.CmdMock{
sync,
}

var CmdsAddAndExpandSwapPart []console.CmdMock = []console.CmdMock{
{Cmd: "lsblk -npo type /some/device", Output: "disk"},
{Cmd: "sgdisk --verify /some/device", Output: "the end of the disk"},
{Cmd: "sgdisk -P -e /some/device"},
{Cmd: "sgdisk -e /some/device"},
pTableEmpty,
{Cmd: "udevadm settle"},
{Cmd: "blkid -l --match-token LABEL=MYLABEL -o device"},
{Cmd: "sgdisk -P -n=1:2048:+2097152 -t=1:8300 /some/device"},
{Cmd: "sgdisk -n=1:2048:+2097152 -t=1:8300 /some/device"},
pTablePostCreation,
{Cmd: "udevadm settle"},
{Cmd: "partprobe /some/device"},
sync,
{Cmd: "udevadm settle"},
{Cmd: "lsblk -ltnpo name,type /some/device", Output: `/some/device disk
/some/device1 part`},
{Cmd: "mkswap -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: "swap"},
pTablePostExpansion,
{Cmd: "udevadm settle"},
{Cmd: "partprobe /some/device"},
sync,
}

var CmdsExpandPartXfs []console.CmdMock = []console.CmdMock{
{Cmd: "udevadm settle"},
{Cmd: "blkid -l --match-token LABEL=reflabel -o device", Output: "/some/part"},
Expand Down Expand Up @@ -367,5 +397,24 @@ var _ = Describe("Layout", func() {
Expect(err).ToNot(HaveOccurred())
}
})

It("Adds a swap partition and fails expanding it", func() {
testConsole := console.New()
testConsole.AddCmds(CmdsAddAndExpandSwapPart)

buf := new(strings.Builder)
l.SetOutput(buf)

err := Layout(l, schema.Stage{
Layout: schema.Layout{
Device: &schema.Device{Path: "/some/device"},
Parts: []schema.Partition{{FSLabel: "MYLABEL", Size: 1024, FileSystem: "swap"}},
Expand: &schema.Expand{Size: 3072},
},
}, fs, testConsole)

Expect(buf.String()).ToNot(MatchRegexp("level=warning"))
Expect(err.Error()).To(ContainSubstring("swap resizing is not supported"))
})
})
})

0 comments on commit 8db2dfb

Please sign in to comment.