-
Notifications
You must be signed in to change notification settings - Fork 10
/
extract-initrd.sh
executable file
·60 lines (44 loc) · 1.37 KB
/
extract-initrd.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
set -e
# Extract initrd images from initrd.imgs that have microcode+actual image
# concatenated together. Ie. images that Debian initramfs-tools produce.
# Author: Ville Korhonen <ville.korhonen@ylioppilastutkinto.fi>
INITRD="$1"
BLOCKSIZE="512"
if [ ! -f "${INITRD}" ]
then
echo "E: File not found: ${INITRD}!"
exit 1
fi
INITRD="$(realpath ${INITRD})"
INITRDFULL="initrd.full.img"
INITRDREAL="initrd.real.img"
TEMPDIR="$(mktemp -d tmp.extract-initrd.XXXXXX)"
echo "I: Tempdir ${TEMPDIR}"
cd ${TEMPDIR}
cp ${INITRD} ${INITRDFULL}
BLOCKS="$(cpio -t < ${INITRDFULL} 2>&1 |grep blocks$ |cut -d' ' -f1)"
echo "I: First image is ${BLOCKS} blocks..."
RBLOCKS="$(((${BLOCKS}-1)*${BLOCKSIZE}))"
ls -liah ${INITRDFULL}
NPOS="0x$(dd if=${INITRDFULL} bs=1 skip=${RBLOCKS} count=${BLOCKSIZE} 2>/dev/null |hd |grep "1f 8b 08" |awk '{print $1}')"
NRBLOCKS="$((${RBLOCKS}+${NPOS}))"
CTYPE="$(dd if=${INITRDFULL} bs=1 skip=${NRBLOCKS} |file --mime-type - |awk '{print $2}')"
echo "I: Content type: ${CTYPE}"
case "${CTYPE}" in
application/gzip)
SUFFIX=".gz"
UNPACKCMD="gunzip"
;;
*)
SUFFIX=""
UNPACKCMD=""
;;
esac
echo "I: Extracting initrd image to ${INITRDREAL}${SUFFIX}..."
dd if=${INITRDFULL} bs=1 skip=${NRBLOCKS} of=${INITRDREAL}${SUFFIX}
if [ -n "${UNPACKCMD}" ]
then
echo "I: Unpacking initrd image to ${INITRDREAL}"
${UNPACKCMD} ${INITRDREAL}${SUFFIX}
fi