[pve-devel] [PATCH 5/5] generate_cloudinit_network

Alexandre Derumier aderumier at odiso.com
Wed Jun 10 17:10:13 CEST 2015


the config is in debian|ubuntu format,
but it's translated by cloud-init client to other distro config if needed

improve me: add ipv6 support

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer.pm | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 86 insertions(+), 2 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 790aa7e..26308bf 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -23,13 +23,14 @@ use PVE::SafeSyslog;
 use Storable qw(dclone);
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::Storage;
-use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach);
+use PVE::Tools qw(run_command lock_file lock_file_full file_read_firstline dir_glob_foreach $IPV6RE $IPV4RE);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::INotify;
 use PVE::ProcFSTools;
 use PVE::QMPClient;
 use PVE::RPCEnvironment;
+
 use Time::HiRes qw(gettimeofday);
 
 my $qemu_snap_storage = {rbd => 1, sheepdog => 1};
@@ -406,6 +407,11 @@ EODESCR
         type => 'string',
         description => "Ip address cidr format",
     },
+    gateway => {
+        optional => 1,
+        type => 'string',
+        description => "Default gateway ip address",
+    },
     cloudinit => {
 	optional => 1,
 	type => 'boolean',
@@ -6288,6 +6294,7 @@ sub generate_cloudinitconfig {
     mkdir "$path/drive/openstack/content";
     generate_cloudinit_userdata($conf, $path);
     generate_cloudinit_metadata($conf, $path);
+    generate_cloudinit_network($conf, $path);
 
     my $cmd = [];
     push @$cmd, 'genisoimage';
@@ -6350,7 +6357,8 @@ sub generate_cloudinit_metadata {
     }
 
     my $content = "{\n";   
-    $content .= "     \"uuid\": \"$uuid_str\"\n";
+    $content .= "     \"uuid\": \"$uuid_str\",\n";
+    $content .= "     \"network_config\" :{ \"content_path\": \"/content/0000\"}\n";
     $content .= "}\n";   
 
     my $fn = "$path/drive/openstack/latest/meta_data.json";
@@ -6360,4 +6368,80 @@ sub generate_cloudinit_metadata {
 
 }
 
+sub generate_cloudinit_network {
+    my ($conf, $path) = @_;
+
+    my $ip = parse_ipv4_cidr($conf->{ipaddress});
+
+    my $content = "auto lo\n";
+    $content .="iface lo inet loopback\n\n";
+    $content .="auto eth0\n";
+    if($conf->{ipaddress}){
+	$content .="iface eth0 inet static\n";
+	$content .="        address $ip->{address}\n";
+	$content .="        netmask $ip->{netmask}\n";
+	$content .="        gateway $conf->{gateway}\n" if $conf->{gateway};
+    }else{
+	$content .="iface eth0 inet dhcp\n";
+    }
+
+    $content .="        dns-nameservers $conf->{nameserver}\n" if $conf->{nameserver};
+    $content .="        dns-search $conf->{searchdomain}\n" if $conf->{searchdomain};
+
+    my $fn = "$path/drive/openstack/content/0000";
+    file_write($fn, $content);
+
+}
+
+
+my $ipv4_reverse_mask = [
+    '0.0.0.0',
+    '128.0.0.0',
+    '192.0.0.0',
+    '224.0.0.0',
+    '240.0.0.0',
+    '248.0.0.0',
+    '252.0.0.0',
+    '254.0.0.0',
+    '255.0.0.0',
+    '255.128.0.0',
+    '255.192.0.0',
+    '255.224.0.0',
+    '255.240.0.0',
+    '255.248.0.0',
+    '255.252.0.0',
+    '255.254.0.0',
+    '255.255.0.0',
+    '255.255.128.0',
+    '255.255.192.0',
+    '255.255.224.0',
+    '255.255.240.0',
+    '255.255.248.0',
+    '255.255.252.0',
+    '255.255.254.0',
+    '255.255.255.0',
+    '255.255.255.128',
+    '255.255.255.192',
+    '255.255.255.224',
+    '255.255.255.240',
+    '255.255.255.248',
+    '255.255.255.252',
+    '255.255.255.254',
+    '255.255.255.255',
+];
+
+# Note: we cannot use Net:IP, because that only allows strict 
+# CIDR networks
+sub parse_ipv4_cidr {
+    my ($cidr, $noerr) = @_;
+
+    if ($cidr =~ m!^($IPV4RE)(?:/(\d+))$! && ($2 > 7) &&  ($2 < 32)) {
+        return { address => $1, netmask => $ipv4_reverse_mask->[$2] };
+    }
+
+    return undef if $noerr;
+
+    die "unable to parse ipv4 address/mask\n";
+}
+
 1;
-- 
2.1.4




More information about the pve-devel mailing list