Skip to contentSkip to conversation

Fix Debian HVM ssh ECONNREFUSED race

PATCH In review Ian Jackson 2 participants 16 messages Sep 29, 2015

lore
Share & tools
lore
#0 lore raw

cover Fix Debian HVM ssh ECONNREFUSED race

This race seems actually to exist in principle in all of our hosts and guests.

The first patch nobbles it for all installs based on preseeding, by overwriting the unnecessary and problematic hook script in our overlay.

The remaining patches arrange to install the overlay in ts-debian-fixup, which is called for installs done with xen-create-image rather than d-i.

#1 lore raw

PATCH 1/7 Debian installs: Nobble /etc/network/if-up.d/openssh-server

(See the comment in the new file for the explanation.)

This change affects all our Debian installs (both hosts and guests) which are done with preseeding, because preseed_base() arranges to install overlay/.

---
 overlay/etc/network/if-up.d/openssh-server |   32 ++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100755 overlay/etc/network/if-up.d/openssh-server
overlay/etc/network/if-up.d/openssh-server new file +32 -0
new file mode 100755
index 0000000..9fe2faf
@@ -0,0 +1,32 @@
+#!/bin/sh
+exit 0
+
+# In a default Debian install, this script reloads (or, in some
+# versions of Debian, restarts) sshd as new network interfaces come
+# up.  This is in case you have specific listen addresses specified in
+# the config.
+#
+# But the default config listens on 0.0.0.0 and ::.  So sshd is active
+# as soon as an interface is up, and does not need to be restarted or
+# reloaded.
+#
+# This restarting or reloading is harmful because it causes ssh to
+# stop listening briefly.  We can see the following race:
+#
+#  target sshd       target dhcp client     osstest controller
+#
+#   starts            starts
+#   binds to ANY      obtains lease
+#                     configures eth0
+#                                            connects to :22 with nc
+#   accepts conn                             nc succeeds
+#                                            decides target sshd is up
+#                     runs ifup hook
+#                     ifup hook reloads
+#
+#   gets SIGHUP
+#   closes listen socket
+#   rereads config                           runs ssh root@target
+#                                            ssh gets ECONNREFUSED
+#   opens new listen socket
+#                                            declares test fail
Signature · 1 line
1.7.10.4
Acked-by on v1 1/7
#2 lore raw

Re: [OSSTEST PATCH 1/7] Debian installs: Nobble /etc/network/if-up.d/openssh-server

Quoted 7 lines · Ian Jackson

(See the comment in the new file for the explanation.)

This change affects all our Debian installs (both hosts and guests) which are done with preseeding, because preseed_base() arranges to install overlay/.

#3 lore raw

PATCH 2/7 TestSupport::open_unique_stashfile: Provide a RDWR filehandle

In reply to Ian Jackson

The caller can then rewind and reread it if they feel like.

---
 Osstest/TestSupport.pm |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Osstest/TestSupport.pm +1 -1
