Skip to content

Commit

Permalink
Switch to session libvirt
Browse files Browse the repository at this point in the history
The `crc` network can live on the system libvirt while we do the rest on
the session connection.

Fixes #20.
  • Loading branch information
zeenix committed Oct 29, 2019
1 parent 49e9d16 commit 0da5269
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
9 changes: 5 additions & 4 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const (
DriverName = "libvirt"
DriverVersion = "0.12.6"

connectionString = "qemu:///system"
sessionConnectionString = "qemu:///session"
systemConnectionString = "qemu:///system"

dnsmasqLeases = "/var/lib/libvirt/dnsmasq/%s.leases"
dnsmasqStatus = "/var/lib/libvirt/dnsmasq/%s.status"
DefaultMemory = 8096
Expand Down Expand Up @@ -43,9 +45,9 @@ const (
<graphics type='vnc' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
</graphics>
<interface type='network'>
<interface type='bridge'>
<mac address='52:fd:fc:07:21:82'/>
<source network='{{.Network}}'/>
<source bridge='{{.Network}}'/>
<model type='virtio'/>
</interface>
<console type='pty'></console>
Expand All @@ -58,4 +60,3 @@ const (
</devices>
</domain>`
)

41 changes: 28 additions & 13 deletions libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type Driver struct {

// Libvirt connection and state
connectionString string
conn *libvirt.Connect
sessionConn *libvirt.Connect
systemConn *libvirt.Connect
VM *libvirt.Domain
vmLoaded bool
}
Expand Down Expand Up @@ -146,22 +147,36 @@ func (d *Driver) GetURL() (string, error) {
return "", nil
}

func (d *Driver) getConn() (*libvirt.Connect, error) {
if d.conn == nil {
conn, err := libvirt.NewConnect(connectionString)
func (d *Driver) getSessionConn() (*libvirt.Connect, error) {
log.Debugf("Connecting to session")
if d.sessionConn == nil {
conn, err := libvirt.NewConnect(sessionConnectionString)
if err != nil {
log.Errorf("Failed to connect to libvirt: %s", err)
return &libvirt.Connect{}, errors.New("Unable to connect to kvm driver")
}
d.sessionConn = conn
}
return d.sessionConn, nil
}

func (d *Driver) getSystemConn() (*libvirt.Connect, error) {
log.Debugf("Connecting to system")
if d.systemConn == nil {
conn, err := libvirt.NewConnectReadOnly(systemConnectionString)
if err != nil {
log.Errorf("Failed to connect to libvirt: %s", err)
return &libvirt.Connect{}, errors.New("Unable to connect to kvm driver, did you add yourself to the libvirtd group?")
}
d.conn = conn
d.systemConn = conn
}
return d.conn, nil
return d.systemConn, nil
}

// Create, or verify the private network is properly configured
func (d *Driver) validateNetwork() error {
log.Debug("Validating network")
conn, err := d.getConn()
conn, err := d.getSystemConn()
if err != nil {
return err
}
Expand Down Expand Up @@ -211,7 +226,7 @@ func (d *Driver) validateNetwork() error {
}

func (d *Driver) PreCreateCheck() error {
conn, err := d.getConn()
conn, err := d.getSessionConn()
if err != nil {
return err
}
Expand Down Expand Up @@ -282,7 +297,7 @@ func (d *Driver) Create() error {
return err
}

conn, err := d.getConn()
conn, err := d.getSessionConn()
if err != nil {
return err
}
Expand Down Expand Up @@ -430,7 +445,7 @@ func (d *Driver) GetState() (state.State, error) {
func (d *Driver) validateVMRef() error {
if !d.vmLoaded {
log.Debugf("Fetching VM...")
conn, err := d.getConn()
conn, err := d.getSessionConn()
if err != nil {
return err
}
Expand Down Expand Up @@ -461,7 +476,7 @@ func (d *Driver) getMAC() (string, error) {
...
<devices>
...
<interface type='network'>
<interface type='bridge'>
...
<mac address='52:54:00:d2:3f:ba'/>
...
Expand All @@ -472,7 +487,7 @@ func (d *Driver) getMAC() (string, error) {
Address string `xml:"address,attr"`
}
type Source struct {
Network string `xml:"network,attr"`
Network string `xml:"bridge,attr"`
}
type Interface struct {
Type string `xml:"type,attr"`
Expand All @@ -496,7 +511,7 @@ func (d *Driver) getMAC() (string, error) {
}

func (d *Driver) getIPByMacFromSettings(mac string) (string, error) {
conn, err := d.getConn()
conn, err := d.getSystemConn()
if err != nil {
return "", err
}
Expand Down

0 comments on commit 0da5269

Please sign in to comment.