Skip to content

Commit c91ebc0

Browse files
committed
virter: add pool option to Disk
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.
1 parent d77dac5 commit c91ebc0

File tree

6 files changed

+21
-5
lines changed

6 files changed

+21
-5
lines changed

cmd/disk.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type DiskArg struct {
1212
Size Size `arg:"size"`
1313
Format string `arg:"format,qcow2"`
1414
Bus string `arg:"bus,virtio"`
15+
Pool string `arg:"pool,"`
1516
}
1617

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

4042
// Set implements flag.Value.Set.
4143
func (d *DiskArg) Set(str string) error {

cmd/disk_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ func TestFromFlag(t *testing.T) {
2020
expectError: true,
2121
}, {
2222
name: "full spec parses",
23-
input: "name=test,size=5GiB,format=qcow2,bus=virtio",
23+
input: "name=test,size=5GiB,format=qcow2,bus=virtio,pool=mypool",
2424
expect: cmd.DiskArg{
2525
Name: "test",
2626
Size: cmd.Size{KiB: uint64(5 * unit.G / unit.K)},
2727
Format: "qcow2",
2828
Bus: "virtio",
29+
Pool: "mypool",
2930
},
3031
}, {
3132
name: "only required args parses",

cmd/vm_run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func vmRunCommand() *cobra.Command {
300300
// and then manually marshal them to Disks.
301301
// If this ever gets implemented in pflag , we will be able to solve this
302302
// in a much smoother way.
303-
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`)
303+
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`)
304304
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`)
305305
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"`)
306306

internal/virter/libvirtxml.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,13 @@ func (v *Virter) vmXML(vm VMConfig, mac string, meta *VMMeta) (string, error) {
101101
VMDisk{device: VMDiskDeviceCDROM, poolName: v.provisionStoragePool.Name, volumeName: DynamicLayerName(ciDataVolumeName(vm.Name)), bus: "scsi", format: "raw"},
102102
}
103103
for _, d := range vm.Disks {
104+
pool := d.GetPool()
105+
if pool == "" {
106+
pool = v.provisionStoragePool.Name
107+
}
104108
vmDisks = append(vmDisks, VMDisk{
105109
device: VMDiskDeviceDisk,
106-
poolName: v.provisionStoragePool.Name,
110+
poolName: pool,
107111
volumeName: DynamicLayerName(diskVolumeName(vm.Name, d.GetName())),
108112
bus: d.GetBus(),
109113
format: d.GetFormat(),

internal/virter/virter.go

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ type Disk interface {
113113
GetSizeKiB() uint64
114114
GetFormat() string
115115
GetBus() string
116+
GetPool() string
116117
}
117118

118119
type NICType string

internal/virter/vm.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ func (v *Virter) anyImageExists(vmConfig VMConfig) (bool, error) {
5151
}
5252

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

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

165169
for _, d := range vmConfig.Disks {
166170
log.Printf("Create volume '%s'", d.GetName())
167-
_, err = v.NewDynamicLayer(diskVolumeName(vmConfig.Name, d.GetName()), v.provisionStoragePool, WithCapacity(d.GetSizeKiB()), WithFormat(d.GetFormat()))
171+
pool, err := v.lookupPool(d.GetPool())
172+
if err != nil {
173+
return fmt.Errorf("failed to lookup libvirt pool %s: %w", d.GetPool(), err)
174+
}
175+
_, err = v.NewDynamicLayer(diskVolumeName(vmConfig.Name, d.GetName()), pool, WithCapacity(d.GetSizeKiB()), WithFormat(d.GetFormat()))
168176
if err != nil {
169177
return err
170178
}

0 commit comments

Comments
 (0)