Add an option to pack executables.
authorrjones <rjones>
Mon, 23 Mar 2009 16:54:18 +0000 (16:54 +0000)
committerrjones <rjones>
Mon, 23 Mar 2009 16:54:18 +0000 (16:54 +0000)
Be careful to retain same inode number for /etc/services.

README
examples/minimal-filesystem.sh
febootstrap-minimize.pod
febootstrap-minimize.sh
febootstrap.spec.in

diff --git a/README b/README
index 33087a7..e9391e8 100644 (file)
--- a/README
+++ b/README
@@ -34,6 +34,9 @@ Requirements
 
   MAKEDEV
 
+  upx
+    - Ultimate Packer for eXecutables, for minimizing binaries
+
   qemu
     - If you want to test-run your systems.
 
index 01ca9e8..9a0f575 100755 (executable)
@@ -27,7 +27,7 @@ fi
 # ... but let's minimize it aggressively.
 
 echo -n "Before minimization: "; du -sh minimal
-../febootstrap-minimize --all ./minimal
+../febootstrap-minimize --all --pack-executables ./minimal
 echo -n "After minimization:  "; du -sh minimal
 
 # Create the /init which is just a simple script to give users an
index e7a2084..96cb177 100644 (file)
@@ -117,6 +117,16 @@ C</var/cache/ldconfig> (the dynamic linking cache).  This is not
 needed.  Dynamic linking during program execution will be marginally
 slower.
 
+=item B<--no-pack-executables>
+
+=item B<--pack-executables>
+
+Pack executables and shell scripts using L<upx(1)>.
+
+This is not done by default because although it can reduce the image
+size, it increases the amount of memory used since those executables
+cannot be shared.
+
 =back
 
 =head1 TODO
@@ -136,10 +146,6 @@ Remove unused binaries.
 
 Remove unused libraries.
 
-=item *
-
-Pack executables using UPX.
-
 =back
 
 =head1 SEE ALSO
index 58b86fa..90915e3 100755 (executable)
@@ -20,7 +20,7 @@
 
 TEMP=`getopt \
         -o '' \
-        --long help,all,none,keep-locales,drop-locales,keep-docs,drop-docs,keep-cracklib,drop-cracklib,keep-i18n,drop-i18n,keep-zoneinfo,drop-zoneinfo,keep-rpmdb,drop-rpmdb,keep-yum-cache,drop-yum-cache,keep-services,drop-services,keep-sln,drop-sln,keep-ldconfig,drop-ldconfig \
+        --long help,all,none,keep-locales,drop-locales,keep-docs,drop-docs,keep-cracklib,drop-cracklib,keep-i18n,drop-i18n,keep-zoneinfo,drop-zoneinfo,keep-rpmdb,drop-rpmdb,keep-yum-cache,drop-yum-cache,keep-services,drop-services,keep-sln,drop-sln,keep-ldconfig,drop-ldconfig,no-pack-executables,pack-executables \
         -n febootstrap-minimize -- "$@"`
 if [ $? != 0 ]; then
     echo "febootstrap-minimize: problem parsing the command line arguments"
@@ -57,6 +57,7 @@ keep_yum_cache=yes
 }
 
 set_all
+pack_executables=no
 
 usage ()
 {
@@ -132,6 +133,12 @@ while true; do
        --drop-ldconfig)
            keep_ldconfig=no
            shift;;
+       --no-pack-executables)
+           pack_executables=no
+           shift;;
+       --pack-executables)
+           pack_executables=yes
+           shift;;
        --help)
            usage
            exit 0;;
@@ -151,14 +158,21 @@ fi
 
 target="$1"
 
-#----------------------------------------------------------------------
-
 if [ ! -d "$target" ]; then
     echo "febootstrap-minimize: $target: target directory not found"
     exit 1
 fi
 
-#du -sh "$target"
+# Create a temporary directory, make sure it gets cleaned up at the end.
+tmpdir=$(mktemp -d)
+remove_tmpdir ()
+{
+  status=$?
+  rm -rf "$tmpdir" && exit $status
+}
+trap remove_tmpdir EXIT
+
+#----------------------------------------------------------------------
 
 if [ "$keep_locales" != "yes" ]; then
     rm -f "$target"/usr/lib/locale/*
@@ -199,7 +213,8 @@ if [ "$keep_yum_cache" != "yes" ]; then
 fi
 
 if [ "$keep_services" != "yes" ]; then
-    rm -f "$target"/etc/services
+    # NB: Overwrite the same file so that we have the same inode,
+    # since fakeroot tracks files by inode number.
     cat > "$target"/etc/services <<'__EOF__'
 tcpmux 1/tcp
 tcpmux 1/udp
@@ -259,4 +274,19 @@ if [ "$keep_ldconfig" != "yes" ]; then
     rm -f "$target"/sbin/ldconfig
     rm -f "$target"/etc/ld.so.cache
     rm -rf "$target"/var/cache/ldconfig/*
-fi
\ No newline at end of file
+fi
+
+if [ "$pack_executables" = "yes" ]; then
+    # NB. Be careful to keep the same inode number, since fakeroot
+    # tracks files by inode number.
+    for path in $(find "$target" -type f -perm /111 |
+                 xargs file |
+                 grep executable |
+                 awk -F: '{print $1}'); do
+       base=$(basename $path)
+       cp $path $tmpdir
+       (cd $tmpdir && upx -q -q --best $base)
+       cat $tmpdir/$base > $path
+       rm $tmpdir/$base
+    done
+fi
index 0c5db41..ae95abf 100644 (file)
@@ -19,6 +19,7 @@ Requires:    yum >= 3.2
 Requires:    MAKEDEV
 Requires:    util-linux-ng
 Requires:    cpio
+Requires:    upx
 
 # These are suggestions.  However making them hard requirements
 # pulls in far too much stuff.