debug: add more tools to the dracut debug module
[dracut.git] / mkinitrd-suse.sh
blobaf42cbe68a241866e42161680ee892c7dea84eee
1 #!/bin/bash --norc
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/>.
21 boot_dir="/boot"
22 quiet=0
23 logfile=/var/log/YaST2/mkinitrd.log
24 dracut_cmd=dracut
26 error() { echo "$@" >&2; }
28 usage () {
29 [[ $1 = '-n' ]] && cmd=echo || cmd=error
31 $cmd "usage: ${0##*/} [options]"
32 $cmd ""
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"
35 $cmd " available."
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."
38 $cmd ""
39 $cmd " options:"
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
73 exit 1
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.
79 read_arg() {
80 # $1 = arg name
81 # $2 = arg value
82 # $3 = arg parameter
83 param="$1"
84 local rematch='^[^=]*=(.*)$' result
85 if [[ $2 =~ $rematch ]]; then
86 read "$param" <<< "${BASH_REMATCH[1]}"
87 else
88 for ((i=3; $i <= $#; i++)); do
89 # Only read next arg if it not an arg itself.
90 if [[ ${@:$i:1} = -* ]];then
91 break
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.
96 done
97 read "$1" <<< "$result"
98 return $(($i - 3))
102 # Helper functions to calculate ipconfig command line
103 calc_netmask() {
104 local prefix=$1
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);
114 echo $netmask
117 ipconfig() {
118 local interface=$1
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')
126 ipaddr=${iplink%%/*}
127 prefix=${iplink##*/}
128 netmask=$(calc_netmask $prefix)
130 echo "${ipaddr}:${serveraddr}:${gateway}:${netmask}:${hostname}:${interface}:none::${macaddr}"
133 is_xen_kernel() {
134 local kversion=$1
135 local root_dir=$2
136 local cfg
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
142 return
143 done
144 test $kversion != "${kversion%-xen*}"
145 return
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
155 s390|s390x)
156 regex='image'
158 ppc|ppc64)
159 regex='vmlinux'
161 i386|x86_64)
162 regex='vmlinuz'
164 arm*)
165 regex='[uz]Image'
167 aarch64)
168 regex='Image'
170 *) regex='vmlinu.'
172 esac
174 kernel_images=""
175 initrd_images=""
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"
195 done
196 for kernel_image in $kernel_images;do
197 kernels="$kernels ${kernel_image#*-}"
198 done
199 for initrd_image in $initrd_images;do
200 targets="$targets $initrd_image"
201 done
204 while (($# > 0)); do
205 case ${1%%=*} in
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#*-}"
213 done
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";
222 else
223 targets="$targets $initrd_image";
225 done
227 -b) read_arg boot_dir "$@" || shift $?
228 if [ ! -d $boot_dir ];then
229 error "Boot directory $boot_dir does not exist"
230 exit 1
233 -t) read_arg tmp_dir "$@" || shift $?
234 dracut_args="${dracut_args} --tmpdir $tmp_dir"
236 -M) read_arg map_file "$@" || shift $?
238 -A) host_only=0;;
239 -B) skip_update_bootloader=1;;
240 -v|--verbose) dracut_args="${dracut_args} -v";;
241 -L) logfile=;;
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/"
256 exit 1
258 -s) read_arg boot_splash "$@" || shift $?
259 echo "mkinitrd: boot splash not yet supported"
260 exit 1
262 -V) echo "mkinitrd: vendor scipts are no longer supported"
263 exit 1;;
264 --dracut)
265 read_arg dracut_cmd "$@" || shift $? ;;
266 --version|-R)
267 echo "mkinitrd: dracut compatibility wrapper"
268 exit 0;;
269 --quiet|-q) quiet=1;;
270 *) if [[ ! $targets ]]; then
271 targets=$1
272 elif [[ ! $kernels ]]; then
273 kernels=$1
274 else
275 usage
276 fi;;
277 esac
278 shift
279 done
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
287 targets=( $targets )
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}"
302 shopt -s extglob
304 for ((i=0 ; $i<${#targets[@]} ; i++)); do
306 if [[ $img_vers ]];then
307 target="${targets[$i]}-${kernels[$i]}"
308 else
309 target="${targets[$i]}"
311 kernel="${kernels[$i]}"
313 if is_xen_kernel $kernel $rootfs ; then
314 modules_all="${module_list} ${domu_module_list}"
315 else
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
329 # expansion magics
330 if [ -n "${modules_all}" ];then
331 $dracut_cmd $dracut_args --force-drivers "${modules_all}" "$target" "$kernel" &>/dev/null
332 else
333 $dracut_cmd $dracut_args "$target" "$kernel" &>/dev/null
335 else
336 if [ -n "${modules_all}" ];then
337 $dracut_cmd $dracut_args --force-drivers "${modules_all}" "$target" "$kernel"
338 else
339 $dracut_cmd $dracut_args "$target" "$kernel"
342 done
344 if [ "$skip_update_bootloader" ] ; then
345 echo 2>&1 "Did not refresh the bootloader. You might need to refresh it manually."
346 else
347 update-bootloader --refresh