18971c
From 815b9c16a4e90b67bab008da678d73faa74f8e0c Mon Sep 17 00:00:00 2001
18971c
From: Harald Hoyer <harald@redhat.com>
18971c
Date: Fri, 24 Jan 2014 15:30:22 +0100
18971c
Subject: [PATCH] lsinitrd.sh: make use of the skipcpio utility
18971c
18971c
With the skipcpio utility, the whole contents of an initramfs with an
18971c
early cpio image can be displayed.
18971c
---
18971c
 lsinitrd.sh | 96 +++++++++++++++++++++++++++++++++++++++++------------
18971c
 1 file changed, 75 insertions(+), 21 deletions(-)
18971c
18971c
diff --git a/lsinitrd.sh b/lsinitrd.sh
18971c
index f9f243bb..70e12f81 100755
18971c
--- a/lsinitrd.sh
18971c
+++ b/lsinitrd.sh
18971c
@@ -33,6 +33,9 @@ usage()
18971c
     } >&2
18971c
 }
18971c
 
18971c
+
18971c
+[[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
18971c
+
18971c
 sorted=0
18971c
 declare -A filenames
18971c
 
18971c
@@ -103,14 +106,72 @@ if ! [[ -f "$image" ]]; then
18971c
     exit 1
18971c
 fi
18971c
 
18971c
+extract_files()
18971c
+{
18971c
+    (( ${#filenames[@]} == 1 )) && nofileinfo=1
18971c
+    for f in ${!filenames[@]}; do
18971c
+        [[ $nofileinfo ]] || echo "initramfs:/$f"
18971c
+        [[ $nofileinfo ]] || echo "========================================================================"
18971c
+        $CAT $image | cpio --extract --verbose --quiet --to-stdout $f 2>/dev/null
18971c
+        ((ret+=$?))
18971c
+        [[ $nofileinfo ]] || echo "========================================================================"
18971c
+        [[ $nofileinfo ]] || echo
18971c
+    done
18971c
+}
18971c
+
18971c
+list_files()
18971c
+{
18971c
+    echo "========================================================================"
18971c
+    if [ "$sorted" -eq 1 ]; then
18971c
+        $CAT "$image" | cpio --extract --verbose --quiet --list | sort -n -k5
18971c
+    else
18971c
+        $CAT "$image" | cpio --extract --verbose --quiet --list | sort -k9
18971c
+    fi
18971c
+    ((ret+=$?))
18971c
+    echo "========================================================================"
18971c
+}
18971c
+
18971c
+
18971c
+if (( ${#filenames[@]} <= 0 )); then
18971c
+    echo "Image: $image: $(du -h $image | while read a b; do echo $a;done)"
18971c
+    echo "========================================================================"
18971c
+fi
18971c
+
18971c
 read -N 6 bin < "$image"
18971c
+case $bin in
18971c
+    $'\x71\xc7'*|070701)
18971c
+        CAT="cat --"
18971c
+        is_early=$(cpio --extract --verbose --quiet --to-stdout -- 'early_cpio' < "$image" 2>/dev/null)
18971c
+        if [[ "$is_early" ]]; then
18971c
+            if (( ${#filenames[@]} > 0 )); then
18971c
+                extract_files
18971c
+            else
18971c
+                echo "Early CPIO image"
18971c
+                list_files
18971c
+            fi
18971c
+            SKIP="$dracutbasedir/skipcpio"
18971c
+            if ! [[ -x $SKIP ]]; then
18971c
+                echo
18971c
+                echo "'$SKIP' not found, cannot display remaining contents!" >&2
18971c
+                echo
18971c
+                exit 0
18971c
+            fi
18971c
+        fi
18971c
+        ;;
18971c
+esac
18971c
+
18971c
+if [[ $SKIP ]]; then
18971c
+    read -N 6 bin < <($SKIP "$image")
18971c
+fi
18971c
+
18971c
 case $bin in
18971c
     $'\x1f\x8b'*)
18971c
         CAT="zcat --";;
18971c
     BZh*)
18971c
         CAT="bzcat --";;
18971c
     $'\x71\xc7'*|070701)
18971c
-        CAT="cat --";;
18971c
+        CAT="cat --"
18971c
+        ;;
18971c
     $'\x04\x22'*)
18971c
         CAT="lz4 -d -c";;
18971c
     *)
18971c
@@ -121,34 +182,27 @@ case $bin in
18971c
         ;;
18971c
 esac
18971c
 
18971c
+skipcpio()
18971c
+{
18971c
+    $SKIP "$@" | $ORIG_CAT
18971c
+}
18971c
+
18971c
+if [[ $SKIP ]]; then
18971c
+    ORIG_CAT="$CAT"
18971c
+    CAT=skipcpio
18971c
+fi
18971c
+
18971c
 ret=0
18971c
 
18971c
 if (( ${#filenames[@]} > 0 )); then
18971c
-    (( ${#filenames[@]} == 1 )) && nofileinfo=1
18971c
-    for f in ${!filenames[@]}; do
18971c
-        [[ $nofileinfo ]] || echo "initramfs:/$f"
18971c
-        [[ $nofileinfo ]] || echo "========================================================================"
18971c
-        $CAT $image | cpio --extract --verbose --quiet --to-stdout $f 2>/dev/null
18971c
-        ((ret+=$?))
18971c
-        [[ $nofileinfo ]] || echo "========================================================================"
18971c
-        [[ $nofileinfo ]] || echo
18971c
-    done
18971c
+    extract_files
18971c
 else
18971c
-    echo "Image: $image: $(du -h $image | while read a b; do echo $a;done)"
18971c
-    echo "========================================================================"
18971c
-    version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- '*lib/dracut/dracut-*' 2>/dev/null)
18971c
+    version=$($CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- 'lib/dracut/dracut-*' 'usr/lib/dracut/dracut-*' 2>/dev/null)
18971c
     ((ret+=$?))
18971c
     echo "$version with dracut modules:"
18971c
     $CAT "$image" | cpio --extract --verbose --quiet --to-stdout -- '*lib/dracut/modules.txt' 2>/dev/null
18971c
     ((ret+=$?))
18971c
-    echo "========================================================================"
18971c
-    if [ "$sorted" -eq 1 ]; then
18971c
-        $CAT "$image" | cpio --extract --verbose --quiet --list | sort -n -k5
18971c
-    else
18971c
-        $CAT "$image" | cpio --extract --verbose --quiet --list | sort -k9
18971c
-    fi
18971c
-    ((ret+=$?))
18971c
-    echo "========================================================================"
18971c
+    list_files
18971c
 fi
18971c
 
18971c
 exit $ret