index 2b67e32..7b9d815 100644
@@ -1005,7 +1005,7 @@ sub open_unique_stashfile ($) {
     my $dh;
     for (;;) {
         my $df= $$leafref;
-        $dh= new IO::File "$stash/$df", O_WRONLY|O_EXCL|O_CREAT;
+        $dh= new IO::File "$stash/$df", O_RDWR|O_EXCL|O_CREAT;
         last if $dh;
         die "$df $!" unless $!==&EEXIST;
         next_unique_name $leafref;
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 3/7 TestSupport: Honour $stdin fh argument to cmd, tcmd and tcmdex

In reply to Ian Jackson

These are internal functions, so the change is entirely within TestSupport. All the call sites are adjusted to pass undef so there is no functional change.

---
 Osstest/TestSupport.pm |   27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
Osstest/TestSupport.pm +16 -11
index 7b9d815..91829a0 100644
@@ -323,9 +323,13 @@ sub target_adjust_timeout ($$) {
 #---------- running commands eg on targets ----------
 
 sub cmd {
-    my ($timeout,$stdout,@cmd) = @_;
+    my ($timeout,$stdin,$stdout,@cmd) = @_;
     my $child= fork;  die $! unless defined $child;
     if (!$child) {
+        if (defined $stdin) {
+            open STDIN, '<&', $stdin
+                or die "STDIN $stdin $cmd[0] $!";
+        }
         if (defined $stdout) {
             open STDOUT, '>&', $stdout
                 or die "STDOUT $stdout $cmd[0] $!";
@@ -395,19 +399,20 @@ sub sshopts () {
 }
 
 sub tcmdex {
-    my ($timeout,$stdout,$cmd,$optsref,@args) = @_;
+    my ($timeout,$stdin,$stdout,$cmd,$optsref,@args) = @_;
     logm("executing $cmd ... @args");
     # We use timeout(1) as a backstop, in case $cmd doesn't die.  We
     # need $cmd to die because we won't release the resources we own
     # until all of our children are dead.
-    my $r= cmd($timeout,$stdout, 'timeout',$timeout+30, $cmd,@$optsref,@args);
+    my $r= cmd($timeout,$stdin,$stdout,
+	       'timeout',$timeout+30, $cmd,@$optsref,@args);
     $r and die "status $r";
 }
 
 sub tgetfileex {
     my ($ruser, $ho,$timeout, $rsrc,$ldst) = @_;
     unlink $ldst or $!==&ENOENT or die "$ldst $!";
-    tcmdex($timeout,undef,
+    tcmdex($timeout,undef,undef,
            'scp', sshopts(),
            sshuho($ruser,$ho).":$rsrc", $ldst);
 } 
@@ -424,12 +429,12 @@ sub tputfileex {
     my ($ruser, $ho,$timeout, $lsrc,$rdst, $rsync) = @_;
     my @args= ($lsrc, sshuho($ruser,$ho).":$rdst");
     if (!defined $rsync) {
-        tcmdex($timeout,undef,
+        tcmdex($timeout,undef,undef,
                'scp', sshopts(),
                @args);
     } else {
         unshift @args, $rsync if length $rsync;
-        tcmdex($timeout,undef,
+        tcmdex($timeout,undef,undef,
                'rsync', [ '-e', 'ssh '.join(' ',@{ sshopts() }) ],
                @args);
     }
@@ -625,19 +630,19 @@ sub target_await_down ($$) {
 }    
 
 sub tcmd { # $tcmd will be put between '' but not escaped
-    my ($stdout,$user,$ho,$tcmd,$timeout,$extrasshopts) = @_;
+    my ($stdin,$stdout,$user,$ho,$tcmd,$timeout,$extrasshopts) = @_;
     $timeout=30 if !defined $timeout;
     target_adjust_timeout($ho,\$timeout);
-    tcmdex($timeout,$stdout,
+    tcmdex($timeout,$stdin,$stdout,
            'ssh', sshopts(), @{ $extrasshopts || [] },
            sshuho($user,$ho), $tcmd);
 }
-sub target_cmd ($$;$$) { tcmd(undef,'osstest',@_); }
-sub target_cmd_root ($$;$$) { tcmd(undef,'root',@_); }
+sub target_cmd ($$;$$) { tcmd(undef,undef,'osstest',@_); }
+sub target_cmd_root ($$;$$) { tcmd(undef,undef,'root',@_); }
 
 sub tcmdout {
     my $stdout= IO::File::new_tmpfile();
-    tcmd($stdout,@_);
+    tcmd(undef,$stdout,@_);
     $stdout->seek(0,0) or die "$stdout $!";
     my $r;
     { local ($/) = undef;
Signature · 1 line
1.7.10.4
#7 lore raw

PATCH 4/7 TestSupport: Provide target_cmd_inputfh_root

In reply to Ian Jackson

No caller yet.

---
 Osstest/TestSupport.pm |    6 ++++++
 1 file changed, 6 insertions(+)
Osstest/TestSupport.pm +6 -0
index 91829a0..3fc8e15 100644
@@ -50,6 +50,7 @@ BEGIN {
 
                       target_cmd_root target_cmd target_cmd_build
                       target_cmd_output_root target_cmd_output
+                      target_cmd_inputfh_root
                       target_getfile target_getfile_root
                       target_putfile target_putfile_root
                       target_putfilecontents_stash
@@ -655,6 +656,11 @@ sub tcmdout {
 sub target_cmd_output ($$;$) { tcmdout('osstest',@_); }
 sub target_cmd_output_root ($$;$) { tcmdout('root',@_); }
 
+sub target_cmd_inputfh_root ($$$;$$) {
+    my ($tho,$stdinfh,$tcmd,@rest) = @_;
+    tcmd($stdinfh,undef,'root',$tho,$tcmd,@rest);
+}
+
 sub poll_loop ($$$&) {
     my ($maxwait, $interval, $what, $code) = @_;
     # $code should return undef when all is well
Signature · 1 line
1.7.10.4
Acked-by on v1 4/7
#8 lore raw

Re: [OSSTEST PATCH 4/7] TestSupport: Provide target_cmd_inputfh_root

Quoted 3 lines · Ian Jackson

No caller yet.

Quoted 28 lines
---
 Osstest/TestSupport.pm |    6 ++++++
 1 file changed, 6 insertions(+)
Osstest/TestSupport.pm +6 -0
index 91829a0..3fc8e15 100644
@@ -50,6 +50,7 @@ BEGIN {
 
                       target_cmd_root target_cmd target_cmd_build
                       target_cmd_output_root target_cmd_output
+                      target_cmd_inputfh_root
                       target_getfile target_getfile_root
                       target_putfile target_putfile_root
                       target_putfilecontents_stash
@@ -655,6 +656,11 @@ sub tcmdout {
 sub target_cmd_output ($$;$) { tcmdout('osstest',@_); }
 sub target_cmd_output_root ($$;$) { tcmdout('root',@_); }
 
+sub target_cmd_inputfh_root ($$$;$$) {
+    my ($tho,$stdinfh,$tcmd,@rest) = @_;
+    tcmd($stdinfh,undef,'root',$tho,$tcmd,@rest);
+}
+
 sub poll_loop ($$$&) {
     my ($maxwait, $interval, $what, $code) = @_;
     # $code should return undef when all is well
#9 lore raw

PATCH 5/7 Debian preseed: Break out debian_overlays

In reply to Ian Jackson

We are going to want to handle the overlays elswhere too, so factor out the iteration over them.

---
 Osstest/Debian.pm |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
Osstest/Debian.pm +12 -2
index a158f34..47d1767 100644
@@ -34,6 +34,7 @@ BEGIN {
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
     @EXPORT      = qw(debian_boot_setup
+                      debian_overlays
                       %preseed_cmds
                       preseed_base
                       preseed_create
@@ -775,14 +776,23 @@ echo COMPRESS=/usr/sbin/osstest-initramfs-gzip >> \\
 END
 }
 
+sub debian_overlays ($) {
+    my ($func) = @_;
+    $func->($c{OverlayLocal}, 'overlay-local.tar');
+    $func->('overlay', 'overlay.tar');
+}
+
 sub preseed_base ($$$$;@) {
     my ($ho,$suite,$sfx,$extra_packages,%xopts) = @_;
 
     $xopts{ExtraPreseed} ||= '';
 
     preseed_ssh($ho, $sfx);
-    preseed_hook_overlay($ho, $sfx, $c{OverlayLocal}, 'overlay-local.tar');
-    preseed_hook_overlay($ho, $sfx, 'overlay', 'overlay.tar');
+
+    debian_overlays(sub {
+	my ($srcdir, $tfilename) = @_;
+	preseed_hook_overlay($ho, $sfx, $srcdir, $tfilename);
+    });
 
     my $preseed = <<"END";
 d-i debian-installer/locale string en_GB
Signature · 1 line
1.7.10.4
#11 lore raw

PATCH 6/7 ts-debian-fixup: Put "/mnt" in a Perl variable

In reply to Ian Jackson

No functional change now, but this will allow us to change the mountpoint.

---
 ts-debian-fixup |   14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
ts-debian-fixup +8 -6
index beae049..6d24587 100755
@@ -50,15 +50,17 @@ sub ether () {
     store_runvar("$gho->{Guest}_tcpcheckport", 22);
 }
 
+our $mountpoint = '/mnt';
+
 sub access () {
     guest_umount_lv($ho, $gho);
     target_cmd_root($ho, <<END);
         set -ex
-        mount /dev/$gho->{Vg}/$gho->{Lv} /mnt
+        mount /dev/$gho->{Vg}/$gho->{Lv} $mountpoint
         perl -i~ -pe "s/^root:[^:]+:/root::/" /etc/shadow
-        mkdir -p /mnt/root/.ssh /mnt/etc/ssh
-        cp -a /root/.ssh/* /mnt/root/.ssh/.
-        cp -a /etc/ssh/ssh_host_*key* /mnt/etc/ssh/.
+        mkdir -p $mountpoint/root/.ssh $mountpoint/etc/ssh
+        cp -a /root/.ssh/* $mountpoint/root/.ssh/.
+        cp -a /etc/ssh/ssh_host_*key* $mountpoint/etc/ssh/.
 END
 }
 
@@ -66,7 +68,7 @@ our $extra;
 
 sub console () {
     my $console=
-        target_kernkind_console_inittab($ho,$gho,"/mnt");
+        target_kernkind_console_inittab($ho,$gho,"$mountpoint");
     return unless length $console;
     
     my $xextra= "console=$console earlyprintk=xen";
@@ -97,7 +99,7 @@ sub filesystems () {
         set -ex
         perl -i~ -pe "
             s,^(/dev/)sda(\\\\d+),\\\${1}$rootdev\\\$2,;
-        " /mnt/etc/fstab
+        " $mountpoint/etc/fstab
 END
 }
 
Signature · 1 line
1.7.10.4
#13 lore raw

PATCH 7/7 ts-debian-fixup: Install the overlays

In reply to Ian Jackson

We want debootstrap-installed guests to get these overlays too.

---
 ts-debian-fixup |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
ts-debian-fixup +14 -0
index 6d24587..3261185 100755
@@ -19,6 +19,7 @@ use strict qw(vars);
 use DBI;
 use Osstest;
 use Osstest::TestSupport;
+use Osstest::Debian;
 
 tsreadconfig();
 
@@ -64,6 +65,18 @@ sub access () {
 END
 }
 
+sub overlay ($$) {
+    my ($srcdir, $tfilename) = @_;
+
+    my $leaf = "$gho->{Guest}-$tfilename";
+    my $fh = open_unique_stashfile(\$leaf);
+    contents_make_cpio($fh,'ustar',$srcdir);
+    seek $fh,0,0 or die "$leaf: $!";
+    target_cmd_inputfh_root($ho, $fh, <<END);
+        tar -C $mountpoint -xf - # $leaf
+END
+}
+
 our $extra;
 
 sub console () {
@@ -175,6 +188,7 @@ savecfg();
 ether();
 target_kernkind_check($gho);
 access();
+debian_overlays(\&overlay);
 console();
 filesystems();
 otherfixupcfg();
Signature · 1 line
1.7.10.4
Acked-by on v1 7/7
#14 lore raw

Re: [OSSTEST PATCH 7/7] ts-debian-fixup: Install the overlays

Quoted 3 lines · Ian Jackson

We want debootstrap-installed guests to get these overlays too.

I'm not especially sure it is necessary to stash the tarball, we don't for HVM or other d-i based installs (AFAICS), but it's done now and no point undoing it.

Ian.

#15 lore raw

Re: [OSSTEST PATCH 7/7] ts-debian-fixup: Install the overlays

Ian Campbell writes ("Re: [OSSTEST PATCH 7/7] ts-debian-fixup: Install the overlays"):

Quoted 8 lines · Ian Jackson
Quoted 1 line

I'm not especially sure it is necessary to stash the tarball, we don't for HVM or other d-i based installs (AFAICS), but it's done now and no point undoing it.

I guess I could have used tmpfile.

Ian.