[pve-devel] [PATCH container] fix #912: centos/redhat confusion

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Mar 9 10:13:00 CET 2016


With the introduction of unmanaged containers a check was
added to verify that the ostype is the same as the
autodetected one.
Since our CentOS plugin was named 'redhat' but the ostype
setting was 'centos' this error triggered and rendered
centos containers unusable.

Renamed the LXC::Setup::Redhat package LXC::Setup::CentOS
and changed the plugin name to and autodetected type from
'redhat' to 'centos' as well.
---
 src/PVE/LXC/Setup.pm        |   8 +-
 src/PVE/LXC/Setup/CentOS.pm | 249 ++++++++++++++++++++++++++++++++++++++++++++
 src/PVE/LXC/Setup/Fedora.pm |   4 +-
 src/PVE/LXC/Setup/Makefile  |   2 +-
 src/PVE/LXC/Setup/Redhat.pm | 249 --------------------------------------------
 5 files changed, 256 insertions(+), 256 deletions(-)
 create mode 100644 src/PVE/LXC/Setup/CentOS.pm
 delete mode 100644 src/PVE/LXC/Setup/Redhat.pm

diff --git a/src/PVE/LXC/Setup.pm b/src/PVE/LXC/Setup.pm
index 3374806..b22f32a 100644
--- a/src/PVE/LXC/Setup.pm
+++ b/src/PVE/LXC/Setup.pm
@@ -7,7 +7,7 @@ use PVE::Tools;
 
 use PVE::LXC::Setup::Debian;
 use PVE::LXC::Setup::Ubuntu;
-use PVE::LXC::Setup::Redhat;
+use PVE::LXC::Setup::CentOS;
 use PVE::LXC::Setup::Fedora;
 use PVE::LXC::Setup::SUSE;
 use PVE::LXC::Setup::ArchLinux;
