Skip to content

Commit

Permalink
Merge pull request #16 from spectrocloud/add-drivesize-option
Browse files Browse the repository at this point in the history
Add drivesize option
  • Loading branch information
jimmykarily authored Apr 25, 2023
2 parents eff2a49 + 294b704 commit edfded3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
16 changes: 14 additions & 2 deletions pkg/machine/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ type QEMU struct {
func (q *QEMU) Create(ctx context.Context) (context.Context, error) {
log.Info("Create qemu machine")

driveSize := q.driveSize()
drive := q.machineConfig.Drive
if q.machineConfig.AutoDriveSetup && q.machineConfig.Drive == "" {
err := q.CreateDisk(fmt.Sprintf("%s.img", q.machineConfig.ID), "40g")
err := q.CreateDisk(fmt.Sprintf("%s.img", q.machineConfig.ID), driveSize)
if err != nil {
return ctx, fmt.Errorf("creating disk with default size: %w", err)
return ctx, fmt.Errorf("creating disk with size %s: %w", driveSize, err)
}
drive = filepath.Join(q.machineConfig.StateDir, fmt.Sprintf("%s.img", q.machineConfig.ID))
}
Expand Down Expand Up @@ -193,3 +194,14 @@ func (q *QEMU) SendFile(src, dst, permissions string) error {
func (q *QEMU) monitorSockFile() string {
return path.Join(q.machineConfig.StateDir, "qemu-monitor.sock")
}

// Converts the user's drive size (which is Mb as a string) to the qemu format.
// https://qemu.readthedocs.io/en/latest/tools/qemu-img.html#cmdoption-qemu-img-arg-create
func (q *QEMU) driveSize() string {
driveSize := types.DefaultDriveSize
if q.machineConfig.Drive != "" {
driveSize = q.machineConfig.Drive
}

return fmt.Sprintf("%sM", driveSize)
}
15 changes: 15 additions & 0 deletions pkg/machine/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"gopkg.in/yaml.v3"
)

const (
DefaultDriveSize = "30000" // Mb
)

type SSH struct {
User string `yaml:"user,omitempty"`
Port string `yaml:"port,omitempty"`
Expand All @@ -22,6 +26,7 @@ type MachineConfig struct {

DataSource string `yaml:"datasource,omitempty"`
Drive string `yaml:"drive,omitempty"`
DriveSize string `yaml:"drivesize,omitempty"`
AutoDriveSetup bool `yaml:"auto_drive,omitempty"`
ID string `yaml:"id,omitempty"`
Memory string `yaml:"memory,omitempty"`
Expand Down Expand Up @@ -168,6 +173,16 @@ func WithDrive(drive string) MachineOption {
}
}

func WithDriveSize(drivesize string) MachineOption {
return func(mc *MachineConfig) error {
if drivesize != "" {
mc.DriveSize = drivesize
}

return nil
}
}

func FromFile(path string) MachineOption {
return func(mc *MachineConfig) error {
dat, err := ioutil.ReadFile(path)
Expand Down
6 changes: 5 additions & 1 deletion pkg/machine/vbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ func (v *VBox) Create(ctx context.Context) (context.Context, error) {
return ctx, fmt.Errorf("while set VM: %w - %s", err, out)
}

driveSize := types.DefaultDriveSize
if v.machineConfig.Drive != "" {
driveSize = v.machineConfig.Drive
}
drive := v.machineConfig.Drive
if v.machineConfig.AutoDriveSetup && v.machineConfig.Drive == "" {
err := v.CreateDisk(fmt.Sprintf("%s.vdi", v.machineConfig.ID), "30000")
err := v.CreateDisk(fmt.Sprintf("%s.vdi", v.machineConfig.ID), driveSize)
if err != nil {
return ctx, err
}
Expand Down

0 comments on commit edfded3

Please sign in to comment.