|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + libvirtxml "github.com/libvirt/libvirt-go-xml" |
| 5 | + log "github.com/sirupsen/logrus" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "net" |
| 8 | +) |
| 9 | + |
| 10 | +func networkAddCommand() *cobra.Command { |
| 11 | + var forward string |
| 12 | + var dhcp bool |
| 13 | + var network string |
| 14 | + var domain string |
| 15 | + |
| 16 | + addCmd := &cobra.Command{ |
| 17 | + Use: "add <name>", |
| 18 | + Short: "Add a new network", |
| 19 | + Long: `Add a new network. VMs can be attached to such a network in addition to the default network used by virter.`, |
| 20 | + Args: cobra.ExactArgs(1), |
| 21 | + Run: func(cmd *cobra.Command, args []string) { |
| 22 | + v, err := InitVirter() |
| 23 | + if err != nil { |
| 24 | + log.Fatal(err) |
| 25 | + } |
| 26 | + defer v.ForceDisconnect() |
| 27 | + |
| 28 | + var forwardDesc *libvirtxml.NetworkForward |
| 29 | + if forward != "" { |
| 30 | + forwardDesc = &libvirtxml.NetworkForward{ |
| 31 | + Mode: forward, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + var addressesDesc []libvirtxml.NetworkIP |
| 36 | + if network != "" { |
| 37 | + ip, n, err := net.ParseCIDR(network) |
| 38 | + if err != nil { |
| 39 | + log.Fatal(err) |
| 40 | + } |
| 41 | + |
| 42 | + var dhcpDesc *libvirtxml.NetworkDHCP |
| 43 | + if dhcp { |
| 44 | + start := nextIP(ip) |
| 45 | + end := previousIP(broadcastAddress(n)) |
| 46 | + |
| 47 | + n.Network() |
| 48 | + dhcpDesc = &libvirtxml.NetworkDHCP{ |
| 49 | + Ranges: []libvirtxml.NetworkDHCPRange{{Start: start.String(), End: end.String()}}, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + addressesDesc = append(addressesDesc, libvirtxml.NetworkIP{ |
| 54 | + Address: ip.String(), |
| 55 | + Netmask: net.IP(n.Mask).String(), |
| 56 | + DHCP: dhcpDesc, |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + var domainDesc *libvirtxml.NetworkDomain |
| 61 | + if domain != "" { |
| 62 | + domainDesc = &libvirtxml.NetworkDomain{ |
| 63 | + Name: domain, |
| 64 | + LocalOnly: "yes", |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + desc := libvirtxml.Network{ |
| 69 | + Name: args[0], |
| 70 | + Forward: forwardDesc, |
| 71 | + IPs: addressesDesc, |
| 72 | + Domain: domainDesc, |
| 73 | + } |
| 74 | + |
| 75 | + err = v.NetworkAdd(desc) |
| 76 | + if err != nil { |
| 77 | + log.Fatal(err) |
| 78 | + } |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + addCmd.Flags().StringVarP(&forward, "forward-mode", "m", "", "Set the forward mode, for example 'nat'") |
| 83 | + addCmd.Flags().StringVarP(&network, "network-cidr", "n", "", "Configure the network range (IPv4) in CIDR notation. The IP will be assigned to the host device.") |
| 84 | + addCmd.Flags().BoolVarP(&dhcp, "dhcp", "p", false, "Configure DHCP. Use together with '--network-cidr'. DHCP range is configured starting from --network-cidr+1 until the broadcast address") |
| 85 | + addCmd.Flags().StringVarP(&domain, "domain", "d", "", "Configure DNS names for the network") |
| 86 | + return addCmd |
| 87 | +} |
| 88 | + |
| 89 | +func nextIP(ip net.IP) net.IP { |
| 90 | + dup := make(net.IP, len(ip)) |
| 91 | + copy(dup, ip) |
| 92 | + for j := len(dup) - 1; j >= 0; j-- { |
| 93 | + dup[j]++ |
| 94 | + if dup[j] > 0 { |
| 95 | + break |
| 96 | + } |
| 97 | + } |
| 98 | + return dup |
| 99 | +} |
| 100 | + |
| 101 | +func previousIP(ip net.IP) net.IP { |
| 102 | + dup := make(net.IP, len(ip)) |
| 103 | + copy(dup, ip) |
| 104 | + for j := len(dup) - 1; j >= 0; j-- { |
| 105 | + dup[j]-- |
| 106 | + if dup[j] < 255 { |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | + return dup |
| 111 | +} |
| 112 | + |
| 113 | +func broadcastAddress(ipnet *net.IPNet) net.IP { |
| 114 | + dup := make(net.IP, len(ipnet.IP)) |
| 115 | + copy(dup, ipnet.IP) |
| 116 | + for i := range dup { |
| 117 | + dup[i] = dup[i] | ^ipnet.Mask[i] |
| 118 | + } |
| 119 | + return dup |
| 120 | +} |
0 commit comments