Skip to content

Commit

Permalink
[wip] run: Add --expand-root
Browse files Browse the repository at this point in the history
I used this to test out CentOS/centos-bootc#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)
  • Loading branch information
cgwalters committed Mar 4, 2024
1 parent 3fd30be commit 1a1ff70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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")

Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 20 additions & 2 deletions pkg/vm/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down

0 comments on commit 1a1ff70

Please sign in to comment.