3 # mkinitrd compatibility wrapper for SUSE.
5 # Copyright (c) 2013 SUSE Linux Products GmbH. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 logfile
=/var
/log
/YaST
2/mkinitrd.log
26 error
() { echo "$@" >&2; }
29 [[ $1 = '-n' ]] && cmd
=echo || cmd
=error
31 $cmd "usage: ${0##*/} [options]"
33 $cmd " Create initial ramdisk images that contain all kernel modules needed"
34 $cmd " in the early boot process, before the root file system becomes"
36 $cmd " This usually includes SCSI and/or RAID modules, a file system module"
37 $cmd " for the root file system, or a network interface driver module for dhcp."
40 $cmd " -f \"feature list\" Features to be enabled when generating initrd."
41 $cmd " Available features are:"
42 $cmd " iscsi, md, multipath, lvm, lvm2,"
43 $cmd " ifup, fcoe, dcbd"
44 $cmd " -k \"kernel list\" List of kernel images for which initrd files are"
45 $cmd " created. Defaults to all kernels found in /boot."
46 $cmd " -i \"initrd list\" List of file names for the initrd; position have"
47 $cmd " match to \"kernel list\". Defaults to all kernels"
48 $cmd " found in /boot."
49 $cmd " -b boot_dir Boot directory. Defaults to /boot."
50 $cmd " -t tmp_dir Temporary directory. Defaults to /var/tmp."
51 $cmd " -M map System.map file to use."
52 $cmd " -A Create a so called \"monster initrd\" which"
53 $cmd " includes all features and modules possible."
54 $cmd " -B Do not update bootloader configuration."
55 $cmd " -v Verbose mode."
56 $cmd " -L Disable logging."
57 $cmd " -h This help screen."
58 $cmd " -m \"module list\" Modules to include in initrd. Defaults to the"
59 $cmd " INITRD_MODULES variable in /etc/sysconfig/kernel"
60 $cmd " -u \"DomU module list\" Modules to include in initrd. Defaults to the"
61 $cmd " DOMU_INITRD_MODULES variable in"
62 $cmd " /etc/sysconfig/kernel."
63 $cmd " -d root_device Root device. Defaults to the device from"
64 $cmd " which / is mounted. Overrides the rootdev"
65 $cmd " environment variable if set."
66 $cmd " -j device Journal device"
67 $cmd " -D interface Run dhcp on the specified interface."
68 $cmd " -I interface Configure the specified interface statically."
69 $cmd " -a acpi_dsdt Obsolete, do not use."
70 $cmd " -s size Add splash animation and bootscreen to initrd."
72 [[ $1 = '-n' ]] && exit 0
76 # Little helper function for reading args from the commandline.
77 # it automatically handles -a b and -a=b variants, and returns 1 if
78 # we need to shift $3.
84 local rematch
='^[^=]*=(.*)$' result
85 if [[ $2 =~
$rematch ]]; then
86 read "$param" <<< "${BASH_REMATCH[1]}"
88 for ((i
=3; $i <= $#; i
++)); do
89 # Only read next arg if it not an arg itself.
90 if [[ ${@:$i:1} = -* ]];then
93 result
="$result ${@:$i:1}"
94 # There is no way to shift our callers args, so
95 # return "no of args" to indicate they should do it instead.
97 read "$1" <<< "$result"
102 # Helper functions to calculate ipconfig command line
106 [ -z "$prefix" ] && return
107 mask
=$
(( 0xffffffff << (32 - $prefix) ))
108 byte1
=$
(( mask
>> 24 ))
109 byte2
=$
(( mask
>> 16 ))
110 byte3
=$
(( mask
>> 8 ))
111 byte4
=$
(( mask
& 0xff ))
112 netmask
=$
(printf "%d.%d.%d.%d" $
(( byte1
& 0xff )) $
(( byte2
& 0xff )) $
(( byte3
& 0xff )) $byte4);
119 local iplink macaddr broadcast gateway ipaddr prefix netmask
121 iplink
=$
(ip addr show dev
$interface |
sed -n 's/ *inet \(.*\) brd.*/\1/p')
122 macaddr
=$
(ip addr show dev
$interface |
sed -n 's/.*ether \(.*\) brd.*/\1/p')
123 broadcast
=$
(ip addr show dev
$interface |
sed -n 's/.*brd \(.*\) scope.*/\1/p')
124 gateway
=$
(ip route show dev
$interface |
sed -n 's/default via \([0-9\.]*\).*/\1/p')
128 netmask
=$
(calc_netmask
$prefix)
130 echo "${ipaddr}:${serveraddr}:${gateway}:${netmask}:${hostname}:${interface}:none::${macaddr}"
138 for cfg
in ${root_dir}/boot
/config-
$kversion $root_dir/lib
/modules
/$kversion/build
/.config
140 test -r $cfg ||
continue
141 grep -q "^CONFIG_XEN=y\$" $cfg
144 test $kversion != "${kversion%-xen*}"
148 # kernel_image_gz_from_image() and kernel_version_from_image() are helpers
149 # for arm* kernels which produce zImage files which cannot be read from
150 # get_kernel_version -> get rid of this workaround if possible
151 kernel_image_gz_from_image
() {
152 local arch
=$
(uname
-i)
155 # uImage kernels can't be extracted directly. Use the vmlinux.gz instead
156 r
=${r//uImage/vmlinux}
158 # on ARM a zImage can't be extracted directly. Other platforms define it
159 # as a gzipped vmlinux file, but not ARM. So only on ARM, use vmlinux.gz.
160 if [[ $arch =~ arm
]] ||
[[ $arch =~ aarch
]]; then
161 r
=${r//zImage/vmlinux}
167 kernel_version_from_image
() {
168 local kernel_image
="$1" kernel_image_gz
=$
(kernel_image_gz_from_image
"$1")
170 if get_kernel_version
"$kernel_image" 2>/dev
/null
; then
173 get_kernel_version
"$kernel_image_gz" 2>/dev
/null
176 # Taken over from SUSE mkinitrd
177 default_kernel_images
() {
178 local regex kernel_image kernel_version version_version initrd_image
179 local qf
='%{NAME}-%{VERSION}-%{RELEASE}\n'
181 case "$(uname -m)" in
203 for kernel_image
in $
(ls $boot_dir \
204 |
sed -ne "\|^$regex\(-[0-9.]\+-[0-9]\+-[a-z0-9]\+$\)\?|p" \
205 |
grep -v kdump$
) ; do
207 # Note that we cannot check the RPM database here -- this
208 # script is itself called from within the binary kernel
209 # packages, and rpm does not allow recursive calls.
211 [ -L "$boot_dir/$kernel_image" ] && continue
212 [ "${kernel_image%%.gz}" != "$kernel_image" ] && continue
214 kernel_version
=$
(kernel_version_from_image \
215 $boot_dir/$kernel_image 2> /dev
/null
)
216 initrd_image
=$
(echo $kernel_image |
sed -e "s|${regex}|initrd|")
217 if [ "$kernel_image" != "$initrd_image" -a \
218 -n "$kernel_version" -a \
219 -d "/lib/modules/$kernel_version" ]; then
220 kernel_images
="$kernel_images $boot_dir/$kernel_image"
221 initrd_images
="$initrd_images $boot_dir/$initrd_image"
224 for kernel_image
in $kernel_images;do
225 kernels
="$kernels ${kernel_image#*-}"
227 for initrd_image
in $initrd_images;do
228 targets
="$targets $initrd_image"
234 -f) read_arg feature_list
"$@" ||
shift $?
235 # Could be several features
237 -k) # Would be nice to get a list of images here
238 read_arg kernel_images
"$@" ||
shift $?
239 for kernel_image
in $kernel_images;do
240 [ -L "/boot/$kernel_image" ] && kernel_image
="$(readlink "/boot
/$kernel_image")"
241 kernels
="$kernels ${kernel_image#*-}"
244 -i) read_arg initrd_images
"$@" ||
shift $?
245 for initrd_image
in $initrd_images;do
246 [ -L "/boot/$initrd_image" ] && initrd_image
="$(readlink "/boot
/$initrd_image")"
247 # Check if the initrd_image contains a path.
248 # if not, then add the default boot_dir
249 dname
=`dirname $initrd_image`
250 if [ "$dname" == "." ]; then
251 targets
="$targets $boot_dir/$initrd_image";
253 targets
="$targets $initrd_image";
257 -b) read_arg boot_dir
"$@" ||
shift $?
258 if [ ! -d $boot_dir ];then
259 error
"Boot directory $boot_dir does not exist"
263 -t) read_arg tmp_dir
"$@" ||
shift $?
264 dracut_args
="${dracut_args} --tmpdir $tmp_dir"
266 -M) read_arg map_file
"$@" ||
shift $?
268 -A) dracut_args
="${dracut_args} --no-host-only";;
269 -B) skip_update_bootloader
=1;;
270 -v|
--verbose) dracut_args
="${dracut_args} -v";;
272 -h|
--help) usage
-n;;
273 -m) read_arg module_list
"$@" ||
shift $?
;;
274 -u) read_arg domu_module_list
"$@" ||
shift $?
275 echo "mkinitrd: DomU modules not yet supported" ;;
276 -d) read_arg rootfs
"$@" ||
shift $?
277 dracut_args
="${dracut_args} --filesystems $rootfs" ;;
278 -D) read_arg dhcp_if
"$@" ||
shift $?
279 dracut_cmdline
="${dracut_cmdline} ip=${dhcp_if}:dhcp"
281 -I) read_arg static_if
"$@" ||
shift $?
282 dracut_cmdline
="${dracut_cmdline} ip=$(ipconfig $static_if)":
284 -a) read_arg acpi_dsdt
"$@" ||
shift $?
285 echo "Obsolete -a param, use acpi_table_dir= and acpi_override= variables in /etc/dracut.conf.d/"
288 -s) read_arg boot_splash
"$@" ||
shift $?
289 echo "mkinitrd: boot splash not yet supported"
292 -V) echo "mkinitrd: vendor scipts are no longer supported"
295 read_arg dracut_cmd
"$@" ||
shift $?
;;
297 echo "mkinitrd: dracut compatibility wrapper"
299 --quiet|
-q) quiet
=1;;
300 *) if [[ ! $targets ]]; then
302 elif [[ ! $kernels ]]; then
311 [[ $targets && $kernels ]] || default_kernel_images
312 if [[ ! $targets ||
! $kernels ]];then
313 error
"No kernel found in $boot_dir or bad modules dir in /lib/modules"
317 # We can have several targets/kernels, transform the list to an array
319 [[ $kernels ]] && kernels
=( $kernels )
321 [[ $logfile ]] && dracut_args
="${dracut_args} --logfile $logfile"
322 dracut_args
="${dracut_args} --force"
324 [[ $dracut_cmdline ]] && dracut_args
="${dracut_args} --kernel-cmdline ${dracut_cmdline}"
325 [ -z "$(type -p update-bootloader)" ] && skip_update_bootloader
=1
327 # Update defaults from /etc/sysconfig/kernel
328 if [ -f /etc
/sysconfig
/kernel
] ; then
329 .
/etc
/sysconfig
/kernel
331 [[ $module_list ]] || module_list
="${INITRD_MODULES}"
332 [[ $domu_module_list ]] || domu_module_list
="${DOMU_INITRD_MODULES}"
337 for ((i
=0 ; $i<${#targets[@]} ; i
++)); do
339 if [[ $img_vers ]];then
340 target
="${targets[$i]}-${kernels[$i]}"
342 target
="${targets[$i]}"
344 kernel
="${kernels[$i]}"
346 if is_xen_kernel
$kernel $rootfs ; then
347 modules_all
="${module_list} ${domu_module_list}"
349 modules_all
="${module_list}"
352 # Remove leading and trailing spaces needs (set above): shopt -s extglob
353 modules_all
=${modules_all%%+([[:space:]])}
354 modules_all
=${modules_all##+([[:space:]])}
356 echo "Creating initrd: $target"
358 # Duplicate code: No way found how to redirect output based on $quiet
359 if [[ $quiet == 1 ]];then
360 # Duplicate code: --force-drivers must not be called with empty string
361 # -> dracut bug workarounded ugly, because of complex whitespace
363 if [ -n "${modules_all}" ];then
364 $dracut_cmd $dracut_args --force-drivers "${modules_all}" "$target" "$kernel" &>/dev
/null
365 [ $?
-ne 0 ] && failed
="$failed $target"
367 $dracut_cmd $dracut_args "$target" "$kernel" &>/dev
/null
368 [ $?
-ne 0 ] && failed
="$failed $target"
371 if [ -n "${modules_all}" ];then
372 $dracut_cmd $dracut_args --force-drivers "${modules_all}" "$target" "$kernel"
373 [ $?
-ne 0 ] && failed
="$failed $target"
375 $dracut_cmd $dracut_args "$target" "$kernel"
376 [ $?
-ne 0 ] && failed
="$failed $target"
381 if [ "$skip_update_bootloader" ] ; then
382 echo 2>&1 "Did not refresh the bootloader. You might need to refresh it manually."
384 update-bootloader
--refresh
385 [ $?
-ne 0 ] && echo "Updating bootloader failed" && exit 1
388 if [ "$failed" != "" ]; then
389 echo "Generating $failed targets failed"