2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
5 STATEDIR
=/var
/lib
/dracut
8 KPKGCONF
=/etc
/kernel-img.conf
16 && [ x
"$DPKG_MAINTSCRIPT_PACKAGE" != x
] \
19 && dpkg-trigger
--check-supported 2>/dev
/null
21 if dpkg-trigger
--no-await update-initramfs
; then
22 echo "dracut-update-initramfs: deferring update (trigger activated)"
29 if [ -n "${1}" ]; then
33 Usage: ${0} [OPTION]...
36 -k [version] Specify kernel version or 'all'
37 -c Create a new initramfs
38 -u Update an existing initramfs
39 -d Remove an existing initramfs
40 -t Take over a custom initramfs with this one
41 -b Set alternate boot directory
52 # borrowed from udev's postinst
53 if [ "$(stat -c %d/%i /)" = "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
54 # the devicenumber/inode pair of / is the same as that of
55 # /sbin/init's root, so we're *not* in a chroot and hence
64 if [ -n "${1}" ]; then
72 if [ -n "${1}" ]; then
80 if [ "${verbose}" = 1 ]; then
87 [ -e "${STATEDIR}/${1}" ] && [ -e "${initramfs}" ]
93 initramfs
="${BOOTDIR}/dracut.img-${version}"
97 # backup initramfs while running
100 [ ! -r "${initramfs}" ] && return 0
101 initramfs_bak
="${initramfs}.dpkg-bak"
102 [ -r "${initramfs_bak}" ] && rm -f "${initramfs_bak}"
103 ln -f "${initramfs}" "${initramfs_bak}" \
104 ||
cp -a "${initramfs}" "${initramfs_bak}"
105 verbose
"Keeping ${initramfs_bak}"
108 # keep booted initramfs
109 backup_booted_initramfs
()
111 initramfs_bak
="${initramfs}.dpkg-bak"
113 # first time run thus no backup
114 [ ! -r "${initramfs_bak}" ] && return 0
116 # chroot with no /proc
117 [ ! -r /proc
/uptime
] && rm -f "${initramfs_bak}" && return 0
119 # no kept backup wanted
120 [ "${backup_initramfs}" = "no" ] && rm -f "${initramfs_bak}" && return 0
123 if [ ! -r "${initramfs}.bak" ]; then
124 mv -f ${initramfs_bak} "${initramfs}.bak"
125 verbose
"Backup ${initramfs}.bak"
129 # keep booted initramfs
130 uptime_days
=$
(awk '{printf "%d", $1 / 3600 / 24}' /proc
/uptime
)
131 if [ -n "$uptime_days" ]; then
132 boot_initramfs
=$
(find "${initramfs}.bak" -mtime +${uptime_days})
134 if [ -n "${boot_initramfs}" ]; then
135 mv -f "${initramfs_bak}" "${initramfs}.bak"
136 verbose
"Backup ${initramfs}.bak"
139 verbose
"Removing current backup ${initramfs_bak}"
140 rm -f ${initramfs_bak}
143 # nuke generated copy
146 [ -z "${initramfs_bak}" ] && return 0
147 rm -f "${initramfs_bak}"
148 verbose
"Removing ${initramfs_bak}"
154 echo "dracut-update-initramfs: Generating ${initramfs}"
156 if [ "${verbose}" = 1 ]; then
160 if dracut
${OPTS} "${initramfs}.new" "${version}"; then
161 mv -f "${initramfs}.new
" "${initramfs}"
164 mkinitramfs_return="$?
"
166 rm -f "${initramfs}.new
"
167 if [ "$mkinitramfs_return" = "2" ]; then
168 # minversion wasn't met, exit 0
171 echo "update-initramfs
: failed
for ${initramfs}"
172 exit $mkinitramfs_return
179 # show lilo errors on failure
180 if ! lilo -t > /dev/null 2>&1 ; then
181 echo "ERROR lilo fails
for new
${initramfs}:"
188 # check if lilo is on mbr
191 # try to discover grub|grub2 and be happy
192 [ -r /boot/grub/grub.cfg ] \
193 && groot=$(awk '/^set root=/{print substr($2, 7, 3); exit}' \
195 [ -r /boot/grub/menu.lst ] \
196 && groot=$(awk '/^root/{print substr($2, 2, 3); exit}' \
198 [ -e /boot/grub/device.map ] && [ -n "${groot}" ] \
199 && dev=$(awk "/${groot}/{ print \
$NF}" /boot/grub/device.map)
200 [ -n "${dev}" ] && [ -r ${dev} ] \
201 && dd if="${dev}" bs=512 skip=0 count=1 2> /dev/null \
202 | grep -q GRUB && return 0
204 # check out lilo.conf for validity
205 boot=$(awk -F = '/^boot=/{ print $2}' /etc/lilo.conf)
206 [ -z "${boot}" ] && return 0
209 if [ -r /proc/mdstat ]; then
211 boot="/dev
/$
(awk "/^md${MD}/{print substr(\$5, 1, 3)}" \
216 if [ -r /proc/mdstat ]; then
218 boot="/dev
/$
(awk "/^${MD}/{print substr(\$5, 1, 3)}" \
223 [ ! -r "${boot}" ] && return 0
224 dd if="${boot}" bs=512 skip=0 count=1 2> /dev/null | grep -q LILO \
225 && run_lilo && return 0
227 # no idea which bootloader is used
229 echo "WARNING
: grub and lilo installed.
"
230 echo "If you use grub as bootloader everything is fine.
"
231 echo "If you use lilo as bootloader you must run lilo
!"
238 # if both lilo and grub around, figure out if lilo needs to be run
239 if ( command -v update-grub >/dev/null 2>&1 \
240 || [ -e /boot/grub/menu.lst ] || [ -e /boot/grub/grub.cfg ] ) \
241 && ( [ -e /etc/lilo.conf ] && command -v lilo >/dev/null 2>&1 ); then
242 [ -r "${KPKGCONF}" ] && \
243 do_b=$(awk '/^do_bootloader/{print $3}' "${KPKGCONF}")
244 if [ "${do_b}" = "yes" ] || [ "${do_b}" = "Yes
" ] \
245 || [ "${do_b}" = "YES
" ]; then
248 elif [ "${do_b}" = "no
" ] || [ "${do_b}" = "No
" ] \
249 || [ "${do_b}" = "NO
" ]; then
253 # do_bootloader unconfigured
257 if [ -r /etc/lilo.conf ] && command -v lilo >/dev/null 2>&1; then
261 if command -v elilo >/dev/null 2>&1; then
265 if [ -r /etc/zipl.conf ]; then
268 if flash-kernel --supported >/dev/null 2>&1; then
269 flash-kernel ${version}
275 sha1sum "${initramfs}" | diff "${STATEDIR}/${version}" - >/dev/null 2>&1
279 # Note that this must overwrite so that updates work.
282 sha1sum "${initramfs}" > "${STATEDIR}/${version}"
287 rm -f "${STATEDIR}/${version}"
290 # ro /boot is not modified
293 # check irrelevant inside of a chroot
294 if [ ! -r /proc/mounts ] || chrooted; then
298 boot_opts=$(awk '/boot/{if ((match($4, /^ro/) || match($4, /,ro/)) \
299 && $2 == "/boot
") print "ro
"}' /proc/mounts)
300 if [ -n "${boot_opts}" ]; then
301 echo "WARNING
: /boot is ro mounted.
"
302 echo "update-initramfs
: Not updating
${initramfs}"
307 get_sorted_versions()
311 for gsv_x in "${STATEDIR}"/*; do
312 gsv_x="$
(basename "${gsv_x}")"
313 if [ "${gsv_x}" = '*' ]; then
317 for gsv_i in $version_list; do
318 if dpkg --compare-versions "${gsv_x}" '>' "${gsv_i}"; then
319 worklist="${worklist} ${gsv_x} ${gsv_i}"
322 worklist="${worklist} ${gsv_i}"
325 if [ "${gsv_x}" != "" ]; then
326 worklist="${worklist} ${gsv_x}"
328 version_list="${worklist}"
331 verbose "Available versions
: ${version_list}"
334 set_current_version()
336 if [ -f /boot/dracut.img-`uname -r` ]; then
343 if [ -e /initrd.img ] && [ -L /initrd.img ]; then
344 linktarget="$
(basename "$(readlink /initrd.img)")"
347 if [ -e /boot/initrd.img ] && [ -L /boot/initrd.img ]; then
348 linktarget="$
(basename "$(readlink /boot/initrd.img)")"
351 if [ -z "${linktarget}" ]; then
355 version="${linktarget##*img-}"
358 set_highest_version()
361 set -- ${version_list}
367 if [ -z "${version}" ]; then
368 usage "Create mode requires a version argument
"
373 if [ "${takeover}" = 0 ]; then
374 if version_exists "${version}"; then
375 panic "Cannot create version
${version}: already exists
"
378 if [ -e "${initramfs}" ]; then
379 panic "${initramfs} already exists
, cannot create.
"
388 if [ "${update_initramfs}" = "no
" ]; then
389 echo "update-initramfs
: Not updating initramfs.
"
393 if [ -z "${version}" ]; then
397 if [ -z "${version}" ]; then
401 if [ -z "${version}" ]; then
405 if [ -z "${version}" ]; then
406 verbose "Nothing to
do, exiting.
"
422 backup_booted_initramfs
427 if [ -z "${version}" ]; then
428 usage "Delete mode requires a version argument
"
433 if [ ! -e "${initramfs}" ]; then
434 panic "Cannot delete
${initramfs}, doesn
't exist."
437 if ! version_exists "${version}"; then
438 panic "Cannot delete version ${version}: Not created by this utility."
443 echo "update-initramfs: Deleting ${initramfs}"
450 # Check for update mode on existing and modified initramfs
453 # No check on takeover
454 [ "${takeover}" = 1 ] && return 0
455 if [ ! -e "${initramfs}" ]; then
456 mild_panic "${initramfs} does not exist. Cannot update."
458 if ! compare_sha1; then
459 echo "update-initramfs: ${initramfs} has been altered." >&2
460 mild_panic "update-initramfs: Cannot update. Override with -t option."
467 # We default to takeover=1 in Ubuntu, but not Debian
472 while getopts "k:cudyvtb:h?" flag; do
497 if [ ! -d "${BOOTDIR}" ]; then
498 echo "Error: ${BOOTDIR} is not a directory."
508 shift $((${OPTIND} - 1))
510 if [ $# -ne 0 ]; then
511 echo "Invalid argument for option -k."
516 if [ -z "${mode}" ]; then
517 usage "You must specify at least one of -c, -u, or -d."
520 if [ "${version}" = "all" ] \
521 || ( [ "${update_initramfs}" = "all" ] && [ -z "${version}" ] ); then
522 : FIXME check for --yes, and if not ask are you sure
524 if [ -z "${version_list}" ]; then
525 verbose "Nothing to do, exiting."
530 if [ "${verbose}" = "1" ]; then
533 if [ "${takeover}" = "1" ]; then
536 if [ "${yes}" = "1" ]; then
539 for u_version in ${version_list}; do
540 # Don't stop
if one version doesn
't work.
542 verbose "Execute: ${0} -${mode} -k \"${u_version}\" ${OPTS}"
543 "${0}" -${mode} -k "${u_version}" ${OPTS}