[pve-devel] [PATCH pve-container] update inittab instead of replacing it

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Oct 14 14:48:27 CEST 2015


---
 src/PVE/LXC/Setup/Base.pm   |  6 ++---
 src/PVE/LXC/Setup/Debian.pm | 61 ++++++++++++++++++++++++++++++++++-----------
 2 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/src/PVE/LXC/Setup/Base.pm b/src/PVE/LXC/Setup/Base.pm
index ba7453d..9a39468 100644
--- a/src/PVE/LXC/Setup/Base.pm
+++ b/src/PVE/LXC/Setup/Base.pm
@@ -191,7 +191,7 @@ sub setup_network {
 }
 
 sub setup_init {
-    my ($self, $conf) = @_;
+    my ($self, $conf, $creation) = @_;
 
     die "please implement this inside subclass"
 }
@@ -454,7 +454,7 @@ sub rewrite_ssh_host_keys {
 sub pre_start_hook {
     my ($self, $conf) = @_;
 
-    $self->setup_init($conf);
+    $self->setup_init($conf, 0);
     $self->setup_network($conf);
     $self->set_hostname($conf);
     $self->set_dns($conf);
@@ -470,7 +470,7 @@ sub post_create_hook {
     &$randomize_crontab($self, $conf);
     
     $self->set_user_password($conf, 'root', $root_password);
-    $self->setup_init($conf);
+    $self->setup_init($conf, 1);
     $self->setup_network($conf);
     $self->set_hostname($conf);
     $self->set_dns($conf);
diff --git a/src/PVE/LXC/Setup/Debian.pm b/src/PVE/LXC/Setup/Debian.pm
index 1b2a277..068d155 100644
--- a/src/PVE/LXC/Setup/Debian.pm
+++ b/src/PVE/LXC/Setup/Debian.pm
@@ -77,26 +77,57 @@ p0::powerfail:/sbin/init 0
 __EOD__
 
 sub setup_init {
-    my ($self, $conf) = @_;
+    my ($self, $conf, $creation) = @_;
 
     my $filename = "/etc/inittab";
+    return if !$self->ct_file_exists($filename);
 
-    if ($self->ct_file_exists($filename)) {
-	my $inittab = $default_inittab;
-
-	my $ttycount =  PVE::LXC::get_tty_count($conf);
-	for (my $i = 1; $i <= $ttycount; $i++) {
-	    next if $i == 7; # reserved for X11
-	    my $levels = ($i == 1) ? '2345' : '23';
-	    if ($self->{version} < 7) {
-		$inittab .= "$i:$levels:respawn:/sbin/getty -L 38400 tty$i\n";
-	    } else {
-		$inittab .= "$i:$levels:respawn:/sbin/getty --noclear 38400 tty$i\n";
-	    }
+    my $ttycount =  PVE::LXC::get_tty_count($conf);
+    my $inittab = $creation ? $default_inittab : $self->ct_file_get_contents($filename);
+
+    my %tty_done;
+    my $linecount = 0;
+    my $last_tty_line;
+
+    # For every getty line enable/disable it via a comment tag ('#') depending
+    # on whether its id is within the ttycount range.
+    # Afterwards, for every new tty for which no tty entry line was found
+    # generate a new one.
+    my $process_line = sub {
+	my ($line) = @_;
+	++$linecount;
+
+	# Check for a getty line:
+	return $line if $line !~ /^\s*#?\s*(\d+):(\d+):([^:]*):(.*getty.*)$/;
+	my ($id, $runlevels, $action, $process) = ($1, $2, $3, $4);
+
+	# Remember the linenumber so we can insert new getty entries grouped
+	# together with the entries already in the file.
+	$last_tty_line = $linecount;
+	return $line if $id <= 0; # sanity check
+
+	$tty_done{$id} = 1; # remember it's already available
+
+	my $disabled = $id > $ttycount ? '#' : '';
+	return "${disabled}$id:$runlevels:$action:$process";
+    };
+    my @lines = map { &$process_line($_) } split(/\n/, $inittab);
+    $last_tty_line ||= scalar(@lines);
+
+    my $version = $self->{version};
+    for my $id (1..$ttycount) {
+	next if $tty_done{$id};
+	my $levels = ($id == 1) ? '2345' : '23';
+	if ($version < 7) {
+	    splice @lines, $last_tty_line++, 0, "$id:$levels:respawn:/sbin/getty -L 38400 tty$id";
+	} else {
+	    splice @lines, $last_tty_line++, 0, "$id:$levels:respawn:/sbin/getty --noclear 38400 tty$id";
 	}
-	
-	$self->ct_file_set_contents($filename, $inittab);
     }
+
+    $inittab = join("\n", @lines) . "\n";
+
+    $self->ct_file_set_contents($filename, $inittab);
 }
 
 sub setup_network {
-- 
2.1.4





More information about the pve-devel mailing list