dmsquash-live/apply-live-updates.sh: needs 'find'
[dracut.git] / mkinitrd-dracut.sh
blobda34e34f32c0fb92e6fc202c5ad3f6a208f91bdf
1 #!/bin/bash --norc
2 kver=$(uname -r)
4 boot_dir="/boot"
5 quiet=0
6 host_only=0
7 force=0
9 error() { echo "$@" >&2; }
11 usage () {
12 [[ $1 = '-n' ]] && cmd=echo || cmd=error
14 $cmd "usage: ${0##*/} [--version] [--help] [-v] [-f] [--preload <module>]"
15 $cmd " [--image-version] [--with=<module>]"
16 $cmd " [--nocompress]"
17 $cmd " <initrd-image> <kernel-version>"
18 $cmd ""
19 $cmd " (ex: ${0##*/} /boot/initramfs-$kver.img $kver)"
21 [[ $1 = '-n' ]] && exit 0
22 exit 1
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
32 param="$1"
33 local rematch='^[^=]*=(.*)$' result
34 if [[ $2 =~ $rematch ]]; then
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
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))
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'
56 case "$(uname -m)" in
57 s390|s390x)
58 regex='image'
60 ppc|ppc64)
61 regex='vmlinux'
63 i386|x86_64)
64 regex='vmlinuz'
66 arm*)
67 regex='[uz]Image'
69 aarch64)
70 regex='Image'
72 *) regex='vmlinu.'
74 esac
76 # user mode linux
77 if grep -q UML /proc/cpuinfo; then
78 regex='linux'
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
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.
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"
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
109 host_only=1
110 force=1
113 while (($# > 0)); do
114 case ${1%%=*} in
115 --with-usb) read_arg usbmodule "$@" || shift $?
116 basicmodules="$basicmodules ${usbmodule:-usb-storage}"
117 unset usbmodule;;
118 --with-avail) read_arg modname "$@" || shift $?
119 basicmodules="$basicmodules $modname";;
120 --with) read_arg modname "$@" || shift $?
121 basicmodules="$basicmodules $modname";;
122 --version)
123 echo "mkinitrd: dracut compatibility wrapper"
124 exit 0;;
125 -v|--verbose) dracut_args="${dracut_args} -v";;
126 -f|--force) force=1;;
127 --preload) read_arg modname "$@" || shift $?
128 basicmodules="$basicmodules $modname";;
129 --image-version) img_vers=yes;;
130 --rootfs|-d) read_arg rootfs "$@" || shift $?
131 dracut_args="${dracut_args} --filesystems $rootfs";;
132 --nocompress) dracut_args="$dracut_args --no-compress";;
133 --help) usage -n;;
134 --builtin) ;;
135 --without*) ;;
136 --without-usb) ;;
137 --fstab*) ;;
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) ;;
147 --rootdev*) ;;
148 --thawdev*) ;;
149 --rootopts*) ;;
150 --root*) ;;
151 --loopdev*) ;;
152 --loopfs*) ;;
153 --loopopts*) ;;
154 --looppath*) ;;
155 --dsdt*) ;;
156 --bootchart) ;;
157 -s) ;;
158 --quiet|-q) quiet=1;;
159 -b) read_arg boot_dir "$@" || shift $?
160 if [ ! -d $boot_dir ];then
161 error "Boot directory $boot_dir does not exist"
162 exit 1
165 -k) # Would be nice to get a list of images here
166 read_arg kernel_images "$@" || shift $?
167 for kernel_image in $kernel_images;do
168 kernels="$kernels ${kernel_image#*-}"
169 done
170 host_only=1
171 force=1
173 -i) read_arg initrd_images "$@" || shift $?
174 for initrd_image in $initrd_images;do
175 targets="$targets $boot_dir/$initrd_image"
176 done
178 *) if [[ ! $targets ]]; then
179 targets=$1
180 elif [[ ! $kernels ]]; then
181 kernels=$1
182 else
183 usage
184 fi;;
185 esac
186 shift
187 done
189 [[ $targets && $kernels ]] || default_kernel_images
190 [[ $targets && $kernels ]] || (error "No kernel found in $boot_dir" && usage)
192 # We can have several targets/kernels, transform the list to an array
193 targets=( $targets )
194 [[ $kernels ]] && kernels=( $kernels )
196 [[ $host_only == 1 ]] && dracut_args="${dracut_args} -H"
197 [[ $force == 1 ]] && dracut_args="${dracut_args} -f"
199 echo "Creating: target|kernel|dracut args|basicmodules "
200 for ((i=0 ; $i<${#targets[@]} ; i++)); do
202 if [[ $img_vers ]];then
203 target="${targets[$i]}-${kernels[$i]}"
204 else
205 target="${targets[$i]}"
207 kernel="${kernels[$i]}"
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
218 else
219 if [[ $basicmodules ]]; then
220 dracut $dracut_args --add-drivers "$basicmodules" "$target" \
221 "$kernel"
222 else
223 dracut $dracut_args "$target" "$kernel"
226 done