-
Notifications
You must be signed in to change notification settings - Fork 0
/
initrd-repack.sh
executable file
·131 lines (116 loc) · 3.4 KB
/
initrd-repack.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
# Filename: initrd-repack.sh
# Purpose: inject archive in existing initrd
# Authors: Andreas Nowak <[email protected]>
# License: This file is licensed under the GPL v2 or any later version.
################################################################################
# vim: ai ts=2 sw=2 et sts=2 ft=sh
# vim: autoindent tabstop=2 shiftwidth=2 expandtab softtabstop=2 filetype=sh
################################################################################
# make sure we have the sbin directories in our PATH
PATH="${PATH}:/sbin:/usr/local/sbin:/usr/sbin"
# define function getfilesize before "set -e" {{{
if stat --help >/dev/null 2>&1; then
getfilesize='stat -c %s' # GNU stat
else
getfilesize='stat -f %z' # BSD stat
fi
# }}}
# adjust variables if necessary through environment {{{
# work directory for creating the filesystem
WRKDIR='/tmp/'
# support mkisofs as well as genisoimage
ZCAT=$(which zcat)
if [ ! -x "$ZCAT" ]; then
echo "Error: zcat not executeable"
exit 1
fi
CPIO=$(which cpio)
if [ ! -x "$CPIO" ]; then
echo "Error: cpio not executeable"
exit 1
fi
# }}}
# helper stuff {{{
set -e
usage() {
echo >&2 "Usage: $0 [-v] -i initrd.img.gz -a archive.deb [-t tempdir]"
echo >&2 "
Options:
-v verbose output
Examples:
$0 -i boot.img.gz -a linux-image-extra-4.13.0-36-generic_4.13.0-36.40_amd64.deb -t /home/user/temp/
Will extract files from linux-image-extra-4.13.0-36-generic_4.13.0-36.40_amd64.deb and inject it
to the given boot.img.gz
"
[ -n "$1" ] && exit $1 || exit 1
}
# }}}
# command line handling {{{
[[ $# -gt 1 ]] || usage 1
INITRD=''
VERBOSE=''
ARCHIVE=''
while getopts vi:a:t: name; do
case $name in
i) INITRD="$OPTARG";;
t) WRKDIR="$(readlink -f "$OPTARG")"; [ -n "$WRKDIR" ] || { echo "Could not read $OPTARG - exiting" >&2 ; exit 1 ; } ;;
a) ARCHIVE="$OPTARG";;
v) VERBOSE='true';;
?) usage 2;;
esac
done
if [ ! -f "$INITRD" -o -z "$INITRD" ]; then
echo "Error: initrd $INITRD not found"
exit 1
fi
if [ ! -f "$ARCHIVE" -o -z "$ARCHIVE" ]; then
echo "Error: archive $ARCHIVE not found"
exit 1
fi
# }}}
# preparation {{{
MKTEMP=$(which mktemp)
if [ ! -x "$MKTEMP" ]; then
echo "Error: mktemp not executeable"
exit 1
fi
TMPDIR=$($MKTEMP -d --tmpdir=${WRKDIR} --suffix='-repack-initrd')
mkdir -p $TMPDIR/unpack
INITRDPATH=$(realpath $INITRD)
ARCHIVEPATH=$(realpath $ARCHIVE)
# }}}
# determine filetype and set command {{{
FILECMD="$(which file) -b --mime-type"
ARCHIVETYPE=$($FILECMD $ARCHIVEPATH)
# }}}
# unpack and build {{{
cd $TMPDIR/unpack
$ZCAT $INITRD | $CPIO -idmv 1>/dev/null 2>&1 ||:
case $ARCHIVETYPE in
application/vnd.debian.binary-package)
DPKG=$(which dpkg)
if [ ! -x "$DPKG" ]; then
echo "Error: dpkg not executeable"
exit 1
fi
$DPKG -x $ARCHIVEPATH $TMPDIR/unpack
;;
application/x-rpm)
RPM2CPIO=$(which rpm2cpio)
if [ ! -x "$RPM2CPIO" ]; then
echo "Error: rpm2cpio not executeable"
exit 1
fi
$RPM2CPIO $ARCHIVEPATH | $CPIO -idmv
;;
*)
echo "Archivetype $ARCHIVETYPE not supported"
exit 1
esac
find . | $CPIO -o -c 2>/dev/null | gzip -9 > $INITRDPATH.new
echo "Info: New initrd $INITRD.new has been created"
# }}}
# cleanup {{{
rm -rf $TMPDIR
# }}}