Beispiel #1
0
func (device *NetworkDevice) BringGuestOnline(vm VirtualMachine.VirtualMachine) error {
	id := strconv.Itoa(device.DeviceID)
	vmid := vm.GetUUID()
	deviceid := "epair" + id + "b"
	needs_dhcp := false

	if vm.IsOnline() == false {
		return errors.New("VM is turned off. VM must be turned on to have its networking stack brought online")
	}

	if device.IsOnline() == false {
		if err := device.BringHostOnline(); err != nil {
			return err
		}
	}

	cmd := exec.Command("/sbin/ifconfig", deviceid, "vnet", vmid)
	for _, option := range device.Options {
		if option.OptionKey == "DHCP" {
			needs_dhcp = true
			continue
		}

		cmd.Args = append(cmd.Args, option.OptionKey)
		if len(option.OptionValue) > 0 {
			cmd.Args = append(cmd.Args, option.OptionValue)
		}
	}

	err := cmd.Run()

	if err != nil {
		return err
	}

	if needs_dhcp {
		cmd = exec.Command("/usr/sbin/jexec", vmid, "/sbin/dhclient", deviceid)
		if rawoutput, err := cmd.CombinedOutput(); err != nil {
			return fmt.Errorf("/sbin/dhclient %s: %s", deviceid, virtbsdutil.ByteToString(rawoutput))
		}
	}

	for _, address := range device.Addresses {
		proto := "inet"
		if strings.Index(address.Address, ":") >= 0 {
			proto = "inet6"
		}

		cmd = exec.Command("/usr/sbin/jexec", vmid, "/sbin/ifconfig", deviceid, proto, address.Address, "alias")
		err = cmd.Run()

		if err != nil {
			device.BringOffline()
			return err
		}
	}

	cmd = exec.Command("/usr/sbin/jexec", vmid, "/sbin/ifconfig", deviceid, "up")

	return nil
}
Beispiel #2
0
func GetNetworkDevices(db map[string]interface{}, vm VirtualMachine.VirtualMachine) []*NetworkDevice {
	var devices []*NetworkDevice

	if _, ok := db["dbmap"]; ok == true {
		_, err := db["dbmap"].(*gorp.DbMap).Select(&devices, "select * from NetworkDevice where VmUUID = ?", vm.GetUUID())
		if err != nil {
			panic(err)
			return nil
		}
	} else {
		_, err := db["sqlexecutor"].(gorp.SqlExecutor).Select(&devices, "select * from NetworkDevice where VmUUID = ?", vm.GetUUID())
		if err != nil {
			panic(err)
			return nil
		}
	}

	return devices
}