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 28, 2025
1 parent 2789218 commit 8e11b03
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
8 changes: 4 additions & 4 deletions hack/clean-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ virsh -c qemu:///system net-destroy baremetal-e2e
virsh -c qemu:///system net-undefine baremetal-e2e

# Clean volume pool directory
rm -rf /tmp/pool_oo/*
rm -rf /tmp/pool_oo

# Clean volume pool
virsh pool-destroy default || true
virsh pool-delete default || true
virsh pool-undefine default || true
virsh pool-destroy oooq_pool || true
virsh pool-delete oooq_pool || true
virsh pool-undefine oooq_pool || true
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, 0777); 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
5 changes: 2 additions & 3 deletions test/vbmctl/templates/VM.xml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='{{ .PoolPath }}/{{ .HostName }}.qcow2'/>
<target dev='vda' bus='virtio'/>
<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

0 comments on commit 8e11b03

Please sign in to comment.