From 1a1ff7097e01f8004e6398c5af0fcf03710238e7 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 4 Mar 2024 17:44:50 -0500 Subject: [PATCH] [wip] run: Add --expand-root I used this to test out https://github.com/CentOS/centos-bootc/issues/394 But right now this leaks the disk; to fix that we need to use `-add-fd` instead. (This all conflicts with the libvirt stuff so putting it on the backburner) --- cmd/run.go | 5 ++++- pkg/vm/run.go | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index c50b82ac..5a0f3ffe 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -17,6 +17,7 @@ type osVmConfig struct { CloudInitDir string KsFile string Background bool + ExpandRoot uint RemoveVm bool // Kill the running VM when it exits RemoveDiskImage bool // After exit of the VM, remove the disk image } @@ -41,6 +42,8 @@ func init() { runCmd.Flags().StringVar(&vmConfig.CloudInitDir, "cloudinit", "", "--cloudinit [[transport:]cloud-init data directory] (transport: cdrom | imds)") + runCmd.Flags().UintVar(&vmConfig.ExpandRoot, "expand-root", 0, "Add additional space to block device (size in GiB)") + runCmd.Flags().BoolVarP(&vmConfig.Background, "background", "B", false, "Do not spawn SSH, run in background") runCmd.Flags().BoolVar(&vmConfig.RemoveVm, "rm", false, "Kill the running VM when it exits, requires --interactive") @@ -75,7 +78,7 @@ func doRun(flags *cobra.Command, args []string) error { return fmt.Errorf("ssh getFreeTcpPort: %w", err) } - err = vm.Run(vmDir, sshPort, vmConfig.User, config.MachineSshKeyPriv, ciData, ciPort) + err = vm.Run(vmDir, sshPort, vmConfig.User, config.MachineSshKeyPriv, ciData, ciPort, vmConfig.ExpandRoot) if err != nil { return fmt.Errorf("runBootcVM: %w", err) } diff --git a/pkg/vm/run.go b/pkg/vm/run.go index 0d9e57e4..9e926fb2 100644 --- a/pkg/vm/run.go +++ b/pkg/vm/run.go @@ -36,7 +36,7 @@ func createQemuCommand() *exec.Cmd { return exec.Command(path, args...) } -func Run(vmDir string, sshPort int, user, sshIdentity string, ciData bool, ciPort int) error { +func Run(vmDir string, sshPort int, user, sshIdentity string, ciData bool, ciPort int, expandRoot uint) error { var args []string args = append(args, "-cpu", "host") args = append(args, "-m", "2G") @@ -49,8 +49,26 @@ func Run(vmDir string, sshPort int, user, sshIdentity string, ciData bool, ciPor vmPidFile := filepath.Join(vmDir, "run.pid") args = append(args, "-pidfile", vmPidFile) + tempf, err := os.CreateTemp(vmDir, "tmpdisk") + if err != nil { + return err + } + diskPath := tempf.Name() + logrus.Debugf("Copying to %s", diskPath) + if err := tempf.Close(); err != nil { + return fmt.Errorf("failed to close: %w", err) + } vmDiskImage := filepath.Join(vmDir, config.DiskImage) - driveCmd := fmt.Sprintf("if=virtio,format=raw,file=%s", vmDiskImage) + if err := exec.Command("cp", "--reflink=always", vmDiskImage, diskPath).Run(); err != nil { + return fmt.Errorf("failed to copy %s %s: %w", vmDiskImage, diskPath, err) + } + if expandRoot > 0 { + if err := exec.Command("truncate", "-s", fmt.Sprintf("%dG", expandRoot), diskPath).Run(); err != nil { + return fmt.Errorf("failed to expand disk: %w", err) + } + } + + driveCmd := fmt.Sprintf("if=virtio,format=raw,file=%s", diskPath) args = append(args, "-drive", driveCmd) if ciData { if ciPort != -1 {