From 4a80534bfe33eaaf4ea88ec42bb904d78445ca2e Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Wed, 16 Jun 2021 14:03:42 +0200 Subject: [PATCH] Template: IPv6 understands auto, IPv4/IPv6 has sanity-functions. DataCenter-description. --- lib/pve/templates.rb | 70 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/lib/pve/templates.rb b/lib/pve/templates.rb index 1074ec7..f331cf7 100644 --- a/lib/pve/templates.rb +++ b/lib/pve/templates.rb @@ -27,14 +27,27 @@ module PVE::CTTemplate File.read( options[:'ssh-public-keys-file'] || '/root/.ssh/authorized_keys') end + def _ipv4 ip, gw + return [ip, nil] if %w[dhcp].include? ip + ip = IPAddress::IPv4.new ip + [ip.to_string, gw || ip.hosts.last.to_s] + end + + def _ipv6 ip, gw + return [ip, nil] if %w[dhcp auto].include? ip + ip = IPAddress::IPv6.new ip + [ip.to_string, gw || ip.hosts.last.to_s] + end + def net0() if options.ipv4 || options.ipv6 - ipv4 = IPAddress::IPv4.new options.ipv4 + ipv4, gw4 = _ipv4( options.ipv4, options.gateway4) + ipv6, gw6 = _ipv6( options.ipv6, options.gateway6) { name: 'eth0', bridge: 'vmbr1', - ip: ipv4.to_string, - gw: options.gateway || ipv4.hosts.last.to_s + ip: ipv4, ip6: ipv6, + gw: gw4, gw6: gw6, } end end @@ -44,6 +57,10 @@ module PVE::CTTemplate end class Default < Base + def self.help + nil + end + def self.requirements { node: [:string, false, "Create CT on this node."], @@ -62,7 +79,7 @@ module PVE::CTTemplate :'ssh-public-keys-file' => [:string, false, "Read SSH-Public-Keys from file."], ipv4: [:string, false, "IPv4-Address with net-size."], gateway4: [:string, false, "IPv4-Address of gateway."], - ipv6: [:string, false, "IPv6-Address with net-size."], + ipv6: [:string, false, "IPv6-Address with net-size or auto."], gateway6: [:string, false, "IPv6-Address of gateway."], storage: [:string, false, "Device will be create on this Storage (default: local"], } @@ -70,12 +87,24 @@ module PVE::CTTemplate end class Datacenter < Base + def self.help + <<-EOF.gsub /^ {6}/, '' + Datacenter provides an interface for special network-settings. + Networks in Datacenters are often based on this behaviour: + A Network has an ID like 99. + This defines the VLANs: 2099 for Layer2/3099 for Layer3. + The IPv4-Range would be 10.99.0.0/16, but container will be put static in 10.99.255.0/24. + IPv6 uses RADV, so we do not need to know the IPv6-Range => auto. + VMID can be generated by Network-ID, too: smallest unused VMID in 100*ID. + EOF + end + def self.requirements { node: [:string, false, "Create CT on this node."], name: [:string, true, "Set (uniq) name"], arch: [:enum, false, "Architecture", %w[amd64 i386 arm64 armhf]], - vmid: [:numeric, true, "VM-ID. Proxmox internal number (100...)"], + vmid: [:numeric, false, "VM-ID. Proxmox internal number (100...)"], ostype: [:string, true, "OS-Type (OS or distribution)"], cmode: [:enum, false, "Console-mode", %w[shell console tty]], cores: [:numeric, false, "Count of cores"], @@ -87,12 +116,12 @@ module PVE::CTTemplate :'ssh-public-keys' => [:string, false, "SSH-Public-Keys, which should be added to root-user in CT."], :'ssh-public-keys-file' => [:string, false, "Read SSH-Public-Keys from file."], :'network-id' => [:numeric, true, "Put Container to this VLAN and use a random IPv4-Address for this CT."], - ipv4: [:string, false, "IPv4-Address with net-size."], + ipv4: [:string, false, "IPv4-Address with net-size or dhcp."], gateway4: [:string, false, "IPv4-Address of gateway."], - ipv6: [:string, false, "IPv6-Address with net-size."], + ipv6: [:string, false, "IPv6-Address with net-size or auto|dhcp."], gateway6: [:string, false, "IPv6-Address of gateway."], storage: [:string, false, "Device will be create on this Storage (default: root)"], - ostemplate: [:string, false, "OS-Template eg. vztmp/superlinux-1.2-amd64.tar.xz"], + ostemplate: [:string, false, "OS-Template eg. local:vztmpl/superlinux-1.2-amd64.tar.xz"], } end @@ -112,15 +141,27 @@ module PVE::CTTemplate end def net0 + ipv4, gw4 = + if options.ipv4 + _ipv4( options.ipv4, options.gateway4) + else + self.ipv4_gw + end + ipv6, gw6 = + if options.ipv6 + _ipv6( options.ipv6, options.gateway6) + else + ['auto', nil] + end { name: 'eth0', bridge: 'vmbr1', tag: 2000+network_id, mtu: 9166, firewall: 1, - ip: ipv4.to_string, - gw: ipv4.hosts.last.to_s, - } + ip: ipv4, gw: gw4, + ip6: ipv6, gw6: gw6, + }.delete_if {|k,v| v.nil? } end def vmid @@ -131,14 +172,13 @@ module PVE::CTTemplate IPAddress::IPv4.new "10.#{network_id}.255.0/24" end - def ipv4 - return options.ipv4 if options.ipv4 - return @ipv4 if @ipv4 + def ipv4_gw + return @ipv4_gw if @ipv4_gw ipv4s = network.hosts @virts.each do |v| v.config[:network].each {|n| ipv4s.delete n[:ip] if n[:ip] } end - @ipv4 = ipv4s.first + @ipv4_gw = [ipv4s.first.to_string, network.last.to_s] end def ostemplate