cms/cmssetup.sh: fixed indention
[dracut.git] / mkinitrd-suse.sh
blob23dc2621701405d70c6d5c27605fa92e5b49a549
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 host_only=1
24 force=0
25 logfile=/var/log/YaST2/mkinitrd.log
26 dracut_cmd=dracut
28 error() { echo "$@" >&2; }
30 usage () {
31 [[ $1 = '-n' ]] && cmd=echo || cmd=error
33 $cmd "usage: ${0##*/} [options]"
34 $cmd ""
35 $cmd " Create initial ramdisk images that contain all kernel modules needed"
36 $cmd " in the early boot process, before the root file system becomes"
37 $cmd " available."
38 $cmd " This usually includes SCSI and/or RAID modules, a file system module"
39 $cmd " for the root file system, or a network interface driver module for dhcp."
40 $cmd ""
41 $cmd " options:"
42 $cmd " -f \"feature list\" Features to be enabled when generating initrd."
43 $cmd " Available features are:"
44 $cmd " iscsi, md, multipath, lvm, lvm2,"
45 $cmd " ifup, fcoe, dcbd"
46 $cmd " -k \"kernel list\" List of kernel images for which initrd files are"
47 $cmd " created. Defaults to all kernels found in /boot."
48 $cmd " -i \"initrd list\" List of file names for the initrd; position have"
49 $cmd " match to \"kernel list\". Defaults to all kernels"
50 $cmd " found in /boot."
51 $cmd " -b boot_dir Boot directory. Defaults to /boot."
52 $cmd " -t tmp_dir Temporary directory. Defaults to /var/tmp."
53 $cmd " -M map System.map file to use."
54 $cmd " -A Create a so called \"monster initrd\" which"
55 $cmd " includes all features and modules possible."
56 $cmd " -B Do not update bootloader configuration."
57 $cmd " -v Verbose mode."
58 $cmd " -L Disable logging."
59 $cmd " -h This help screen."
60 $cmd " -m \"module list\" Modules to include in initrd. Defaults to the"
61 $cmd " INITRD_MODULES variable in /etc/sysconfig/kernel"
62 $cmd " -u \"DomU module list\" Modules to include in initrd. Defaults to the"
63 $cmd " DOMU_INITRD_MODULES variable in"
64 $cmd " /etc/sysconfig/kernel."
65 $cmd " -d root_device Root device. Defaults to the device from"
66 $cmd " which / is mounted. Overrides the rootdev"
67 $cmd " enviroment variable if set."
68 $cmd " -j device Journal device"
69 $cmd " -D interface Run dhcp on the specified interface."
70 $cmd " -I interface Configure the specified interface statically."
71 $cmd " -a acpi_dsdt Attach compiled ACPI DSDT (Differentiated"
72 $cmd " System Description Table) to initrd. This"
73 $cmd " replaces the DSDT of the BIOS. Defaults to"
74 $cmd " the ACPI_DSDT variable in /etc/sysconfig/kernel."
75 $cmd " -s size Add splash animation and bootscreen to initrd."
77 [[ $1 = '-n' ]] && exit 0
78 exit 1
81 # Little helper function for reading args from the commandline.
82 # it automatically handles -a b and -a=b variants, and returns 1 if
83 # we need to shift $3.
84 read_arg() {
85 # $1 = arg name
86 # $2 = arg value
87 # $3 = arg parameter
88 param="$1"
89 local rematch='^[^=]*=(.*)$' result
90 if [[ $2 =~ $rematch ]]; then
91 read "$param" <<< "${BASH_REMATCH[1]}"
92 else
93 for ((i=3; $i <= $#; i++)); do
94 # Only read next arg if it not an arg itself.
95 if [[ ${@:$i:1} = -* ]];then
96 break
98 result="$result ${@:$i:1}"
99 # There is no way to shift our callers args, so
100 # return "no of args" to indicate they should do it instead.
101 done
102 read "$1" <<< "$result"
103 return $(($i - 3))
107 # Helper functions to calculate ipconfig command line
108 calc_netmask() {
109 local prefix=$1
111 [ -z "$prefix" ] && return
112 mask=$(echo "(2 ^ 32) - (2 ^ $prefix)" | bc -l)
113 byte1=$(( mask >> 24 ))
114 byte2=$(( mask >> 16 ))
115 byte3=$(( mask >> 8 ))
116 byte4=$(( mask & 0xff ))
117 netmask=$(printf "%d.%d.%d.%d" $(( byte1 & 0xff )) $(( byte2 & 0xff )) $(( byte3 & 0xff )) $byte4);
119 echo $netmask
122 ipconfig() {
123 local interface=$1
124 local iplink macaddr broadcast gateway ipaddr prefix netmask
126 iplink=$(ip addr show dev $interface | sed -n 's/ *inet \(.*\) brd.*/\1/p')
127 macaddr=$(ip addr show dev $interface | sed -n 's/.*ether \(.*\) brd.*/\1/p')
128 broadcast=$(ip addr show dev $interface | sed -n 's/.*brd \(.*\) scope.*/\1/p')
129 gateway=$(ip route show dev $interface | sed -n 's/default via \([0-9\.]*\).*/\1/p')
131 ipaddr=${iplink%%/*}
132 prefix=${iplink##*/}
133 netmask=$(calc_netmask $prefix)
135 echo "${ipaddr}:${serveraddr}:${gateway}:${netmask}:${hostname}:${interface}:none::${macaddr}"
138 is_xen_kernel() {
139 local kversion=$1
140 local root_dir=$2
141 local cfg
143 for cfg in ${root_dir}/boot/config-$kversion $root_dir/lib/modules/$kversion/build/.config
145 test -r $cfg || continue
146 grep -q "^CONFIG_XEN=y\$" $cfg
147 return
148 done
149 test $kversion != "${kversion%-xen*}"
150 return
154 # Taken over from SUSE mkinitrd
155 default_kernel_images() {
156 local regex kernel_image kernel_version version_version initrd_image
157 local qf='%{NAME}-%{VERSION}-%{RELEASE}\n'
159 case "$(uname -m)" in
160 s390|s390x)
161 regex='image'
163 ppc|ppc64)
164 regex='vmlinux'
166 i386|x86_64)
167 regex='vmlinuz'
169 arm*)
170 regex='[uz]Image'
172 aarch64)
173 regex='Image'
175 *) regex='vmlinu.'
177 esac
179 kernel_images=""
180 initrd_images=""
181 for kernel_image in $(ls $boot_dir \
182 | sed -ne "\|^$regex\(-[0-9.]\+-[0-9]\+-[a-z0-9]\+$\)\?|p" \
183 | grep -v kdump$ ) ; do
185 # Note that we cannot check the RPM database here -- this
186 # script is itself called from within the binary kernel
187 # packages, and rpm does not allow recursive calls.
189 [ -L "$boot_dir/$kernel_image" ] && continue
190 [ "${kernel_image%%.gz}" != "$kernel_image" ] && continue
191 kernel_version=$(/usr/bin/get_kernel_version \
192 $boot_dir/$kernel_image 2> /dev/null)
193 initrd_image=$(echo $kernel_image | sed -e "s|${regex}|initrd|")
194 if [ "$kernel_image" != "$initrd_image" -a \
195 -n "$kernel_version" -a \
196 -d "/lib/modules/$kernel_version" ]; then
197 kernel_images="$kernel_images $boot_dir/$kernel_image"
198 initrd_images="$initrd_images $boot_dir/$initrd_image"
200 done
201 for kernel_image in $kernel_images;do
202 kernels="$kernels ${kernel_image#*-}"
203 done
204 for initrd_image in $initrd_images;do
205 targets="$targets $initrd_image"
206 done
207 host_only=1
208 force=1
211 while (($# > 0)); do
212 case ${1%%=*} in
213 -f) read_arg feature_list "$@" || shift $?
214 # Could be several features
216 -k) # Would be nice to get a list of images here
217 read_arg kernel_images "$@" || shift $?
218 for kernel_image in $kernel_images;do
219 kernels="$kernels ${kernel_image#*-}"
220 done
221 host_only=1
222 force=1
224 -i) read_arg initrd_images "$@" || shift $?
225 for initrd_image in $initrd_images;do
226 # Check if the initrd_image contains a path.
227 # if not, then add the default boot_dir
228 dname=`dirname $initrd_image`
229 if [ "$dname" == "." ]; then
230 targets="$targets $boot_dir/$initrd_image";
231 else
232 targets="$targets $initrd_image";
234 done
236 -b) read_arg boot_dir "$@" || shift $?
237 if [ ! -d $boot_dir ];then
238 error "Boot directory $boot_dir does not exist"
239 exit 1
242 -t) read_arg tmp_dir "$@" || shift $?
243 dracut_args="${dracut_args} --tmpdir $tmp_dir"
245 -M) read_arg map_file "$@" || shift $?
247 -A) host_only=0;;
248 -B) skip_update_bootloader=1;;
249 -v|--verbose) dracut_args="${dracut_args} -v";;
250 -L) logfile=;;
251 -h|--help) usage -n;;
252 -m) read_arg module_list "$@" || shift $? ;;
253 -u) read_arg domu_module_list "$@" || shift $?
254 echo "mkinitrd: DomU modules not yet supported" ;;
255 -d) read_arg rootfs "$@" || shift $?
256 dracut_args="${dracut_args} --filesystems $rootfs" ;;
257 -D) read_arg dhcp_if "$@" || shift $?
258 dracut_cmdline="${dracut_cmdline} ip=${dhcp_if}:dhcp"
260 -I) read_arg static_if "$@" || shift $?
261 dracut_cmdline="${dracut_cmdline} ip=$(ipconfig $static_if)":
263 -a) read_arg acpi_dsdt "$@" || shift $?
264 echo "mkinitrd: custom DSDT not yet supported"
265 exit 1
267 -s) read_arg boot_splash "$@" || shift $?
268 echo "mkinitrd: boot splash not yet supported"
269 exit 1
271 -V) echo "mkinitrd: vendor scipts are no longer supported"
272 exit 1;;
273 --dracut)
274 read_arg dracut_cmd "$@" || shift $? ;;
275 --version|-R)
276 echo "mkinitrd: dracut compatibility wrapper"
277 exit 0;;
278 --force) force=1;;
279 --quiet|-q) quiet=1;;
280 *) if [[ ! $targets ]]; then
281 targets=$1
282 elif [[ ! $kernels ]]; then
283 kernels=$1
284 else
285 usage
286 fi;;
287 esac
288 shift
289 done
291 [[ $targets && $kernels ]] || default_kernel_images
292 [[ $targets && $kernels ]] || (error "No kernel found in $boot_dir" && usage)
294 # We can have several targets/kernels, transform the list to an array
295 targets=( $targets )
296 [[ $kernels ]] && kernels=( $kernels )
298 [[ $logfile ]] && dracut_args="${dracut_args} --logfile $logfile"
299 [[ $host_only == 1 ]] && dracut_args="${dracut_args} --hostonly"
300 [[ $force == 1 ]] && dracut_args="${dracut_args} --force"
301 [[ $dracut_cmdline ]] && dracut_args="${dracut_args} --kernel-cmdline ${dracut_cmdline}"
302 [ -z "$(type -p update-bootloader)" ] && skip_update_bootloader=1
304 # Update defaults from /etc/sysconfig/kernel
305 if [ -f /etc/sysconfig/kernel ] ; then
306 . /etc/sysconfig/kernel
308 [[ $module_list ]] || module_list="${INITRD_MODULES}"
309 basicmodules="$basicmodules ${module_list}"
310 [[ $domu_module_list ]] || domu_module_list="${DOMU_INITRD_MODULES}"
311 [[ $acpi_dsdt ]] || acpi_dsdt="${ACPI_DSDT}"
313 echo "Creating: target|kernel|dracut args|basicmodules "
314 for ((i=0 ; $i<${#targets[@]} ; i++)); do
316 if [[ $img_vers ]];then
317 target="${targets[$i]}-${kernels[$i]}"
318 else
319 target="${targets[$i]}"
321 kernel="${kernels[$i]}"
323 # Duplicate code: No way found how to redirect output based on $quiet
324 if [[ $quiet == 1 ]];then
325 echo "$target|$kernel|$dracut_args|$basicmodules"
326 if is_xen_kernel $kernel $rootfs ; then
327 basicmodules="$basicmodules ${domu_module_list}"
329 if [[ $basicmodules ]]; then
330 $dracut_cmd $dracut_args --add-drivers "$basicmodules" "$target" \
331 "$kernel" &>/dev/null
332 else
333 $dracut_cmd $dracut_args "$target" "$kernel" &>/dev/null
335 else
336 if is_xen_kernel $kernel $rootfs ; then
337 basicmodules="$basicmodules ${domu_module_list}"
339 if [[ $basicmodules ]]; then
340 $dracut_cmd $dracut_args --add-drivers "$basicmodules" "$target" \
341 "$kernel"
342 else
343 $dracut_cmd $dracut_args "$target" "$kernel"
346 done
348 if [ "$skip_update_bootloader" ] ; then
349 echo 2>&1 "Did not refresh the bootloader. You might need to refresh it manually."
350 else
351 update-bootloader --refresh