Skip to content

Commit

Permalink
virter: add pool option to Disk
Browse files Browse the repository at this point in the history
We may want to attach "dynamic" disks to a VM that are stored in a
different storage pool than the default one.

So allow users to specify a "pool=..." option on the command line, which
changes the storage location of the disk to the specified pool.
  • Loading branch information
chrboe committed May 25, 2023
1 parent d77dac5 commit c91ebc0
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cmd/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type DiskArg struct {
Size Size `arg:"size"`
Format string `arg:"format,qcow2"`
Bus string `arg:"bus,virtio"`
Pool string `arg:"pool,"`
}

type Size struct {
Expand All @@ -36,6 +37,7 @@ func (d *DiskArg) GetName() string { return d.Name }
func (d *DiskArg) GetSizeKiB() uint64 { return d.Size.KiB }
func (d *DiskArg) GetFormat() string { return d.Format }
func (d *DiskArg) GetBus() string { return d.Bus }
func (d *DiskArg) GetPool() string { return d.Pool }

// Set implements flag.Value.Set.
func (d *DiskArg) Set(str string) error {
Expand Down
3 changes: 2 additions & 1 deletion cmd/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ func TestFromFlag(t *testing.T) {
expectError: true,
}, {
name: "full spec parses",
input: "name=test,size=5GiB,format=qcow2,bus=virtio",
input: "name=test,size=5GiB,format=qcow2,bus=virtio,pool=mypool",
expect: cmd.DiskArg{
Name: "test",
Size: cmd.Size{KiB: uint64(5 * unit.G / unit.K)},
Format: "qcow2",
Bus: "virtio",
Pool: "mypool",
},
}, {
name: "only required args parses",
Expand Down
2 changes: 1 addition & 1 deletion cmd/vm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func vmRunCommand() *cobra.Command {
// and then manually marshal them to Disks.
// If this ever gets implemented in pflag , we will be able to solve this
// in a much smoother way.
runCmd.Flags().StringArrayVarP(&diskStrings, "disk", "d", []string{}, `Add a disk to the VM. Format: "name=disk1,size=100MiB,format=qcow2,bus=virtio". Can be specified multiple times`)
runCmd.Flags().StringArrayVarP(&diskStrings, "disk", "d", []string{}, `Add a disk to the VM. Format: "name=disk1,size=100MiB,format=qcow2,bus=virtio,pool=mypool". Can be specified multiple times`)
runCmd.Flags().StringArrayVarP(&nicStrings, "nic", "i", []string{}, `Add a NIC to the VM. Format: "type=network,source=some-net-name". Type can also be "bridge", in which case the source is the bridge device name. Additional config options are "model" (default: virtio) and "mac" (default chosen by libvirt). Can be specified multiple times`)
runCmd.Flags().StringArrayVarP(&mountStrings, "mount", "v", []string{}, `Mount a host path in the VM, like a bind mount. Format: "host=/path/on/host,vm=/path/in/vm"`)

Expand Down
6 changes: 5 additions & 1 deletion internal/virter/libvirtxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ func (v *Virter) vmXML(vm VMConfig, mac string, meta *VMMeta) (string, error) {
VMDisk{device: VMDiskDeviceCDROM, poolName: v.provisionStoragePool.Name, volumeName: DynamicLayerName(ciDataVolumeName(vm.Name)), bus: "scsi", format: "raw"},
}
for _, d := range vm.Disks {
pool := d.GetPool()
if pool == "" {
pool = v.provisionStoragePool.Name
}
vmDisks = append(vmDisks, VMDisk{
device: VMDiskDeviceDisk,
poolName: v.provisionStoragePool.Name,
poolName: pool,
volumeName: DynamicLayerName(diskVolumeName(vm.Name, d.GetName())),
bus: d.GetBus(),
format: d.GetFormat(),
Expand Down
1 change: 1 addition & 0 deletions internal/virter/virter.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Disk interface {
GetSizeKiB() uint64
GetFormat() string
GetBus() string
GetPool() string
}

type NICType string
Expand Down
12 changes: 10 additions & 2 deletions internal/virter/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func (v *Virter) anyImageExists(vmConfig VMConfig) (bool, error) {
}

for _, d := range vmConfig.Disks {
imgs = append(imgs, imageAndPool{diskVolumeName(vmName, d.GetName()), v.provisionStoragePool})
pool, err := v.lookupPool(d.GetPool())
if err != nil {
return false, fmt.Errorf("failed to lookup libvirt pool: %w", err)
}
imgs = append(imgs, imageAndPool{diskVolumeName(vmName, d.GetName()), pool})
}

for _, img := range imgs {
Expand Down Expand Up @@ -164,7 +168,11 @@ func (v *Virter) VMRun(vmConfig VMConfig) error {

for _, d := range vmConfig.Disks {
log.Printf("Create volume '%s'", d.GetName())
_, err = v.NewDynamicLayer(diskVolumeName(vmConfig.Name, d.GetName()), v.provisionStoragePool, WithCapacity(d.GetSizeKiB()), WithFormat(d.GetFormat()))
pool, err := v.lookupPool(d.GetPool())
if err != nil {
return fmt.Errorf("failed to lookup libvirt pool %s: %w", d.GetPool(), err)
}
_, err = v.NewDynamicLayer(diskVolumeName(vmConfig.Name, d.GetName()), pool, WithCapacity(d.GetSizeKiB()), WithFormat(d.GetFormat()))
if err != nil {
return err
}
Expand Down

0 comments on commit c91ebc0

Please sign in to comment.