From 0da5269470c8f6205cea62940b0dd6cccfe46d12 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Date: Tue, 1 Oct 2019 14:21:40 +0200 Subject: [PATCH] Switch to session libvirt The `crc` network can live on the system libvirt while we do the rest on the session connection. Fixes #20. --- constants.go | 9 +++++---- libvirt.go | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/constants.go b/constants.go index 34efcb35..917be351 100644 --- a/constants.go +++ b/constants.go @@ -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 @@ -43,9 +45,9 @@ const ( - + - + @@ -58,4 +60,3 @@ const ( ` ) - diff --git a/libvirt.go b/libvirt.go index e84c1d44..df8f498e 100644 --- a/libvirt.go +++ b/libvirt.go @@ -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 } @@ -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 } @@ -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 } @@ -282,7 +297,7 @@ func (d *Driver) Create() error { return err } - conn, err := d.getConn() + conn, err := d.getSessionConn() if err != nil { return err } @@ -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 } @@ -461,7 +476,7 @@ func (d *Driver) getMAC() (string, error) { ... ... - + ... ... @@ -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"` @@ -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 }