From a80c79ed709ca84ae3b362fcd5133ab5e49d0fef Mon Sep 17 00:00:00 2001 From: dlorenc Date: Fri, 11 May 2018 10:48:49 -0700 Subject: [PATCH] Start the default network for the kvm2 driver. --- pkg/drivers/kvm/network.go | 48 +++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/pkg/drivers/kvm/network.go b/pkg/drivers/kvm/network.go index b3b124f1e68c..ab8c41053d6b 100644 --- a/pkg/drivers/kvm/network.go +++ b/pkg/drivers/kvm/network.go @@ -43,6 +43,34 @@ const networkTmpl = ` ` +func setupNetwork(conn *libvirt.Connect, name string) error { + n, err := conn.LookupNetworkByName(defaultNetworkName) + if err != nil { + return errors.Wrapf(err, "checking network %s", name) + } + autostart, err := n.GetAutostart() + if err != nil { + return errors.Wrapf(err, "checking network %s autostart", name) + } + if !autostart { + if err := n.SetAutostart(true); err != nil { + return errors.Wrapf(err, "setting autostart for network %s", name) + } + } + + active, err := n.IsActive() + if err != nil { + return errors.Wrapf(err, "checking network status for %s", name) + } + if !active { + if err := n.Create(); err != nil { + return errors.Wrapf(err, "starting network %s", name) + } + } + return nil + +} + func (d *Driver) createNetwork() error { if d.MAC == "" { mac, err := randomMAC() @@ -59,11 +87,16 @@ func (d *Driver) createNetwork() error { tmpl := template.Must(template.New("network").Parse(networkTmpl)) var networkXML bytes.Buffer - err = tmpl.Execute(&networkXML, d) - if err != nil { + if err := tmpl.Execute(&networkXML, d); err != nil { return errors.Wrap(err, "executing network template") } + // Start the default network + log.Infof("Setting up network %s", defaultNetworkName) + if err := setupNetwork(conn, defaultNetworkName); err != nil { + return err + } + //Check if network already exists if _, err := conn.LookupNetworkByName(d.PrivateNetwork); err == nil { return nil @@ -73,14 +106,13 @@ func (d *Driver) createNetwork() error { if err != nil { return errors.Wrapf(err, "defining network from xml: %s", networkXML.String()) } - err = network.SetAutostart(true) - if err != nil { - return errors.Wrap(err, "setting network to autostart") + if err := network.Create(); err != nil { + return errors.Wrapf(err, "creating network %s", d.PrivateNetwork) } - err = network.Create() - if err != nil { - return errors.Wrap(err, "creating network") + log.Infof("Setting up network %s", d.PrivateNetwork) + if err := setupNetwork(conn, d.PrivateNetwork); err != nil { + return err } return nil