[PATCH tip/core/rcu 2/8] rcutorture: Add initrd support for systems lacking dracut

2018-11-11 Thread Paul E. McKenney
From: "Paul E. McKenney" 

The support for creating initrd directories using dracut is a great
improvement over having to always hand-create them, it is a bit annoying
to have to install some otherwise irrelevant package just to be able to
run rcutorture.  This commit therefore adds support for creating initrd
directories on systems innocent of dracut.  You do need gcc, but then
again you need that to build the kernel (or to build llvm) in any case.

The idea is to create an initrd directory containing nothing but a
statically linked binary having a for-loop over a long-term sleep().
The result is a Linux kernel with almost no userspace: even the
time-honored /dev, /lib, /tmp, and /usr directories are gone.  In fact,
the only directory present is "/", but only because I don't know how to
get rid of it, at least short of not having an initrd in the first place.
Although statically linked binaries are much maligned, and rightly so,
their disadvantages seem to be irrelevant for this particular use case.
>From https://www.akkadia.org/drepper/no_static_linking.html:

1.  Fixes are difficult to apply to hordes of widely scattered
statically linked binaries.  But in this case, there is only one
binary, but there would otherwise be no fewer than four libraries.

2.  Security measures like local address randomization cannot be used.
Prudence prevents me from asserting that it is impossible to
base a remote attack on a networking-free rcutorture instance.
Nevertheless, bonus points to the first person who comes up with
such an attack!

3.  More efficient use of physical memory.  Not in this case, given
that libc is 1.8MB and the statically linked binary "only" 800K.

4.  Features such as locales, name service switch (NSS),
internationalized domain names (IDN) tool, and so on require
dynamic linking.  Bonus points to the first person coming up
with a valid rcutorture use case requiring these features in
its initrd.

5.  Accidental violations of (L)GPL.  Actually, this change actually
helps -avoid- such violations by reducing the temptation to
pass around tarballs of rcutorture-ready initrd directories.
After all, the rcutorture scripts automatically create an initrd
directory for you, so why bother with the tarballs?

6.  Tools and hacks like ltrace, LD_PRELOAD, LD_PROFILE, and LD_AUDIT
don't work.  Again, bonus points to the first person coming up
with a valid rcutorture use case requiring these features in
its initrd.

Nevertheless, the script will use dracut if available, and will create the
statically linked binary only when dracut are missing.  Those preferring
the smaller initrd directory resulting from the statically linked binary
(like me) are free to hand-edit mkinitrd.sh to remove the code using
dracut.  ;-)

Signed-off-by: Paul E. McKenney 
---
 .../selftests/rcutorture/bin/mkinitrd.sh  | 40 ++--
 .../selftests/rcutorture/doc/initrd.txt   | 99 +++
 2 files changed, 45 insertions(+), 94 deletions(-)

diff --git a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh 
b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
index ae773760f396..87a87ffeaa85 100755
--- a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
+++ b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
@@ -46,15 +46,41 @@ done
 __EOF___
 
 # Try using dracut to create initrd
-command -v dracut >/dev/null 2>&1 || { echo >&2 "Dracut not installed"; exit 
1; }
-echo Creating $D/initrd using dracut.
+if command -v dracut >/dev/null 2>&1
+then
+   echo Creating $D/initrd using dracut.
+   # Filesystem creation
+   dracut --force --no-hostonly --no-hostonly-cmdline --module "base" 
$T/initramfs.img
+   cd $D
+   mkdir initrd
+   cd initrd
+   zcat $T/initramfs.img | cpio -id
+   cp $T/init init
+   chmod +x init
+   echo Done creating $D/initrd using dracut
+   exit 0
+fi
 
-# Filesystem creation
-dracut --force --no-hostonly --no-hostonly-cmdline --module "base" 
$T/initramfs.img
+# No dracut, so create a C-language initrd/init program and statically
+# link it.  This results in a very small initrd, but might be a bit less
+# future-proof than dracut.
+echo "Could not find dracut, attempting C initrd"
 cd $D
 mkdir initrd
 cd initrd
-zcat $T/initramfs.img | cpio -id
-cp $T/init init
-echo Done creating $D/initrd using dracut
+cat > init.c << '___EOF___'
+#include 
+
+int main(int argc, int argv[])
+{
+   for (;;)
+   sleep(1000*1000*1000); /* One gigasecond is ~30 years. */
+   return 0;
+}
+___EOF___
+gcc -static -Os -o init init.c
+strip init
+rm init.c
+echo "Done creating a statically linked C-language initrd"
+
 exit 0
diff --git a/tools/testing/selftests/rcutorture/doc/initrd.txt 
b/tools/testing/selftests/rcutorture/doc/initrd.txt
index 833f826d6ec2..933b4fd12327 100644
--- 

