[pve-devel] [PATCH resend pve-container 1/3] archlinux template support

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Aug 18 16:56:30 CEST 2015


---
 src/PVE/LXC.pm                |  2 +-
 src/PVE/LXCSetup.pm           | 10 +++++---
 src/PVE/LXCSetup/ArchLinux.pm | 54 +++++++++++++++++++++++++++++++++++++++++++
 src/PVE/LXCSetup/Base.pm      | 53 ++++++++++++++++++++++++++++++++++++++++++
 src/PVE/LXCSetup/Makefile     |  2 +-
 5 files changed, 116 insertions(+), 5 deletions(-)
 create mode 100644 src/PVE/LXCSetup/ArchLinux.pm

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 629a41e..5ec5766 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -933,7 +933,7 @@ sub update_lxc_config {
     $raw .= "lxc.arch = $conf->{arch}\n";
 
     my $ostype = $conf->{ostype} || die "missing 'ostype' - internal error";
-    if ($ostype eq 'debian' || $ostype eq 'ubuntu' || $ostype eq 'centos') {
+    if ($ostype =~ /^(?:debian | ubuntu | centos | archlinux)$/x) {
 	$raw .= "lxc.include = /usr/share/lxc/config/$ostype.common.conf\n";
     } else {
 	die "implement me";
diff --git a/src/PVE/LXCSetup.pm b/src/PVE/LXCSetup.pm
index e57ed1a..822ff3d 100644
--- a/src/PVE/LXCSetup.pm
+++ b/src/PVE/LXCSetup.pm
@@ -7,11 +7,13 @@ use PVE::Tools;
 use PVE::LXCSetup::Debian;
 use PVE::LXCSetup::Ubuntu;
 use PVE::LXCSetup::Redhat;
+use PVE::LXCSetup::ArchLinux;
 
 my $plugins = {
-    debian =>  'PVE::LXCSetup::Debian',
-    ubuntu =>  'PVE::LXCSetup::Ubuntu',
-    redhat =>  'PVE::LXCSetup::Redhat',
+    debian    => 'PVE::LXCSetup::Debian',
+    ubuntu    => 'PVE::LXCSetup::Ubuntu',
+    redhat    => 'PVE::LXCSetup::Redhat',
+    archlinux => 'PVE::LXCSetup::ArchLinux',
 };
 
 my $autodetect_type = sub {
@@ -27,6 +29,8 @@ my $autodetect_type = sub {
 	return "debian";
     } elsif (-f  "$rootdir/etc/redhat-release") {
 	return "redhat";
+    } elsif (-f  "$rootdir/etc/arch-release") {
+	return "archlinux";
     }
     die "unable to detect OS disribution\n";
 };
diff --git a/src/PVE/LXCSetup/ArchLinux.pm b/src/PVE/LXCSetup/ArchLinux.pm
new file mode 100644
index 0000000..90a3bf7
--- /dev/null
+++ b/src/PVE/LXCSetup/ArchLinux.pm
@@ -0,0 +1,54 @@
+package PVE::LXCSetup::ArchLinux;
+
+use strict;
+use warnings;
+
+use File::Path 'make_path';
+
+use PVE::LXCSetup::Base;
+
+use base qw(PVE::LXCSetup::Base);
+
+sub new {
+    my ($class, $conf, $rootdir) = @_;
+
+    # /etc/arch-release exists, but it's empty
+    #my $release = PVE::Tools::file_read_firstline("$rootdir/etc/arch-release");
+    #die "unable to read version info\n" if !defined($release);
+
+    my $self = { conf => $conf, rootdir => $rootdir, version => 0 };
+
+    $conf->{ostype} = "archlinux";
+
+    return bless $self, $class;
+}
+
+sub template_fixup {
+    my ($self, $conf) = @_;
+    # ArchLinux doesn't come with any particular predefined and enabled
+    # networking, so it probably makes sense to do the equivalent of
+    # 'systemctl enable systemd-networkd', since that's what we're configuring
+    # in setup_network
+
+    my $rootdir = $self->{rootdir};
+
+    # systemctl enable systemd-networkd
+    make_path("$rootdir/etc/systemd/system/multi-user.target.wants");
+    make_path("$rootdir/etc/systemd/system/socket.target.wants");
+    symlink "/usr/lib/systemd/system/systemd-networkd.service",
+            "$rootdir/etc/systemd/system/multi-user.target.wants/systemd-networkd.service";
+    symlink "/usr/lib/systemd/system/systemd-networkd.socket",
+            "$rootdir/etc/systemd/system/socket.target.wants/systemd-networkd.socket";
+}
+
+sub setup_init {
+    # Nothing to do
+}
+
+sub setup_network {
+    my ($self, $conf) = @_;
+
+    $self->setup_systemd_networkd($conf);
+}
+
+1;
diff --git a/src/PVE/LXCSetup/Base.pm b/src/PVE/LXCSetup/Base.pm
index 49e60e0..6574f07 100644
--- a/src/PVE/LXCSetup/Base.pm
+++ b/src/PVE/LXCSetup/Base.pm
@@ -235,6 +235,59 @@ sub setup_systemd_console {
     }
 }
 
+sub setup_systemd_networkd {
+    my ($self, $conf) = @_;
+
+    my $rootdir = $self->{rootdir};
+
+    foreach my $k (keys %$conf) {
+	next if $k !~ m/^net(\d+)$/;
+	my $d = PVE::LXC::parse_lxc_network($conf->{$k});
+	next if !$d->{name};
+
+	my $filename = "$rootdir/etc/systemd/network/$d->{name}.network";
+
+	my $data = <<"DATA";
+[Match]
+Name = $d->{name}
+
+[Network]
+Description = Interface $d->{name} autoconfigured by PVE
+DATA
+	# DHCP bitflags:
+	my @DHCPMODES = ('none', 'v4', 'v6', 'both');
+	my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
+	my $dhcp = $NONE;
+
+	if (defined(my $ip = $d->{ip})) {
+	    if ($ip eq 'dhcp') {
+		$dhcp |= $DHCP4;
+	    } elsif ($ip ne 'manual') {
+		$data .= "Address = $ip\n";
+	    }
+	}
+	if (defined(my $gw = $d->{gw})) {
+	    $data .= "Gateway = $gw\n";
+	}
+
+	if (defined(my $ip = $d->{ip6})) {
+	    if ($ip eq 'dhcp') {
+		$dhcp |= $DHCP6;
+	    } elsif ($ip ne 'manual') {
+		$data .= "Address = $ip\n";
+	    }
+	}
+	if (defined(my $gw = $d->{gw6})) {
+	    $data .= "Gateway = $gw\n";
+	}
+
+	$data .= "DHCP = $DHCPMODES[$dhcp]\n";
+
+	PVE::Tools::file_set_contents($filename, $data);
+    }
+
+}
+
 my $replacepw  = sub {
     my ($file, $user, $epw, $shadow) = @_;
 
diff --git a/src/PVE/LXCSetup/Makefile b/src/PVE/LXCSetup/Makefile
index 828d89f..ce39b53 100644
--- a/src/PVE/LXCSetup/Makefile
+++ b/src/PVE/LXCSetup/Makefile
@@ -1,4 +1,4 @@
-SOURCES=Base.pm Debian.pm Ubuntu.pm Redhat.pm
+SOURCES=Base.pm Debian.pm Ubuntu.pm Redhat.pm ArchLinux.pm
 
 .PHONY: install
 install:
-- 
2.1.4





More information about the pve-devel mailing list