View difference between Paste ID: XvWqtrpz and uTxDHMAK
SHOW: | | - or go back to the newest paste.
1
root@epsybox:/git# cat /usr/local/bin/extract_initramfs_cpio.sh
2
#!/bin/bash
3
zImage=$1
4
#========================================================
5
# find start of gziped kernel object in the zImage file:
6
#========================================================
7
pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' $zImage | cut -f 1 -d :`
8
9
#========================================================================
10
# the cpio archive might be gzipped too, so two gunzips could be needed:
11
#========================================================================
12
if [ "$pos" = "" ]; then
13
        cp $zImage /tmp/kernel.img
14
else
15
        echo "-I- Extracting kernel image from $zImage (start = $pos)"
16
        dd if=$zImage bs=1 skip=$pos | gunzip > /tmp/kernel.img
17
fi
18
pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' /tmp/kernel.img | cut -f 1 -d :`
19
#===========================================================================
20
# find start and end of the "cpio" initramfs image inside the kernel object:
21
# ASCII cpio header starts with '070701'
22
# The end of the cpio archive is marked with an empty file named TRAILER!!!
23
#===========================================================================
24
if [ ! "$pos" = "" ]; then
25
   echo "-I- Extracting compressed cpio image from kernel image (start = $pos)"
26
   dd if=/tmp/kernel.img bs=1 skip=$pos | gunzip > /tmp/cpio.img
27
   start=`grep -a -b -m 1 --only-matching '070701' /tmp/cpio.img | head -1 | cut -f 1 -d :`
28
   end=`grep -a -b -m 1 --only-matching 'TRAILER!!!' /tmp/cpio.img | head -1 | cut -f 1 -d :`
29
   inputfile=/tmp/cpio.img
30
else
31
   echo "-I- Already uncompressed cpio.img, not decompressing"
32
   start=`grep -a -b -m 1 --only-matching '070701' /tmp/kernel.img | head -1 | cut -f 1 -d :`
33
   end=`grep -a -b -m 1 --only-matching 'TRAILER!!!' /tmp/kernel.img | head -1 | cut -f 1 -d :`
34
   inputfile=/tmp/kernel.img
35
fi
36
37
end=$((end + 10))
38
count=$((end - start))
39
if (($count < 0)); then
40
   echo "-E- Couldn't match start/end of the initramfs image."
41
   exit
42
fi
43
echo "-I- Extracting initramfs image from $inputfile (start = $start, end = $end)"
44
dd if=$inputfile bs=1 skip=$start count=$count > initramfs.cpio
45
46
#to unpack...  cpio