@@ -16,7 +16,7 @@ use PVE::LXC::Setup::Alpine;
 my $plugins = {
     debian    => 'PVE::LXC::Setup::Debian',
     ubuntu    => 'PVE::LXC::Setup::Ubuntu',
-    redhat    => 'PVE::LXC::Setup::Redhat',
+    centos    => 'PVE::LXC::Setup::CentOS',
     fedora    => 'PVE::LXC::Setup::Fedora',
     opensuse  => 'PVE::LXC::Setup::SUSE',
     archlinux => 'PVE::LXC::Setup::ArchLinux',
@@ -40,8 +40,8 @@ my $autodetect_type = sub {
 	return "opensuse";
     } elsif (-f  "$rootdir/etc/fedora-release") {
 	return "fedora";
-    } elsif (-f  "$rootdir/etc/redhat-release") {
-	return "redhat";
+    } elsif (-f  "$rootdir/etc/centos-release" || -f "$rootdir/etc/redhat-release") {
+	return "centos";
     } elsif (-f  "$rootdir/etc/arch-release") {
 	return "archlinux";
     } elsif (-f  "$rootdir/etc/alpine-release") {
diff --git a/src/PVE/LXC/Setup/CentOS.pm b/src/PVE/LXC/Setup/CentOS.pm
new file mode 100644
index 0000000..be30ca6
--- /dev/null
+++ b/src/PVE/LXC/Setup/CentOS.pm
@@ -0,0 +1,249 @@
+package PVE::LXC::Setup::CentOS;
+
+use strict;
+use warnings;
+use Data::Dumper;
+use PVE::Tools;
+use PVE::Network;
+use PVE::LXC;
+
+use PVE::LXC::Setup::Base;
+
+use base qw(PVE::LXC::Setup::Base);
+
+sub new {
+    my ($class, $conf, $rootdir) = @_;
+
+    my $release = PVE::Tools::file_read_firstline("$rootdir/etc/redhat-release");
+    die "unable to read version info\n" if !defined($release);
+
+    my $version;
+
+    if ($release =~ m/release\s+(\d+\.\d+)(\.\d+)?/) {
+	if ($1 >= 6 && $1 < 8) {
+	    $version = $1;
+	}
+    }
+
+    die "unsupported centos release '$release'\n" if !$version;
+
+    my $self = { conf => $conf, rootdir => $rootdir, version => $version };
+
+    $conf->{ostype} = "centos";
+
+    return bless $self, $class;
+}
+
+my $tty_conf = <<__EOD__;
+# tty - getty
+#
+# This service maintains a getty on the specified device.
+#
+# Do not edit this file directly. If you want to change the behaviour,
+# please create a file tty.override and put your changes there.
+
+stop on runlevel [S016]
+
+respawn
+instance \$TTY
+exec /sbin/mingetty \$TTY
+usage 'tty TTY=/dev/ttyX  - where X is console id'
+__EOD__
+    
+my $start_ttys_conf = <<__EOD__;
+#
+# This service starts the configured number of gettys.
+#
+# Do not edit this file directly. If you want to change the behaviour,
+# please create a file start-ttys.override and put your changes there.
+
+start on stopped rc RUNLEVEL=[2345]
+
+env ACTIVE_CONSOLES=/dev/tty[1-6]
+env X_TTY=/dev/tty1
+task
+script
+        . /etc/sysconfig/init
+        for tty in \$(echo \$ACTIVE_CONSOLES) ; do
+                [ "\$RUNLEVEL" = "5" -a "\$tty" = "\$X_TTY" ] && continue
+                initctl start tty TTY=\$tty
+        done
+end script
+__EOD__
+
+my $power_status_changed_conf = <<__EOD__;
+#  power-status-changed - shutdown on SIGPWR
+#
+start on power-status-changed
+    
+exec /sbin/shutdown -h now "SIGPWR received"
+__EOD__
+
+sub template_fixup {
+    my ($self, $conf) = @_;
+
+    if ($self->{version} < 7) {
+	# re-create emissing files for tty
+
+	$self->ct_make_path('/etc/init');
+
+	my $filename = "/etc/init/tty.conf";
+	$self->ct_file_set_contents($filename, $tty_conf)
+	    if ! $self->ct_file_exists($filename);
+
+	$filename = "/etc/init/start-ttys.conf";
+	$self->ct_file_set_contents($filename, $start_ttys_conf)
+	    if ! $self->ct_file_exists($filename);
+
+	$filename = "/etc/init/power-status-changed.conf";
+	$self->ct_file_set_contents($filename, $power_status_changed_conf)
+	    if ! $self->ct_file_exists($filename);
+
+	# do not start udevd
+	$filename = "/etc/rc.d/rc.sysinit";
+	my $data = $self->ct_file_get_contents($filename);
+	$data =~ s!^(/sbin/start_udev.*)$!#$1!gm;
+	$self->ct_file_set_contents($filename, $data);
+	
+	# edit /etc/securetty (enable login on console)
+	$self->setup_securetty($conf, qw(lxc/console lxc/tty1 lxc/tty2 lxc/tty3 lxc/tty4));
+    }
+}
+
+sub setup_init {
+    my ($self, $conf) = @_;
+
+     # edit/etc/securetty
+
+    $self->setup_systemd_console($conf);
+}
+
+sub set_hostname {
+    my ($self, $conf) = @_;
+
+    # Redhat wants the fqdn in /etc/sysconfig/network's HOSTNAME
+    my $hostname = $conf->{hostname} || 'localhost';
+
+    my $hostname_fn = "/etc/hostname";
+    my $sysconfig_network = "/etc/sysconfig/network";
+
+    my $oldname;
+    if ($self->ct_file_exists($hostname_fn)) {
+	$oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
+    } else {
+	my $data = $self->ct_file_get_contents($sysconfig_network);
+	if ($data =~ m/^HOSTNAME=\s*(\S+)\s*$/m) {
+	    $oldname = $1;
+	}
+    }
+
+    my $hosts_fn = "/etc/hosts";
+
+    my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
+    my $hostip = $ipv4 || $ipv6;
+
+    my ($searchdomains) = $self->lookup_dns_conf($conf);
+
+    $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
+
+    if ($self->ct_file_exists($hostname_fn)) {
+	$self->ct_file_set_contents($hostname_fn, "$hostname\n");
+    }
+
+    if ($self->ct_file_exists($sysconfig_network)) {
+	my $data = $self->ct_file_get_contents($sysconfig_network);
+	if ($data !~ s/^HOSTNAME=\h*(\S+)\h*$/HOSTNAME=$hostname/m) {
+	    $data .= "HOSTNAME=$hostname\n";
+	}
+	$self->ct_file_set_contents($sysconfig_network, $data);
+    }
+}
+
+sub setup_network {
+    my ($self, $conf) = @_;
+
+    my ($gw, $gw6);
+
+    $self->ct_make_path('/etc/sysconfig/network-scripts');
+
+    my ($has_ipv4, $has_ipv6);
+
+    foreach my $k (keys %$conf) {
+	next if $k !~ m/^net(\d+)$/;
+	my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
+	next if !$d->{name};
+	$has_ipv4 = 1 if defined($d->{ip});
+	$has_ipv6 = 1 if defined($d->{ip6});
+
+	my $filename = "/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
+	my $routefile = "/etc/sysconfig/network-scripts/route-$d->{name}";
+	my $routes = '';
+
+	my $header = "DEVICE=$d->{name}\nONBOOT=yes\n";
+	my $data = '';
+	my $bootproto = '';
+
+	if ($d->{ip} && $d->{ip} ne 'manual') {
+	    if ($d->{ip} eq 'dhcp') {
+		$bootproto = 'dhcp';
+	    } else {
+		$bootproto = 'none';
+		my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip});
+		$data .= "IPADDR=$ipinfo->{address}\n";
+		$data .= "NETMASK=$ipinfo->{netmask}\n";
+		if (defined($d->{gw})) {
+		    $data .= "GATEWAY=$d->{gw}\n";
+		    if (!PVE::Network::is_ip_in_cidr($d->{gw}, $d->{ip}, 4)) {
+			$routes .= "$d->{gw} dev $d->{name}\n";
+			$routes .= "default via $d->{gw}\n";
+		    }
+		}
+	    }
+	}
+
+	if ($d->{ip6} && $d->{ip6} ne 'manual') {
+	    $bootproto = 'none' if !$bootproto;
+	    $data .= "IPV6INIT=yes\n";
+	    if ($d->{ip6} eq 'auto') {
+		$data .= "IPV6_AUTOCONF=yes\n";
+	    } else {
+		$data .= "IPV6_AUTOCONF=no\n";
+	    }
+	    if ($d->{ip6} eq 'dhcp') {
+		$data .= "DHCPV6C=yes\n";
+	    } else {
+		$data .= "IPV6ADDR=$d->{ip6}\n";
+		if (defined($d->{gw6})) {
+		    $data .= "IPV6_DEFAULTGW=$d->{gw6}\n";
+		    if (!PVE::Network::is_ip_in_cidr($d->{gw6}, $d->{ip6}, 6)) {
+			$routes .= "$d->{gw6} dev $d->{name}\n";
+			$routes .= "default via $d->{gw6}\n";
+		    }
+		}
+	    }
+	}
+
+	next unless $data || $bootproto;
+	$header .= "BOOTPROTO=$bootproto\n";
+	$self->ct_file_set_contents($filename, $header . $data);
+	$self->ct_modify_file($routefile, $routes, delete => 1, prepend => 1);
+    }
+
+    my $sysconfig_network = "/etc/sysconfig/network";
+    if ($self->ct_file_exists($sysconfig_network)) {
+	my $data = $self->ct_file_get_contents($sysconfig_network);
+	if ($has_ipv4) {
+	    if ($data !~ s/(NETWORKING)=\S+/$1=yes/) {
+		$data .= "NETWORKING=yes\n";
+	    }
+	}
+	if ($has_ipv6) {
+	    if ($data !~ s/(NETWORKING_IPV6)=\S+/$1=yes/) {
+		$data .= "NETWORKING_IPV6=yes\n";
+	    }
+	}
+	$self->ct_file_set_contents($sysconfig_network, $data);
+    }
+}
+
+1;
diff --git a/src/PVE/LXC/Setup/Fedora.pm b/src/PVE/LXC/Setup/Fedora.pm
index 2507e26..ff60b10 100644
--- a/src/PVE/LXC/Setup/Fedora.pm
+++ b/src/PVE/LXC/Setup/Fedora.pm
@@ -3,9 +3,9 @@ package PVE::LXC::Setup::Fedora;
 use strict;
 use warnings;
 
