2009-10-09 01:11:09 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2011-10-17 16:09:15 +00:00
|
|
|
export ROOTDIR="$ROOTDIR"
|
|
|
|
|
2010-02-25 03:07:57 +00:00
|
|
|
INITRAMFS_TOOLS_VERSION=@VERSION@
|
2011-10-17 14:49:21 +00:00
|
|
|
STATEDIR=${ROOTDIR}/var/lib/initramfs-tools
|
|
|
|
BOOTDIR=${ROOTDIR}/boot
|
|
|
|
CONF=${ROOTDIR}/etc/initramfs-tools/update-initramfs.conf
|
2009-10-09 01:11:09 +00:00
|
|
|
mode=""
|
|
|
|
version=""
|
|
|
|
|
|
|
|
[ -r ${CONF} ] && . ${CONF}
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
2010-06-02 23:35:01 +00:00
|
|
|
if [ -n "${1:-}" ]; then
|
|
|
|
printf "${*}\n\n" >&2
|
2009-10-09 01:11:09 +00:00
|
|
|
fi
|
|
|
|
cat >&2 << EOF
|
|
|
|
Usage: ${0} [OPTION]...
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-k [version] Specify kernel version or 'all'
|
|
|
|
-c Create a new initramfs
|
|
|
|
-u Update an existing initramfs
|
|
|
|
-d Remove an existing initramfs
|
|
|
|
-t Take over a custom initramfs with this one
|
|
|
|
-b Set alternate boot directory
|
|
|
|
-v Be verbose
|
2010-02-25 03:07:57 +00:00
|
|
|
-V Show initramfs-tools version
|
2009-10-09 01:11:09 +00:00
|
|
|
-h This message
|
|
|
|
|
|
|
|
EOF
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# chroot check
|
|
|
|
chrooted()
|
|
|
|
{
|
|
|
|
# borrowed from udev's postinst
|
|
|
|
if [ "$(stat -c %d/%i /)" = "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
|
|
|
|
# the devicenumber/inode pair of / is the same as that of
|
|
|
|
# /sbin/init's root, so we're *not* in a chroot and hence
|
|
|
|
# return false.
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
mild_panic()
|
|
|
|
{
|
2010-06-02 23:35:01 +00:00
|
|
|
if [ -n "${1:-}" ]; then
|
|
|
|
printf "${*}\n" >&2
|
2009-10-09 01:11:09 +00:00
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
panic()
|
|
|
|
{
|
2010-06-02 23:35:01 +00:00
|
|
|
if [ -n "${1:-}" ]; then
|
|
|
|
printf "${*}\n" >&2
|
2009-10-09 01:11:09 +00:00
|
|
|
fi
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
verbose()
|
|
|
|
{
|
|
|
|
if [ "${verbose}" = 1 ]; then
|
2010-06-02 23:35:01 +00:00
|
|
|
printf "${*}\n"
|
2009-10-09 01:11:09 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
version_exists()
|
|
|
|
{
|
|
|
|
[ -e "${STATEDIR}/${1}" ] && [ -e "${initramfs}" ]
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
set_initramfs()
|
|
|
|
{
|
|
|
|
initramfs="${BOOTDIR}/initrd.img-${version}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# backup initramfs while running
|
|
|
|
backup_initramfs()
|
|
|
|
{
|
|
|
|
[ ! -r "${initramfs}" ] && return 0
|
|
|
|
initramfs_bak="${initramfs}.dpkg-bak"
|
|
|
|
[ -r "${initramfs_bak}" ] && rm -f "${initramfs_bak}"
|
|
|
|
ln -f "${initramfs}" "${initramfs_bak}" \
|
|
|
|
|| cp -a "${initramfs}" "${initramfs_bak}"
|
|
|
|
verbose "Keeping ${initramfs_bak}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# keep booted initramfs
|
|
|
|
backup_booted_initramfs()
|
|
|
|
{
|
|
|
|
initramfs_bak="${initramfs}.dpkg-bak"
|
|
|
|
|
|
|
|
# first time run thus no backup
|
|
|
|
[ ! -r "${initramfs_bak}" ] && return 0
|
|
|
|
|
|
|
|
# chroot with no /proc
|
|
|
|
[ ! -r /proc/uptime ] && rm -f "${initramfs_bak}" && return 0
|
|
|
|
|
|
|
|
# no kept backup wanted
|
|
|
|
[ "${backup_initramfs}" = "no" ] && rm -f "${initramfs_bak}" && return 0
|
|
|
|
|
|
|
|
# no backup yet
|
|
|
|
if [ ! -r "${initramfs}.bak" ]; then
|
|
|
|
mv -f ${initramfs_bak} "${initramfs}.bak"
|
|
|
|
verbose "Backup ${initramfs}.bak"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# keep booted initramfs
|
2010-06-02 23:35:01 +00:00
|
|
|
boot_initramfs=
|
2009-10-09 01:11:09 +00:00
|
|
|
uptime_days=$(awk '{printf "%d", $1 / 3600 / 24}' /proc/uptime)
|
|
|
|
if [ -n "$uptime_days" ]; then
|
|
|
|
boot_initramfs=$(find "${initramfs}.bak" -mtime +${uptime_days})
|
|
|
|
fi
|
|
|
|
if [ -n "${boot_initramfs}" ]; then
|
|
|
|
mv -f "${initramfs_bak}" "${initramfs}.bak"
|
|
|
|
verbose "Backup ${initramfs}.bak"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
verbose "Removing current backup ${initramfs_bak}"
|
|
|
|
rm -f ${initramfs_bak}
|
|
|
|
}
|
|
|
|
|
|
|
|
# nuke generated copy
|
2010-05-17 15:43:30 +00:00
|
|
|
remove_initramfs_bak()
|
2009-10-09 01:11:09 +00:00
|
|
|
{
|
2010-11-10 22:22:48 +00:00
|
|
|
[ -z "${initramfs_bak:-}" ] && return 0
|
2009-10-09 01:11:09 +00:00
|
|
|
rm -f "${initramfs_bak}"
|
|
|
|
verbose "Removing ${initramfs_bak}"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
generate_initramfs()
|
|
|
|
{
|
|
|
|
echo "update-initramfs: Generating ${initramfs}"
|
|
|
|
OPTS="-o"
|
|
|
|
if [ "${verbose}" = 1 ]; then
|
|
|
|
OPTS="-v ${OPTS}"
|
|
|
|
fi
|
|
|
|
if mkinitramfs ${OPTS} "${initramfs}.new" "${version}"; then
|
|
|
|
mv -f "${initramfs}.new" "${initramfs}"
|
|
|
|
set_sha1
|
|
|
|
else
|
|
|
|
mkinitramfs_return="$?"
|
2010-05-17 15:43:30 +00:00
|
|
|
remove_initramfs_bak
|
2009-10-09 01:11:09 +00:00
|
|
|
rm -f "${initramfs}.new"
|
|
|
|
if [ "$mkinitramfs_return" = "2" ]; then
|
|
|
|
# minversion wasn't met, exit 0
|
|
|
|
exit 0
|
|
|
|
fi
|
2010-11-10 22:22:48 +00:00
|
|
|
echo "update-initramfs: failed for ${initramfs}" >&2
|
2009-10-09 01:11:09 +00:00
|
|
|
exit $mkinitramfs_return
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
compare_sha1()
|
|
|
|
{
|
|
|
|
sha1sum "${initramfs}" | diff "${STATEDIR}/${version}" - >/dev/null 2>&1
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
# Note that this must overwrite so that updates work.
|
|
|
|
set_sha1()
|
|
|
|
{
|
|
|
|
sha1sum "${initramfs}" > "${STATEDIR}/${version}"
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_sha1()
|
|
|
|
{
|
|
|
|
rm -f "${STATEDIR}/${version}"
|
|
|
|
}
|
|
|
|
|
|
|
|
# ro /boot is not modified
|
|
|
|
ro_boot_check()
|
|
|
|
{
|
|
|
|
# check irrelevant inside of a chroot
|
|
|
|
if [ ! -r /proc/mounts ] || chrooted; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
boot_opts=$(awk '/boot/{if ((match($4, /^ro/) || match($4, /,ro/)) \
|
|
|
|
&& $2 == "/boot") print "ro"}' /proc/mounts)
|
|
|
|
if [ -n "${boot_opts}" ]; then
|
|
|
|
echo "WARNING: /boot is ro mounted."
|
|
|
|
echo "update-initramfs: Not updating ${initramfs}"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
get_sorted_versions()
|
|
|
|
{
|
|
|
|
version_list=""
|
|
|
|
|
|
|
|
for gsv_x in "${STATEDIR}"/*; do
|
|
|
|
gsv_x="$(basename "${gsv_x}")"
|
|
|
|
if [ "${gsv_x}" = '*' ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
worklist=""
|
|
|
|
for gsv_i in $version_list; do
|
2009-11-26 02:36:33 +00:00
|
|
|
xbps-uhelper cmpver "${gsv_x}" "${gsv_i}"
|
2009-10-09 01:11:09 +00:00
|
|
|
if [ $? -eq 0 ] || [ $? -eq 1 ]; then
|
|
|
|
worklist="${worklist} ${gsv_x} ${gsv_i}"
|
|
|
|
gsv_x=""
|
|
|
|
else
|
|
|
|
worklist="${worklist} ${gsv_i}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ "${gsv_x}" != "" ]; then
|
|
|
|
worklist="${worklist} ${gsv_x}"
|
|
|
|
fi
|
|
|
|
version_list="${worklist}"
|
|
|
|
done
|
|
|
|
|
|
|
|
verbose "Available versions: ${version_list}"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_version()
|
|
|
|
{
|
|
|
|
if [ -f /boot/initrd.img-`uname -r` ]; then
|
|
|
|
version=`uname -r`
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
set_linked_version()
|
|
|
|
{
|
2010-06-02 23:35:01 +00:00
|
|
|
linktarget=
|
2009-10-09 01:11:09 +00:00
|
|
|
if [ -e /initrd.img ] && [ -L /initrd.img ]; then
|
|
|
|
linktarget="$(basename "$(readlink /initrd.img)")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e /boot/initrd.img ] && [ -L /boot/initrd.img ]; then
|
|
|
|
linktarget="$(basename "$(readlink /boot/initrd.img)")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${linktarget}" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
version="${linktarget##initrd.img-}"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_highest_version()
|
|
|
|
{
|
|
|
|
get_sorted_versions
|
2010-06-02 23:35:01 +00:00
|
|
|
if [ -z "${version_list}" ]; then
|
|
|
|
version=
|
|
|
|
return
|
|
|
|
fi
|
2009-10-09 01:11:09 +00:00
|
|
|
set -- ${version_list}
|
|
|
|
version=${1}
|
|
|
|
}
|
|
|
|
|
|
|
|
create()
|
|
|
|
{
|
|
|
|
if [ -z "${version}" ]; then
|
|
|
|
usage "Create mode requires a version argument"
|
|
|
|
fi
|
|
|
|
|
|
|
|
set_initramfs
|
|
|
|
|
|
|
|
if [ "${takeover}" = 0 ]; then
|
|
|
|
if version_exists "${version}"; then
|
|
|
|
panic "Cannot create version ${version}: already exists"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -e "${initramfs}" ]; then
|
|
|
|
panic "${initramfs} already exists, cannot create."
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
generate_initramfs
|
|
|
|
}
|
|
|
|
|
|
|
|
update()
|
|
|
|
{
|
|
|
|
if [ "${update_initramfs}" = "no" ]; then
|
|
|
|
echo "update-initramfs: Not updating initramfs."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${version}" ]; then
|
|
|
|
set_highest_version
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${version}" ]; then
|
|
|
|
set_linked_version
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${version}" ]; then
|
|
|
|
set_current_version
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "${version}" ]; then
|
|
|
|
verbose "Nothing to do, exiting."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
set_initramfs
|
|
|
|
|
|
|
|
ro_boot_check
|
|
|
|
|
|
|
|
altered_check
|
|
|
|
|
|
|
|
backup_initramfs
|
|
|
|
|
|
|
|
generate_initramfs
|
|
|
|
|
|
|
|
backup_booted_initramfs
|
|
|
|
}
|
|
|
|
|
|
|
|
delete()
|
|
|
|
{
|
|
|
|
if [ -z "${version}" ]; then
|
|
|
|
usage "Delete mode requires a version argument"
|
|
|
|
fi
|
|
|
|
|
|
|
|
set_initramfs
|
|
|
|
|
2010-05-17 15:43:30 +00:00
|
|
|
if [ "${takeover}" = 0 ]; then
|
|
|
|
if [ ! -e "${initramfs}" ]; then
|
|
|
|
panic "Cannot delete ${initramfs}, doesn't exist."
|
|
|
|
fi
|
2009-10-09 01:11:09 +00:00
|
|
|
|
2010-05-17 15:43:30 +00:00
|
|
|
if ! version_exists "${version}"; then
|
|
|
|
panic "Cannot delete version ${version}: Not created by this utility."
|
|
|
|
fi
|
2009-10-09 01:11:09 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
altered_check
|
|
|
|
|
|
|
|
echo "update-initramfs: Deleting ${initramfs}"
|
|
|
|
|
|
|
|
delete_sha1
|
|
|
|
|
2010-05-17 15:43:30 +00:00
|
|
|
rm -f "${initramfs}" "${initramfs}.bak"
|
2009-10-09 01:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check for update mode on existing and modified initramfs
|
|
|
|
altered_check()
|
|
|
|
{
|
|
|
|
# No check on takeover
|
|
|
|
[ "${takeover}" = 1 ] && return 0
|
|
|
|
if [ ! -e "${initramfs}" ]; then
|
|
|
|
mild_panic "${initramfs} does not exist. Cannot update."
|
|
|
|
fi
|
|
|
|
if ! compare_sha1; then
|
|
|
|
echo "update-initramfs: ${initramfs} has been altered." >&2
|
|
|
|
mild_panic "update-initramfs: Cannot update. Override with -t option."
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Defaults
|
|
|
|
verbose=0
|
|
|
|
yes=0
|
|
|
|
# We default to takeover=1 in Ubuntu, but not Debian
|
|
|
|
takeover=0
|
|
|
|
|
|
|
|
##
|
|
|
|
|
2011-07-09 22:02:10 +00:00
|
|
|
while getopts "k:cudyvtb:h?V" flag; do
|
2009-10-09 01:11:09 +00:00
|
|
|
case "${flag}" in
|
|
|
|
k)
|
|
|
|
version="${OPTARG}"
|
|
|
|
;;
|
|
|
|
c)
|
|
|
|
mode="c"
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
mode="d"
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
mode="u"
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
verbose="1"
|
|
|
|
;;
|
|
|
|
y)
|
|
|
|
yes="1"
|
|
|
|
;;
|
|
|
|
t)
|
|
|
|
takeover="1"
|
|
|
|
;;
|
|
|
|
b)
|
|
|
|
BOOTDIR="${OPTARG}"
|
|
|
|
if [ ! -d "${BOOTDIR}" ]; then
|
2010-11-10 22:22:48 +00:00
|
|
|
echo "Error: ${BOOTDIR} is not a directory." >&2
|
2009-10-09 01:11:09 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
2010-02-25 03:07:57 +00:00
|
|
|
V)
|
|
|
|
echo "${INITRAMFS_TOOLS_VERSION}"
|
|
|
|
exit 0
|
|
|
|
;;
|
2009-10-09 01:11:09 +00:00
|
|
|
h|?)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((${OPTIND} - 1))
|
|
|
|
|
|
|
|
if [ $# -ne 0 ]; then
|
2010-11-10 22:22:48 +00:00
|
|
|
echo "Invalid argument for option -k." >&2
|
2009-10-09 01:11:09 +00:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Validate arguments
|
|
|
|
if [ -z "${mode}" ]; then
|
|
|
|
usage "You must specify at least one of -c, -u, or -d."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${version}" = "all" ] \
|
|
|
|
|| ( [ "${update_initramfs}" = "all" ] && [ -z "${version}" ] ); then
|
|
|
|
: FIXME check for --yes, and if not ask are you sure
|
|
|
|
get_sorted_versions
|
|
|
|
if [ -z "${version_list}" ]; then
|
|
|
|
verbose "Nothing to do, exiting."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
OPTS="-b ${BOOTDIR}"
|
|
|
|
if [ "${verbose}" = "1" ]; then
|
|
|
|
OPTS="${OPTS} -v"
|
|
|
|
fi
|
|
|
|
if [ "${takeover}" = "1" ]; then
|
|
|
|
OPTS="${OPTS} -t"
|
|
|
|
fi
|
|
|
|
if [ "${yes}" = "1" ]; then
|
|
|
|
OPTS="${OPTS} -y"
|
|
|
|
fi
|
|
|
|
for u_version in ${version_list}; do
|
|
|
|
verbose "Execute: ${0} -${mode} -k \"${u_version}\" ${OPTS}"
|
|
|
|
"${0}" -${mode} -k "${u_version}" ${OPTS}
|
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
case "${mode}" in
|
|
|
|
c)
|
|
|
|
create
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
delete
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
update
|
|
|
|
;;
|
|
|
|
esac
|