From 98f264fa20e59107fce8a79f527a902296c40732 Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Tue, 27 Aug 2013 21:16:29 +0200 Subject: [PATCH] initrd.scripts: move zfs functions to 00-zfs.sh --- defaults/initrd.d/00-zfs.sh | 96 +++++++++++++++++++++++++++++++++++++ defaults/initrd.scripts | 92 ++--------------------------------- 2 files changed, 99 insertions(+), 89 deletions(-) create mode 100755 defaults/initrd.d/00-zfs.sh diff --git a/defaults/initrd.d/00-zfs.sh b/defaults/initrd.d/00-zfs.sh new file mode 100755 index 0000000..4d48bbc --- /dev/null +++ b/defaults/initrd.d/00-zfs.sh @@ -0,0 +1,96 @@ +#!/bin/sh + +. /etc/initrd.d/00-common.sh + +is_zfs() { + [ "${USE_ZFS}" = "1" ] && return 0 + return 1 +} + +# This helper function is to be called using _call_func_timeout. This +# works around the inability of busybox modprobe to handle complex +# module dependencies. This also enables us to wait a reasonable +# amount of time until /dev/zfs appears. +wait_for_zfs() { + while [ ! -c /dev/zfs ]; do modprobe zfs 2> /dev/null; done; +} + +_call_func_timeout() { + local func=$1 timeout=$2 pid watcher + + ( ${func} ) & pid=$! + ( sleep ${timeout} && kill -HUP ${pid} ) 2>/dev/null & watcher=$! + if wait ${pid} 2>/dev/null; then + kill -HUP $watcher 2> /dev/null + wait $watcher 2>/dev/null + return 1 + fi + + return 0 +} + +zfs_init() { + # Set variables based on the value of REAL_ROOT + case "${REAL_ROOT}" in + ZFS=*) + ZFS_POOL=${REAL_ROOT#*=} + ZFS_POOL=${ZFS_POOL%%/*} + USE_ZFS=1 + ;; + ZFS) + USE_ZFS=1 + ;; + esac + + # Verify that it is safe to use ZFS + if [ "USE_ZFS" = "1" ] + then + for i in /sbin/zfs /sbin/zpool + do + if [ ! -x ${i} ] + then + USE_ZFS=0 + bad_msg 'Aborting use of zfs because ${i} not found!' + break + fi + done + fi +} + +zfs_start_volumes() { + # Avoid race involving asynchronous module loading + if _call_func_timeout wait_for_zfs 5; then + bad_msg "Cannot import ZFS pool because /dev/zfs is missing" + + elif [ -z "${ZFS_POOL}" ]; then + good_msg "Importing ZFS pools" + + /sbin/zpool import -N -a ${ZPOOL_FORCE} + if [ "${?}" = "0" ]; then + good_msg "Importing ZFS pools succeeded" + else + bad_msg "Imported ZFS pools failed" + fi + + else + local pools=$(zpool list -H -o name ${ZFS_POOL} 2>&1) + if [ "${pools}" = "${ZFS_POOL}" ]; then + good_msg "ZFS pool ${ZFS_POOL} already imported." + + if [ -n "${CRYPT_ROOT}" ] || [ -n "${CRYPT_SWAP}" ]; then + good_msg "LUKS detected. Reimporting ${ZFS_POOL}" + /sbin/zpool export -f "${ZFS_POOL}" + /sbin/zpool import -N ${ZPOOL_FORCE} "${ZFS_POOL}" + fi + else + good_msg "Importing ZFS pool ${ZFS_POOL}" + /sbin/zpool import -N ${ZPOOL_FORCE} "${ZFS_POOL}" + + if [ "${?}" = "0" ]; then + good_msg "Import of ${ZFS_POOL} succeeded" + else + bad_msg "Import of ${ZFS_POOL} failed" + fi + fi + fi +} \ No newline at end of file diff --git a/defaults/initrd.scripts b/defaults/initrd.scripts index 971ffe7..48b6687 100755 --- a/defaults/initrd.scripts +++ b/defaults/initrd.scripts @@ -2,25 +2,11 @@ . /etc/initrd.defaults . /etc/initrd.d/00-splash.sh -. /etc/initrd.d/00-devmgr.sh . /etc/initrd.d/00-common.sh +. /etc/initrd.d/00-devmgr.sh +. /etc/initrd.d/00-zfs.sh -call_func_timeout() { - local func=$1 timeout=$2 pid watcher - [ $# -ne 2 ] && gen_die "call_func_timeout() called with $# arguments" - - ( ${func} ) & pid=$! - ( sleep ${timeout} && kill -HUP ${pid} ) 2>/dev/null & watcher=$! - if wait ${pid} 2>/dev/null; then - kill -HUP $watcher 2> /dev/null - wait $watcher 2>/dev/null - return 1 - fi - - return 0 -} - modules_load() { for module in $* do @@ -718,42 +704,6 @@ choose_keymap() { fi } -# This helper function is to be called using call_func_timeout. -# This works around the inability of busybox modprobe to handle complex module -# dependencies. This also enables us to wait a reasonable amount of time until -# /dev/zfs appears. -wait_for_zfs() { - while [ ! -c /dev/zfs ]; do modprobe zfs 2> /dev/null; done; -} - -zfs_init() { - # Set variables based on the value of REAL_ROOT - case "${REAL_ROOT}" in - ZFS=*) - ZFS_POOL=${REAL_ROOT#*=} - ZFS_POOL=${ZFS_POOL%%/*} - USE_ZFS=1 - ;; - ZFS) - USE_ZFS=1 - ;; - esac - - # Verify that it is safe to use ZFS - if [ "USE_ZFS" = "1" ] - then - for i in /sbin/zfs /sbin/zpool - do - if [ ! -x ${i} ] - then - USE_ZFS=0 - bad_msg 'Aborting use of zfs because ${i} not found!' - break - fi - done - fi -} - start_volumes() { # Here, we check for /dev/device-mapper, and if it exists, we setup a # a symlink, which should hopefully fix bug #142775 and bug #147015 @@ -822,43 +772,7 @@ start_volumes() { fi fi - if [ "${USE_ZFS}" = "1" ]; then - # Avoid race involving asynchronous module loading - if call_func_timeout wait_for_zfs 5; then - bad_msg "Cannot import ZFS pool because /dev/zfs is missing" - - elif [ -z "${ZFS_POOL}" ]; then - good_msg "Importing ZFS pools" - - /sbin/zpool import -N -a ${ZPOOL_FORCE} - if [ "${?}" = "0" ]; then - good_msg "Importing ZFS pools succeeded" - else - bad_msg "Imported ZFS pools failed" - fi - - else - local pools=$(zpool list -H -o name ${ZFS_POOL} 2>&1) - if [ "${pools}" = "${ZFS_POOL}" ]; then - good_msg "ZFS pool ${ZFS_POOL} already imported." - - if [ -n "${CRYPT_ROOT}" ] || [ -n "${CRYPT_SWAP}" ]; then - good_msg "LUKS detected. Reimporting ${ZFS_POOL}" - /sbin/zpool export -f "${ZFS_POOL}" - /sbin/zpool import -N ${ZPOOL_FORCE} "${ZFS_POOL}" - fi - else - good_msg "Importing ZFS pool ${ZFS_POOL}" - /sbin/zpool import -N ${ZPOOL_FORCE} "${ZFS_POOL}" - - if [ "${?}" = "0" ]; then - good_msg "Import of ${ZFS_POOL} succeeded" - else - bad_msg "Import of ${ZFS_POOL} failed" - fi - fi - fi - fi + is_zfs && zfs_start_volumes is_udev && udevadm settle }