-use PVE::LXC::Setup::Redhat;
+use PVE::LXC::Setup::CentOS;
 
-use base qw(PVE::LXC::Setup::Redhat);
+use base qw(PVE::LXC::Setup::CentOS);
 
 sub new {
     my ($class, $conf, $rootdir) = @_;
diff --git a/src/PVE/LXC/Setup/Makefile b/src/PVE/LXC/Setup/Makefile
index c12f1a3..f70c898 100644
--- a/src/PVE/LXC/Setup/Makefile
+++ b/src/PVE/LXC/Setup/Makefile
@@ -1,4 +1,4 @@
-SOURCES=Base.pm Debian.pm Ubuntu.pm Redhat.pm Fedora.pm SUSE.pm ArchLinux.pm Alpine.pm
+SOURCES=Base.pm Debian.pm Ubuntu.pm CentOS.pm Fedora.pm SUSE.pm ArchLinux.pm Alpine.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/LXC/Setup/Redhat.pm b/src/PVE/LXC/Setup/Redhat.pm
deleted file mode 100644
index 2319411..0000000
--- a/src/PVE/LXC/Setup/Redhat.pm
+++ /dev/null
@@ -1,249 +0,0 @@
-package PVE::LXC::Setup::Redhat;
-
-use strict;
-use warnings;
-use Data::Dumper;
-use PVE::Tools;
-use PVE::Network;
-use PVE::LXC;
-
-use PVE::LXC::Setup::Base;
-
-use base qw(PVE::LXC::Setup::Base);
-
-sub new {
-    my ($class, $conf, $rootdir) = @_;
-
-    my $release = PVE::Tools::file_read_firstline("$rootdir/etc/redhat-release");
-    die "unable to read version info\n" if !defined($release);
-
-    my $version;
-
-    if ($release =~ m/release\s+(\d+\.\d+)(\.\d+)?/) {
-	if ($1 >= 6 && $1 < 8) {
-	    $version = $1;
-	}
-    }
-
-    die "unsupported redhat release '$release'\n" if !$version;
-
-    my $self = { conf => $conf, rootdir => $rootdir, version => $version };
-
-    $conf->{ostype} = "centos";
-
-    return bless $self, $class;
-}
-
-my $tty_conf = <<__EOD__;
-# tty - getty
-#
-# This service maintains a getty on the specified device.
-#
-# Do not edit this file directly. If you want to change the behaviour,
-# please create a file tty.override and put your changes there.
-
-stop on runlevel [S016]
-
-respawn
-instance \$TTY
-exec /sbin/mingetty \$TTY
-usage 'tty TTY=/dev/ttyX  - where X is console id'
-__EOD__
-    
-my $start_ttys_conf = <<__EOD__;
-#
-# This service starts the configured number of gettys.
-#
-# Do not edit this file directly. If you want to change the behaviour,
-# please create a file start-ttys.override and put your changes there.
-
-start on stopped rc RUNLEVEL=[2345]
-
-env ACTIVE_CONSOLES=/dev/tty[1-6]
-env X_TTY=/dev/tty1
-task
-script
-        . /etc/sysconfig/init
-        for tty in \$(echo \$ACTIVE_CONSOLES) ; do
-                [ "\$RUNLEVEL" = "5" -a "\$tty" = "\$X_TTY" ] && continue
-                initctl start tty TTY=\$tty
-        done
-end script
-__EOD__
-
-my $power_status_changed_conf = <<__EOD__;
-#  power-status-changed - shutdown on SIGPWR
-#
-start on power-status-changed
-    
-exec /sbin/shutdown -h now "SIGPWR received"
-__EOD__
-
-sub template_fixup {
-    my ($self, $conf) = @_;
-
-    if ($self->{version} < 7) {
-	# re-create emissing files for tty
-
-	$self->ct_make_path('/etc/init');
-
-	my $filename = "/etc/init/tty.conf";
-	$self->ct_file_set_contents($filename, $tty_conf)
-	    if ! $self->ct_file_exists($filename);
-
-	$filename = "/etc/init/start-ttys.conf";
-	$self->ct_file_set_contents($filename, $start_ttys_conf)
-	    if ! $self->ct_file_exists($filename);
-
-	$filename = "/etc/init/power-status-changed.conf";
-	$self->ct_file_set_contents($filename, $power_status_changed_conf)
-	    if ! $self->ct_file_exists($filename);
-
-	# do not start udevd
-	$filename = "/etc/rc.d/rc.sysinit";
-	my $data = $self->ct_file_get_contents($filename);
-	$data =~ s!^(/sbin/start_udev.*)$!#$1!gm;
-	$self->ct_file_set_contents($filename, $data);
-	
-	# edit /etc/securetty (enable login on console)
-	$self->setup_securetty($conf, qw(lxc/console lxc/tty1 lxc/tty2 lxc/tty3 lxc/tty4));
-    }
-}
-
-sub setup_init {
-    my ($self, $conf) = @_;
-
-     # edit/etc/securetty
-
-    $self->setup_systemd_console($conf);
-}
-
-sub set_hostname {
-    my ($self, $conf) = @_;
-
-    # Redhat wants the fqdn in /etc/sysconfig/network's HOSTNAME
-    my $hostname = $conf->{hostname} || 'localhost';
-
-    my $hostname_fn = "/etc/hostname";
-    my $sysconfig_network = "/etc/sysconfig/network";
-
-    my $oldname;
-    if ($self->ct_file_exists($hostname_fn)) {
-	$oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
-    } else {
-	my $data = $self->ct_file_get_contents($sysconfig_network);
-	if ($data =~ m/^HOSTNAME=\s*(\S+)\s*$/m) {
-	    $oldname = $1;
-	}
-    }
-
-    my $hosts_fn = "/etc/hosts";
-
-    my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
-    my $hostip = $ipv4 || $ipv6;
-
-    my ($searchdomains) = $self->lookup_dns_conf($conf);
-
-    $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
-
-    if ($self->ct_file_exists($hostname_fn)) {
-	$self->ct_file_set_contents($hostname_fn, "$hostname\n");
-    }
-
-    if ($self->ct_file_exists($sysconfig_network)) {
-	my $data = $self->ct_file_get_contents($sysconfig_network);
-	if ($data !~ s/^HOSTNAME=\h*(\S+)\h*$/HOSTNAME=$hostname/m) {
-	    $data .= "HOSTNAME=$hostname\n";
-	}
-	$self->ct_file_set_contents($sysconfig_network, $data);
-    }
-}
-
-sub setup_network {
-    my ($self, $conf) = @_;
-
-    my ($gw, $gw6);
-
-    $self->ct_make_path('/etc/sysconfig/network-scripts');
-
-    my ($has_ipv4, $has_ipv6);
-
-    foreach my $k (keys %$conf) {
-	next if $k !~ m/^net(\d+)$/;
-	my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
-	next if !$d->{name};
-	$has_ipv4 = 1 if defined($d->{ip});
-	$has_ipv6 = 1 if defined($d->{ip6});
-
-	my $filename = "/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
-	my $routefile = "/etc/sysconfig/network-scripts/route-$d->{name}";
-	my $routes = '';
-
-	my $header = "DEVICE=$d->{name}\nONBOOT=yes\n";
-	my $data = '';
-	my $bootproto = '';
-
-	if ($d->{ip} && $d->{ip} ne 'manual') {
-	    if ($d->{ip} eq 'dhcp') {
-		$bootproto = 'dhcp';
-	    } else {
-		$bootproto = 'none';
-		my $ipinfo = PVE::LXC::parse_ipv4_cidr($d->{ip});
-		$data .= "IPADDR=$ipinfo->{address}\n";
-		$data .= "NETMASK=$ipinfo->{netmask}\n";
-		if (defined($d->{gw})) {
-		    $data .= "GATEWAY=$d->{gw}\n";
-		    if (!PVE::Network::is_ip_in_cidr($d->{gw}, $d->{ip}, 4)) {
-			$routes .= "$d->{gw} dev $d->{name}\n";
-			$routes .= "default via $d->{gw}\n";
-		    }
-		}
-	    }
-	}
-
-	if ($d->{ip6} && $d->{ip6} ne 'manual') {
-	    $bootproto = 'none' if !$bootproto;
-	    $data .= "IPV6INIT=yes\n";
-	    if ($d->{ip6} eq 'auto') {
-		$data .= "IPV6_AUTOCONF=yes\n";
-	    } else {
-		$data .= "IPV6_AUTOCONF=no\n";
-	    }
-	    if ($d->{ip6} eq 'dhcp') {
-		$data .= "DHCPV6C=yes\n";
-	    } else {
-		$data .= "IPV6ADDR=$d->{ip6}\n";
-		if (defined($d->{gw6})) {
-		    $data .= "IPV6_DEFAULTGW=$d->{gw6}\n";
-		    if (!PVE::Network::is_ip_in_cidr($d->{gw6}, $d->{ip6}, 6)) {
-			$routes .= "$d->{gw6} dev $d->{name}\n";
-			$routes .= "default via $d->{gw6}\n";
-		    }
-		}
-	    }
-	}
-
-	next unless $data || $bootproto;
-	$header .= "BOOTPROTO=$bootproto\n";
-	$self->ct_file_set_contents($filename, $header . $data);
-	$self->ct_modify_file($routefile, $routes, delete => 1, prepend => 1);
-    }
-
-    my $sysconfig_network = "/etc/sysconfig/network";
-    if ($self->ct_file_exists($sysconfig_network)) {
-	my $data = $self->ct_file_get_contents($sysconfig_network);
-	if ($has_ipv4) {
-	    if ($data !~ s/(NETWORKING)=\S+/$1=yes/) {
-		$data .= "NETWORKING=yes\n";
-	    }
-	}
-	if ($has_ipv6) {
-	    if ($data !~ s/(NETWORKING_IPV6)=\S+/$1=yes/) {
-		$data .= "NETWORKING_IPV6=yes\n";
-	    }
-	}
-	$self->ct_file_set_contents($sysconfig_network, $data);
-    }
-}
-
-1;
-- 
2.1.4





More information about the pve-devel mailing list