[PATCH tip/core/rcu 2/8] rcutorture: Add initrd support for systems lacking dracut

2018-11-11 Thread Paul E. McKenney
From: "Paul E. McKenney" 

The support for creating initrd directories using dracut is a great
improvement over having to always hand-create them, it is a bit annoying
to have to install some otherwise irrelevant package just to be able to
run rcutorture.  This commit therefore adds support for creating initrd
directories on systems innocent of dracut.  You do need gcc, but then
again you need that to build the kernel (or to build llvm) in any case.

The idea is to create an initrd directory containing nothing but a
statically linked binary having a for-loop over a long-term sleep().
The result is a Linux kernel with almost no userspace: even the
time-honored /dev, /lib, /tmp, and /usr directories are gone.  In fact,
the only directory present is "/", but only because I don't know how to
get rid of it, at least short of not having an initrd in the first place.
Although statically linked binaries are much maligned, and rightly so,
their disadvantages seem to be irrelevant for this particular use case.
>From https://www.akkadia.org/drepper/no_static_linking.html:

1.  Fixes are difficult to apply to hordes of widely scattered
statically linked binaries.  But in this case, there is only one
binary, but there would otherwise be no fewer than four libraries.

2.  Security measures like local address randomization cannot be used.
Prudence prevents me from asserting that it is impossible to
base a remote attack on a networking-free rcutorture instance.
Nevertheless, bonus points to the first person who comes up with
such an attack!

3.  More efficient use of physical memory.  Not in this case, given
that libc is 1.8MB and the statically linked binary "only" 800K.

4.  Features such as locales, name service switch (NSS),
internationalized domain names (IDN) tool, and so on require
dynamic linking.  Bonus points to the first person coming up
with a valid rcutorture use case requiring these features in
its initrd.

5.  Accidental violations of (L)GPL.  Actually, this change actually
helps -avoid- such violations by reducing the temptation to
pass around tarballs of rcutorture-ready initrd directories.
After all, the rcutorture scripts automatically create an initrd
directory for you, so why bother with the tarballs?

6.  Tools and hacks like ltrace, LD_PRELOAD, LD_PROFILE, and LD_AUDIT
don't work.  Again, bonus points to the first person coming up
with a valid rcutorture use case requiring these features in
its initrd.

Nevertheless, the script will use dracut if available, and will create the
statically linked binary only when dracut are missing.  Those preferring
the smaller initrd directory resulting from the statically linked binary
(like me) are free to hand-edit mkinitrd.sh to remove the code using
dracut.  ;-)

Signed-off-by: Paul E. McKenney 
---
 .../selftests/rcutorture/bin/mkinitrd.sh  | 40 ++--
 .../selftests/rcutorture/doc/initrd.txt   | 99 +++
 2 files changed, 45 insertions(+), 94 deletions(-)

diff --git a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh 
b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
index ae773760f396..87a87ffeaa85 100755
--- a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
+++ b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
@@ -46,15 +46,41 @@ done
 __EOF___
 
 # Try using dracut to create initrd
-command -v dracut >/dev/null 2>&1 || { echo >&2 "Dracut not installed"; exit 
1; }
-echo Creating $D/initrd using dracut.
+if command -v dracut >/dev/null 2>&1
+then
+   echo Creating $D/initrd using dracut.
+   # Filesystem creation
+   dracut --force --no-hostonly --no-hostonly-cmdline --module "base" 
$T/initramfs.img
+   cd $D
+   mkdir initrd
+   cd initrd
+   zcat $T/initramfs.img | cpio -id
+   cp $T/init init
+   chmod +x init
+   echo Done creating $D/initrd using dracut
+   exit 0
+fi
 
-# Filesystem creation
-dracut --force --no-hostonly --no-hostonly-cmdline --module "base" 
$T/initramfs.img
+# No dracut, so create a C-language initrd/init program and statically
+# link it.  This results in a very small initrd, but might be a bit less
+# future-proof than dracut.
+echo "Could not find dracut, attempting C initrd"
 cd $D
 mkdir initrd
 cd initrd
-zcat $T/initramfs.img | cpio -id
-cp $T/init init
-echo Done creating $D/initrd using dracut
+cat > init.c << '___EOF___'
+#include 
+
+int main(int argc, int argv[])
+{
+   for (;;)
+   sleep(1000*1000*1000); /* One gigasecond is ~30 years. */
+   return 0;
+}
+___EOF___
+gcc -static -Os -o init init.c
+strip init
+rm init.c
+echo "Done creating a statically linked C-language initrd"
+
 exit 0
diff --git a/tools/testing/selftests/rcutorture/doc/initrd.txt 
b/tools/testing/selftests/rcutorture/doc/initrd.txt
index 833f826d6ec2..933b4fd12327 100644
---