Skip to content

Commit

Permalink
Change pool name and hard drive path
Browse files Browse the repository at this point in the history
Signed-off-by: Huy Mai <[email protected]>
  • Loading branch information
mquhuy committed Jan 24, 2025
1 parent a5e5470 commit 7b542ec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
55 changes: 39 additions & 16 deletions test/vbmctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ var (
templateFiles embed.FS
)

const (
poolName = "oooq_pool"
poolPath = "/tmp/pool_oo"
)

type Host struct {
HostName string
Networks bmoe2e.Networks
PoolName string
PoolPath string
}

Expand All @@ -42,9 +48,36 @@ func RenderTemplate(inputFile string, data interface{}) (string, error) {
return buf.String(), nil
}

func startVolumePool(pool *libvirt.StoragePool) error {
if err := pool.SetAutostart(true); err != nil {
fmt.Println("Failed to Set the pool autostart")
fmt.Printf("Error occurred: %v\n", err)
return err
}

active, err := pool.IsActive()
if err != nil {
return err
}

if active {
return nil
}

if err := pool.Create(0); err != nil {
fmt.Println("Failed to Start the pool")
fmt.Printf("Error occurred: %v\n", err)
return err
}
return nil
}

// CreateVolumePool creates a volume pool with specified name if a pool with
// that name does not exist yet.
func CreateVolumePool(poolName, poolPath string) (*libvirt.StoragePool, error) {
if err := os.MkdirAll(poolPath, 0755); err != nil && !os.IsExist(err) {
return nil, err
}
// Connect to libvirt daemon
conn, err := libvirt.NewConnect("qemu:///system")
if err != nil {
Expand All @@ -57,6 +90,9 @@ func CreateVolumePool(poolName, poolPath string) (*libvirt.StoragePool, error) {

if err == nil {
fmt.Println("Pool already exists")
if err := startVolumePool(pool); err != nil {
return nil, err
}
return pool, nil
}

Expand Down Expand Up @@ -90,15 +126,7 @@ func CreateVolumePool(poolName, poolPath string) (*libvirt.StoragePool, error) {
return nil, err
}

if err = pool.SetAutostart(true); err != nil {
fmt.Println("Failed to Set the pool autostart")
fmt.Printf("Error occurred: %v\n", err)
return nil, err
}

if err = pool.Create(0); err != nil {
fmt.Println("Failed to Start the pool")
fmt.Printf("Error occurred: %v\n", err)
if err := startVolumePool(pool); err != nil {
return nil, err
}

Expand Down Expand Up @@ -160,8 +188,6 @@ func CreateVolume(volumeName, poolName, poolPath string, capacityInGB int) error
// started. Errors during qcow2 file creation, volume creation, libvirt connection,
// template rendering, or domain creation are returned.
func CreateLibvirtVM(hostName string, networks *bmoe2e.Networks) error {
poolName := "default"
poolPath := "/tmp/pool_oo"
opts := make(map[string]any)
opts[qcow2.OPT_SIZE] = 3 * (1 << 30) // qcow2 file's size is 3g
opts[qcow2.OPT_FMT] = "qcow2" // qcow2 format
Expand All @@ -186,13 +212,10 @@ func CreateLibvirtVM(hostName string, networks *bmoe2e.Networks) error {
}
defer conn.Close()

data := struct {
HostName string
Networks bmoe2e.Networks
PoolPath string
}{
data := Host{
HostName: hostName,
Networks: *networks,
PoolName: poolName,
PoolPath: poolPath,
}

Expand Down
11 changes: 5 additions & 6 deletions test/vbmctl/templates/VM.xml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='1'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='{{ .PoolPath }}/{{ .HostName }}.qcow2'/>
<target dev='vda' bus='virtio'/>
<disk type='volume' device='disk'>
<driver name='qemu' type='qcow2' cache='unsafe'/>
<source pool='{{ .PoolName }}' volume='{{ .HostName }}.qcow2'/>
<target dev='sda' bus='scsi'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='scsi' index='0' model='virtio-scsi'>
<address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
Expand Down Expand Up @@ -80,13 +81,11 @@
</interface>
{{ end }}
<serial type='pty'>
<log file='/var/log/libvirt/qemu/{{ .HostName }}-serial0.log' append='on'/>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<log file='/var/log/libvirt/qemu/{{ .HostName }}-serial0.log' append='on'/>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
Expand Down
5 changes: 0 additions & 5 deletions test/vbmctl/templates/pool.xml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@
<name>{{ .PoolName }}</name>
<target>
<path>{{ .PoolPath }}</path>
<permissions>
<mode>0755</mode>
<owner>-1</owner>
<group>-1</group>
</permissions>
</target>
</pool>

0 comments on commit 7b542ec

Please sign in to comment.