3 # mkinitrd compability 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 " enviroment 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
=$
(echo "(2 ^ 32) - (2 ^ $prefix)" |
bc -l)
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*}"
149 # Taken over from SUSE mkinitrd
150 default_kernel_images
() {
151 local regex kernel_image kernel_version version_version initrd_image
152 local qf
='%{NAME}-%{VERSION}-%{RELEASE}\n'
154 case "$(uname -m)" in
176 for kernel_image
in $
(ls $boot_dir \
177 |
sed -ne "\|^$regex\(-[0-9.]\+-[0-9]\+-[a-z0-9]\+$\)\?|p" \
178 |
grep -v kdump$
) ; do
180 # Note that we cannot check the RPM database here -- this
181 # script is itself called from within the binary kernel
182 # packages, and rpm does not allow recursive calls.
184 [ -L "$boot_dir/$kernel_image" ] && continue
185 [ "${kernel_image%%.gz}" != "$kernel_image" ] && continue
186 kernel_version
=$
(/usr
/bin
/get_kernel_version \
187 $boot_dir/$kernel_image 2> /dev
/null
)
188 initrd_image
=$
(echo $kernel_image |
sed -e "s|${regex}|initrd|")
189 if [ "$kernel_image" != "$initrd_image" -a \
190 -n "$kernel_version" -a \
191 -d "/lib/modules/$kernel_version" ]; then
192 kernel_images
="$kernel_images $boot_dir/$kernel_image"
193 initrd_images
="$initrd_images $boot_dir/$initrd_image"
196 for kernel_image
in $kernel_images;do
197 kernels
="$kernels ${kernel_image#*-}"
199 for initrd_image
in $initrd_images;do
200 targets
="$targets $initrd_image"
206 -f) read_arg feature_list
"$@" ||
shift $?
207 # Could be several features
209 -k) # Would be nice to get a list of images here
210 read_arg kernel_images
"$@" ||
shift $?
211 for kernel_image
in $kernel_images;do
212 kernels
="$kernels ${kernel_image#*-}"
215 -i) read_arg initrd_images
"$@" ||
shift $?
216 for initrd_image
in $initrd_images;do
217 # Check if the initrd_image contains a path.
218 # if not, then add the default boot_dir
219 dname
=`dirname $initrd_image`
220 if [ "$dname" == "." ]; then
221 targets
="$targets $boot_dir/$initrd_image";
223 targets
="$targets $initrd_image";
227 -b) read_arg boot_dir
"$@" ||
shift $?
228 if [ ! -d $boot_dir ];then
229 error
"Boot directory $boot_dir does not exist"
233 -t) read_arg tmp_dir
"$@" ||
shift $?
234 dracut_args
="${dracut_args} --tmpdir $tmp_dir"
236 -M) read_arg map_file
"$@" ||
shift $?
239 -B) skip_update_bootloader
=1;;
240 -v|
--verbose) dracut_args
="${dracut_args} -v";;
242 -h|
--help) usage
-n;;
243 -m) read_arg module_list
"$@" ||
shift $?
;;
244 -u) read_arg domu_module_list
"$@" ||
shift $?
245 echo "mkinitrd: DomU modules not yet supported" ;;
246 -d) read_arg rootfs
"$@" ||
shift $?
247 dracut_args
="${dracut_args} --filesystems $rootfs" ;;
248 -D) read_arg dhcp_if
"$@" ||
shift $?
249 dracut_cmdline
="${dracut_cmdline} ip=${dhcp_if}:dhcp"
251 -I) read_arg static_if
"$@" ||
shift $?
252 dracut_cmdline
="${dracut_cmdline} ip=$(ipconfig $static_if)":
254 -a) read_arg acpi_dsdt
"$@" ||
shift $?
255 echo "Obsolete -a param, use acpi_table_dir= and acpi_override= variables in /etc/dracut.conf.d/"
258 -s) read_arg boot_splash
"$@" ||
shift $?
259 echo "mkinitrd: boot splash not yet supported"
262 -V) echo "mkinitrd: vendor scipts are no longer supported"
265 read_arg dracut_cmd
"$@" ||
shift $?
;;
267 echo "mkinitrd: dracut compatibility wrapper"
269 --quiet|
-q) quiet
=1;;
270 *) if [[ ! $targets ]]; then
272 elif [[ ! $kernels ]]; then
281 [[ $targets && $kernels ]] || default_kernel_images
282 if [[ ! $targets ||
! $kernels ]];then
283 error
"No kernel found in $boot_dir"
286 # We can have several targets/kernels, transform the list to an array
288 [[ $kernels ]] && kernels
=( $kernels )
290 [[ $logfile ]] && dracut_args
="${dracut_args} --logfile $logfile"
291 dracut_args
="${dracut_args} --force"
293 [[ $dracut_cmdline ]] && dracut_args
="${dracut_args} --kernel-cmdline ${dracut_cmdline}"
294 [ -z "$(type -p update-bootloader)" ] && skip_update_bootloader
=1
296 # Update defaults from /etc/sysconfig/kernel
297 if [ -f /etc
/sysconfig
/kernel
] ; then
298 .
/etc
/sysconfig
/kernel
300 [[ $module_list ]] || module_list
="${INITRD_MODULES}"
301 [[ $domu_module_list ]] || domu_module_list
="${DOMU_INITRD_MODULES}"
304 for ((i
=0 ; $i<${#targets[@]} ; i
++)); do
306 if [[ $img_vers ]];then
307 target
="${targets[$i]}-${kernels[$i]}"
309 target
="${targets[$i]}"
311 kernel
="${kernels[$i]}"
313 if is_xen_kernel
$kernel $rootfs ; then
314 modules_all
="${module_list} ${domu_module_list}"
316 modules_all
="${module_list}"
319 # Remove leading and trailing spaces needs (set above): shopt -s extglob
320 modules_all
=${modules_all%%+([[:space:]])}
321 modules_all
=${modules_all##+([[:space:]])}
323 echo "Creating initrd: $target"
325 # Duplicate code: No way found how to redirect output based on $quiet
326 if [[ $quiet == 1 ]];then
327 # Duplicate code: --force-drivers must not be called with empty string
328 # -> dracut bug workarounded ugly, because of complex whitespace
330 if [ -n "${modules_all}" ];then
331 $dracut_cmd $dracut_args --force-drivers "${modules_all}" "$target" "$kernel" &>/dev
/null
333 $dracut_cmd $dracut_args "$target" "$kernel" &>/dev
/null
336 if [ -n "${modules_all}" ];then
337 $dracut_cmd $dracut_args --force-drivers "${modules_all}" "$target" "$kernel"
339 $dracut_cmd $dracut_args "$target" "$kernel"
344 if [ "$skip_update_bootloader" ] ; then
345 echo 2>&1 "Did not refresh the bootloader. You might need to refresh it manually."
347 update-bootloader
--refresh