Skip to content

Add option to add a data disk to an instance #759

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions docs/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cloud-init:
disk:
- `basedisk`: the base image
- `diffdisk`: the diff image (QCOW2)
- `datadisk`: the data image (QCOW2), optional

QEMU:
- `qemu.pid`: QEMU PID
Expand Down
4 changes: 4 additions & 0 deletions examples/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ memory: null
# 🟢 Builtin default: "100GiB"
disk: null

# Data size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs additional documentation to explain that this is a supplemental data volume, and the parameter is the size, and can use size-related units like "GiB". Maybe the builtin default should change to "0GiB" to make this obvious?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The datadisk also needs to be mentioned in docs/internal.md.

# 🟢 Builtin default: "0"
data: null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"data" isn't descriptive because everything is data 😛

maybe additionalDisks (slice) ?

Copy link
Member Author

@afbjorklund afbjorklund Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, "disk" is also kind of vague. And "data" was ambiguous.

Using "extra" or "additional" could work, perhaps.


# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest
# 🟢 Builtin default: null (Mount nothing)
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable
Expand Down
10 changes: 10 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
y.Disk = pointer.String("100GiB")
}

if y.Data == nil {
y.Data = d.Data
}
if o.Data != nil {
y.Data = o.Data
}
if y.Data == nil || *y.Data == "" {
y.Data = pointer.String("0")
}

if y.Video.Display == nil {
y.Video.Display = d.Video.Display
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestFillDefault(t *testing.T) {
CPUs: pointer.Int(4),
Memory: pointer.String("4GiB"),
Disk: pointer.String("100GiB"),
Data: pointer.String("0"),
Containerd: Containerd{
System: pointer.Bool(false),
User: pointer.Bool(true),
Expand Down Expand Up @@ -190,6 +191,7 @@ func TestFillDefault(t *testing.T) {
CPUs: pointer.Int(7),
Memory: pointer.String("5GiB"),
Disk: pointer.String("105GiB"),
Data: pointer.String("10GiB"),
Containerd: Containerd{
System: pointer.Bool(true),
User: pointer.Bool(false),
Expand Down Expand Up @@ -314,6 +316,7 @@ func TestFillDefault(t *testing.T) {
CPUs: pointer.Int(12),
Memory: pointer.String("7GiB"),
Disk: pointer.String("117GiB"),
Data: pointer.String("17GiB"),
Containerd: Containerd{
System: pointer.Bool(true),
User: pointer.Bool(false),
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type LimaYAML struct {
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes
Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes
Data *string `yaml:"data,omitempty" json:"data,omitempty"` // go-units.RAMInBytes
Mounts []Mount `yaml:"mounts,omitempty" json:"mounts,omitempty"`
SSH SSH `yaml:"ssh,omitempty" json:"ssh,omitempty"` // REQUIRED (FIXME)
Firmware Firmware `yaml:"firmware,omitempty" json:"firmware,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ func Validate(y LimaYAML, warn bool) error {
return fmt.Errorf("field `memory` has an invalid value: %w", err)
}

if y.Data != nil {
if _, err := units.RAMInBytes(*y.Data); err != nil {
return fmt.Errorf("field `data` has an invalid value: %w", err)
}
}

u, err := osutil.LimaUser(false)
if err != nil {
return fmt.Errorf("internal error (not an error of YAML): %w", err)
Expand Down
17 changes: 17 additions & 0 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ func EnsureDisk(cfg Config) error {
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
}
diskSize, _ = units.RAMInBytes(*cfg.LimaYAML.Data)
if diskSize == 0 {
return nil
}
dataDisk := filepath.Join(cfg.InstanceDir, filenames.DataDisk)
if _, err := os.Stat(dataDisk); errors.Is(err, os.ErrNotExist) {
args = []string{"create", "-f", "qcow2"}
args = append(args, dataDisk, strconv.Itoa(int(diskSize)))
cmd := exec.Command("qemu-img", args...)
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
}
}
return nil
}

Expand Down Expand Up @@ -317,6 +330,7 @@ func Cmdline(cfg Config) (string, []string, error) {

baseDisk := filepath.Join(cfg.InstanceDir, filenames.BaseDisk)
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
dataDisk := filepath.Join(cfg.InstanceDir, filenames.DataDisk)
isBaseDiskCDROM, err := iso9660util.IsISO9660(baseDisk)
if err != nil {
return "", nil, err
Expand All @@ -332,6 +346,9 @@ func Cmdline(cfg Config) (string, []string, error) {
} else if !isBaseDiskCDROM {
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio", baseDisk))
}
if _, err := os.Stat(dataDisk); err == nil {
args = append(args, "-drive", fmt.Sprintf("file=%s,if=virtio", dataDisk))
}
// cloud-init
args = append(args, "-cdrom", filepath.Join(cfg.InstanceDir, filenames.CIDataISO))

Expand Down
1 change: 1 addition & 0 deletions pkg/store/filenames/filenames.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
CIDataISO = "cidata.iso"
BaseDisk = "basedisk"
DiffDisk = "diffdisk"
DataDisk = "datadisk"
QemuPID = "qemu.pid"
QMPSock = "qmp.sock"
SerialLog = "serial.log"
Expand Down
5 changes: 5 additions & 0 deletions pkg/store/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Instance struct {
CPUs int `json:"cpus,omitempty"`
Memory int64 `json:"memory,omitempty"` // bytes
Disk int64 `json:"disk,omitempty"` // bytes
Data int64 `json:"data,omitempty"` // bytes
Message string `json:"message,omitempty"`
Networks []limayaml.Network `json:"network,omitempty"`
SSHLocalPort int `json:"sshLocalPort,omitempty"`
Expand Down Expand Up @@ -87,6 +88,10 @@ func Inspect(instName string) (*Instance, error) {
if err == nil {
inst.Disk = disk
}
data, err := units.RAMInBytes(*y.Data)
if err == nil {
inst.Data = data
}
inst.Message = y.Message
inst.Networks = y.Networks
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0
Expand Down