]>
Commit | Line | Data |
---|---|---|
b21d5cb2 | 1 | #!/bin/bash --norc |
644c5241 | 2 | kver=$(uname -r) |
b21d5cb2 | 3 | |
da5ef173 | 4 | boot_dir="/boot" |
cbd1b385 | 5 | quiet=0 |
58ef91ff TR |
6 | host_only=0 |
7 | force=0 | |
da5ef173 | 8 | |
644c5241 | 9 | error() { echo "$@" >&2; } |
b21d5cb2 LB |
10 | |
11 | usage () { | |
644c5241 | 12 | [[ $1 = '-n' ]] && cmd=echo || cmd=error |
b21d5cb2 | 13 | |
80bafe6d | 14 | $cmd "usage: ${0##*/} [--version] [--help] [-v] [-f] [--preload <module>]" |
b21d5cb2 | 15 | $cmd " [--image-version] [--with=<module>]" |
39f87ab2 | 16 | $cmd " [--nocompress]" |
b21d5cb2 LB |
17 | $cmd " <initrd-image> <kernel-version>" |
18 | $cmd "" | |
80bafe6d | 19 | $cmd " (ex: ${0##*/} /boot/initramfs-$kver.img $kver)" |
b21d5cb2 | 20 | |
644c5241 VL |
21 | [[ $1 = '-n' ]] && exit 0 |
22 | exit 1 | |
b21d5cb2 LB |
23 | } |
24 | ||
5bc545ed VL |
25 | # Little helper function for reading args from the commandline. |
26 | # it automatically handles -a b and -a=b variants, and returns 1 if | |
27 | # we need to shift $3. | |
28 | read_arg() { | |
29 | # $1 = arg name | |
30 | # $2 = arg value | |
31 | # $3 = arg parameter | |
6470e0a1 TR |
32 | param="$1" |
33 | local rematch='^[^=]*=(.*)$' result | |
5bc545ed | 34 | if [[ $2 =~ $rematch ]]; then |
6470e0a1 TR |
35 | read "$param" <<< "${BASH_REMATCH[1]}" |
36 | else | |
37 | for ((i=3; $i <= $#; i++)); do | |
38 | # Only read next arg if it not an arg itself. | |
39 | if [[ ${@:$i:1} = -* ]];then | |
40 | break | |
41 | fi | |
42 | result="$result ${@:$i:1}" | |
43 | # There is no way to shift our callers args, so | |
44 | # return "no of args" to indicate they should do it instead. | |
45 | done | |
46 | read "$1" <<< "$result" | |
47 | return $(($i - 3)) | |
5bc545ed VL |
48 | fi |
49 | } | |
b21d5cb2 | 50 | |
40db1daf TR |
51 | # Taken over from SUSE mkinitrd |
52 | default_kernel_images() { | |
53 | local regex kernel_image kernel_version version_version initrd_image | |
54 | local qf='%{NAME}-%{VERSION}-%{RELEASE}\n' | |
55 | ||
a0120420 | 56 | case "${DRACUT_ARCH:-$(uname -m)}" in |
40db1daf TR |
57 | s390|s390x) |
58 | regex='image' | |
59 | ;; | |
60 | ppc|ppc64) | |
61 | regex='vmlinux' | |
62 | ;; | |
63 | i386|x86_64) | |
64 | regex='vmlinuz' | |
65 | ;; | |
66 | arm*) | |
67 | regex='[uz]Image' | |
68 | ;; | |
62f939b8 | 69 | aarch64|riscv64) |
40db1daf TR |
70 | regex='Image' |
71 | ;; | |
72 | *) regex='vmlinu.' | |
73 | ;; | |
74 | esac | |
75 | ||
a3044aba | 76 | # user mode linux |
40db1daf TR |
77 | if grep -q UML /proc/cpuinfo; then |
78 | regex='linux' | |
79 | fi | |
80 | ||
81 | kernel_images="" | |
82 | initrd_images="" | |
83 | for kernel_image in $(ls $boot_dir \ | |
84 | | sed -ne "\|^$regex\(-[0-9.]\+-[0-9]\+-[a-z0-9]\+$\)\?|p" \ | |
85 | | grep -v kdump$ ) ; do | |
86 | ||
87 | # Note that we cannot check the RPM database here -- this | |
88 | # script is itself called from within the binary kernel | |
89 | # packages, and rpm does not allow recursive calls. | |
90 | ||
91 | [ -L "$boot_dir/$kernel_image" ] && continue | |
92 | [ "${kernel_image%%.gz}" != "$kernel_image" ] && continue | |
93 | kernel_version=$(/usr/bin/get_kernel_version \ | |
94 | $boot_dir/$kernel_image 2> /dev/null) | |
95 | initrd_image=$(echo $kernel_image | sed -e "s|${regex}|initrd|") | |
96 | if [ "$kernel_image" != "$initrd_image" -a \ | |
97 | -n "$kernel_version" -a \ | |
98 | -d "/lib/modules/$kernel_version" ]; then | |
99 | kernel_images="$kernel_images $boot_dir/$kernel_image" | |
100 | initrd_images="$initrd_images $boot_dir/$initrd_image" | |
101 | fi | |
102 | done | |
103 | for kernel_image in $kernel_images;do | |
104 | kernels="$kernels ${kernel_image#*-}" | |
105 | done | |
106 | for initrd_image in $initrd_images;do | |
107 | targets="$targets $initrd_image" | |
108 | done | |
58ef91ff TR |
109 | host_only=1 |
110 | force=1 | |
40db1daf TR |
111 | } |
112 | ||
5bc545ed VL |
113 | while (($# > 0)); do |
114 | case ${1%%=*} in | |
6470e0a1 | 115 | --with-usb) read_arg usbmodule "$@" || shift $? |
5bc545ed VL |
116 | basicmodules="$basicmodules ${usbmodule:-usb-storage}" |
117 | unset usbmodule;; | |
6470e0a1 | 118 | --with-avail) read_arg modname "$@" || shift $? |
5bc545ed | 119 | basicmodules="$basicmodules $modname";; |
6470e0a1 | 120 | --with) read_arg modname "$@" || shift $? |
5bc545ed | 121 | basicmodules="$basicmodules $modname";; |
b21d5cb2 LB |
122 | --version) |
123 | echo "mkinitrd: dracut compatibility wrapper" | |
5bc545ed | 124 | exit 0;; |
b7d1b6f9 | 125 | -v|--verbose) dracut_args="${dracut_args} -v";; |
58ef91ff | 126 | -f|--force) force=1;; |
6470e0a1 | 127 | --preload) read_arg modname "$@" || shift $? |
5bc545ed VL |
128 | basicmodules="$basicmodules $modname";; |
129 | --image-version) img_vers=yes;; | |
fbf338ff | 130 | --rootfs|-d) read_arg rootfs "$@" || shift $? |
5bc545ed VL |
131 | dracut_args="${dracut_args} --filesystems $rootfs";; |
132 | --nocompress) dracut_args="$dracut_args --no-compress";; | |
133 | --help) usage -n;; | |
134 | --builtin) ;; | |
b21d5cb2 LB |
135 | --without*) ;; |
136 | --without-usb) ;; | |
137 | --fstab*) ;; | |
b21d5cb2 LB |
138 | --ifneeded) ;; |
139 | --omit-scsi-modules) ;; | |
140 | --omit-ide-modules) ;; | |
141 | --omit-raid-modules) ;; | |
142 | --omit-lvm-modules) ;; | |
143 | --omit-dmraid) ;; | |
144 | --allow-missing) ;; | |
145 | --net-dev*) ;; | |
146 | --noresume) ;; | |
f8bc8990 HH |
147 | --rootdev*) ;; |
148 | --thawdev*) ;; | |
149 | --rootopts*) ;; | |
150 | --root*) ;; | |
151 | --loopdev*) ;; | |
152 | --loopfs*) ;; | |
153 | --loopopts*) ;; | |
154 | --looppath*) ;; | |
155 | --dsdt*) ;; | |
b21d5cb2 | 156 | --bootchart) ;; |
fbf338ff | 157 | -s) ;; |
cbd1b385 | 158 | --quiet|-q) quiet=1;; |
6470e0a1 | 159 | -b) read_arg boot_dir "$@" || shift $? |
da5ef173 TR |
160 | if [ ! -d $boot_dir ];then |
161 | error "Boot directory $boot_dir does not exist" | |
162 | exit 1 | |
163 | fi | |
164 | ;; | |
165 | -k) # Would be nice to get a list of images here | |
6470e0a1 | 166 | read_arg kernel_images "$@" || shift $? |
da5ef173 TR |
167 | for kernel_image in $kernel_images;do |
168 | kernels="$kernels ${kernel_image#*-}" | |
169 | done | |
58ef91ff TR |
170 | host_only=1 |
171 | force=1 | |
da5ef173 | 172 | ;; |
6470e0a1 | 173 | -i) read_arg initrd_images "$@" || shift $? |
da5ef173 | 174 | for initrd_image in $initrd_images;do |
6470e0a1 | 175 | targets="$targets $boot_dir/$initrd_image" |
da5ef173 TR |
176 | done |
177 | ;; | |
6470e0a1 | 178 | *) if [[ ! $targets ]]; then |
da5ef173 TR |
179 | targets=$1 |
180 | elif [[ ! $kernels ]]; then | |
181 | kernels=$1 | |
b21d5cb2 | 182 | else |
5bc545ed VL |
183 | usage |
184 | fi;; | |
b21d5cb2 | 185 | esac |
b21d5cb2 LB |
186 | shift |
187 | done | |
188 | ||
40db1daf TR |
189 | [[ $targets && $kernels ]] || default_kernel_images |
190 | [[ $targets && $kernels ]] || (error "No kernel found in $boot_dir" && usage) | |
da5ef173 TR |
191 | |
192 | # We can have several targets/kernels, transform the list to an array | |
193 | targets=( $targets ) | |
194 | [[ $kernels ]] && kernels=( $kernels ) | |
b21d5cb2 | 195 | |
58ef91ff TR |
196 | [[ $host_only == 1 ]] && dracut_args="${dracut_args} -H" |
197 | [[ $force == 1 ]] && dracut_args="${dracut_args} -f" | |
198 | ||
cbd1b385 | 199 | echo "Creating: target|kernel|dracut args|basicmodules " |
da5ef173 TR |
200 | for ((i=0 ; $i<${#targets[@]} ; i++)); do |
201 | ||
202 | if [[ $img_vers ]];then | |
203 | target="${targets[$i]}-${kernels[$i]}" | |
204 | else | |
205 | target="${targets[$i]}" | |
206 | fi | |
207 | kernel="${kernels[$i]}" | |
208 | ||
cbd1b385 TR |
209 | # Duplicate code: No way found how to redirect output based on $quiet |
210 | if [[ $quiet == 1 ]];then | |
211 | echo "$target|$kernel|$dracut_args|$basicmodules" | |
212 | if [[ $basicmodules ]]; then | |
213 | dracut $dracut_args --add-drivers "$basicmodules" "$target" \ | |
214 | "$kernel" &>/dev/null | |
215 | else | |
216 | dracut $dracut_args "$target" "$kernel" &>/dev/null | |
217 | fi | |
da5ef173 | 218 | else |
cbd1b385 TR |
219 | if [[ $basicmodules ]]; then |
220 | dracut $dracut_args --add-drivers "$basicmodules" "$target" \ | |
221 | "$kernel" | |
222 | else | |
223 | dracut $dracut_args "$target" "$kernel" | |
224 | fi | |
da5ef173 TR |
225 | fi |
226 | done |