Ejemplo n.º 1
0
/*
 * Given a running container and a list of devices before and after a
 * config change, update the devices in the container.
 *
 * Currently we only support nics.  Disks will be supported once we
 * decide how best to insert them.
 */
func devicesApplyDeltaLive(tx *sql.Tx, c *lxdContainer, preDevList shared.Devices, postDevList shared.Devices) error {
	rmList, addList := preDevList.Update(postDevList)
	var err error

	// note - currently Devices.Update() only returns nics
	for key, nic := range rmList {
		if nic["name"] == "" {
			return fmt.Errorf("Do not know a name for the nic for device %s\n", key)
		}
		if err := detachInterface(c, nic["name"]); err != nil {
			return fmt.Errorf("Error removing device %s (nic %s) from container %s: %s", key, nic["name"], c.name, err)
		}
	}

	for key, nic := range addList {
		var tmpName string
		if tmpName, err = setupNic(c, nic); err != nil {
			return fmt.Errorf("Unable to create nic %s for container %s: %s", nic["name"], c.name, err)
		}
		if err := c.c.AttachInterface(tmpName, nic["name"]); err != nil {
			RemoveInterface(tmpName)
			return fmt.Errorf("Unable to move nic %s into container %s as %s: %s", tmpName, c.name, nic["name"], err)
		}
		// Now we need to add the name to the database
		if err := txUpdateNic(tx, c.id, key, nic["name"]); err != nil {
			shared.Debugf("Warning: failed to update database entry for new nic %s: %s\n", key, err)
		}
	}

	return nil
}
Ejemplo n.º 2
0
Archivo: devices.go Proyecto: xnox/lxd
/*
 * Given a running container and a list of devices before and after a
 * config change, update the devices in the container.
 *
 * Currently we only support nics.  Disks will be supported once we
 * decide how best to insert them.
 */
func devicesApplyDeltaLive(tx *sql.Tx, c container, preDevList shared.Devices, postDevList shared.Devices) error {
	rmList, addList := preDevList.Update(postDevList)
	var err error

	for key, dev := range rmList {
		switch dev["type"] {
		case "nic":
			if dev["name"] == "" {
				return fmt.Errorf("Do not know a name for the nic for device %s", key)
			}
			if err := detachInterface(c, dev["name"]); err != nil {
				return fmt.Errorf("Error removing device %s (nic %s) from container %s: %s", key, dev["name"], c.Name(), err)
			}
		case "disk":
			return c.DetachMount(dev)
		case "unix-block":
			return c.DetachUnixDev(dev)
		case "unix-char":
			return c.DetachUnixDev(dev)
		}
	}

	lxContainer := c.LXContainerGet()

	for key, dev := range addList {
		switch dev["type"] {
		case "nic":
			var tmpName string
			if tmpName, err = setupNic(tx, c, key, dev); err != nil {
				return fmt.Errorf("Unable to create nic %s for container %s: %s", dev["name"], c.Name(), err)
			}
			if err := lxContainer.AttachInterface(tmpName, dev["name"]); err != nil {
				removeInterface(tmpName)
				return fmt.Errorf("Unable to move nic %s into container %s as %s: %s", tmpName, c.Name(), dev["name"], err)
			}

			if err := txUpdateNic(tx, c.ID(), key, dev["name"]); err != nil {
				shared.Debugf("Warning: failed to update database entry for new nic %s: %s", key, err)
				return err
			}
		case "disk":
			if dev["source"] == "" || dev["path"] == "" {
				return fmt.Errorf("no source or destination given")
			}
			return c.AttachMount(dev)
		case "unix-block":
			return c.AttachUnixDev(dev)
		case "unix-char":
			return c.AttachUnixDev(dev)
		}
	}

	return nil
}
Ejemplo n.º 3
0
/*
 * Given a running container and a list of devices before and after a
 * config change, update the devices in the container.
 *
 * Currently we only support nics.  Disks will be supported once we
 * decide how best to insert them.
 */
func devicesApplyDeltaLive(tx *sql.Tx, c *lxdContainer, preDevList shared.Devices, postDevList shared.Devices) error {
	rmList, addList := preDevList.Update(postDevList)
	var err error

	// note - currently Devices.Update() only returns nics
	for key, dev := range rmList {
		switch dev["type"] {
		case "nic":
			if dev["name"] == "" {
				return fmt.Errorf("Do not know a name for the nic for device %s\n", key)
			}
			if err := detachInterface(c, dev["name"]); err != nil {
				return fmt.Errorf("Error removing device %s (nic %s) from container %s: %s", key, dev["name"], c.name, err)
			}
		case "disk":
			return c.detachMount(dev)
		}
	}

	for key, dev := range addList {
		switch dev["type"] {
		case "nic":
			var tmpName string
			if tmpName, err = setupNic(c, dev); err != nil {
				return fmt.Errorf("Unable to create nic %s for container %s: %s", dev["name"], c.name, err)
			}
			if err := c.c.AttachInterface(tmpName, dev["name"]); err != nil {
				RemoveInterface(tmpName)
				return fmt.Errorf("Unable to move nic %s into container %s as %s: %s", tmpName, c.name, dev["name"], err)
			}
			// Now we need to add the name to the database
			if err := txUpdateNic(tx, c.id, key, dev["name"]); err != nil {
				shared.Debugf("Warning: failed to update database entry for new nic %s: %s\n", key, err)
			}
		case "disk":
			if dev["source"] == "" || dev["path"] == "" {
				return fmt.Errorf("no source or destination given")
			}
			return c.attachMount(dev)
		}
	}

	return nil
}