Flip awk return values.
[genkernel.git] / defaults / initrd.scripts
index da8cb017ba397f7d5560c4e71da507df9196ae07..ac32c8a3eeb90ef80030f0285e822c608527844f 100755 (executable)
@@ -53,11 +53,10 @@ findmediamount() {
        # $1 = mount dir name / media name
        # $2 = recognition file
        # $3 = variable to have the device path
-       # $4 = directory before /mnt, like NEW_ROOT
+       # $4 = actual mount dir path (full path)
        # args remaining are possible devices 
 
-       local media=$1 recon=$2 vrbl=$3
-       local mntdir="${4}/mnt/${media}"
+       local media=$1 recon=$2 vrbl=$3 mntdir=$4
        shift 4
 
        good_msg "Looking for the ${media}" ${CRYPT_SILENT}
@@ -89,7 +88,7 @@ findmediamount() {
                                # don't skip device mapper devices. Even the craziest scenario
                                # deserves a fair chance.
                                #
-                               for part in `ls /sys/block/${bsn}/${bsn}[0-9]* 2>/dev/null`
+                               for part in `ls /sys/block/${bsn}/${bsn}*[0-9]* 2>/dev/null`
                                do
                                        skip=1
                                        break;
@@ -98,13 +97,8 @@ findmediamount() {
                                then
                                        continue
                                fi
-                               good_msg "Attempting to mount media:- ${x}" ${CRYPT_SILENT}
+                               good_msg "Attempting to mount media: ${x}" ${CRYPT_SILENT}
 
-#                              if [ "${media}" = "cdrom" ]; then
-#                                      mount -r -t iso9660 ${x} ${mntdir} &>/dev/null
-#                              else
-#                                      mount -r -t auto ${x} ${mntdir} &>/dev/null
-#                              fi
                                mount -r -t ${CDROOT_TYPE} ${x} ${mntcddir} >/dev/null 2>&1
                                if [ "$?" = '0' ]
                                then
@@ -145,10 +139,17 @@ devicelist(){
        DEVICES="$DEVICES /dev/sd*"
        # IDE devices
        DEVICES="$DEVICES /dev/hd*"
+       # virtio devices
+       DEVICES="$DEVICES /dev/vd*"
        # USB using the USB Block Driver
        DEVICES="$DEVICES /dev/ubd* /dev/ubd/*"
        # iSeries devices
        DEVICES="$DEVICES /dev/iseries/vcd*"
+       # builtin mmc/sd card reader devices
+       DEVICES="$DEVICES /dev/mmcblk* /dev/mmcblk*/*"
+       # fallback scanning, this might scan something twice, but it's better than
+       # failing to boot.
+       [ -e /proc/partitions ] && DEVICES="${DEVICES} $(awk '/([0-9]+[[:space:]]+)/{print "/dev/" $4}' /proc/partitions)"
        echo ${DEVICES}
 }
 
@@ -158,7 +159,7 @@ bootstrapCD() {
        # a bunch of extra devices
        [ -n "${CDROOT_DEV}" ] && DEVICES="${CDROOT_DEV}"
 
-       findmediamount "cdrom" "${SUBDIR}/livecd" "REAL_ROOT" "${NEW_ROOT}" ${DEVICES}
+       findmediamount "cdrom" "${SUBDIR}/livecd" "REAL_ROOT" "${CDROOT_PATH}" ${DEVICES}
 }
 
 bootstrapKey() {
@@ -166,7 +167,7 @@ bootstrapKey() {
        local KEYDEVS=`devicelist`
        eval local keyloc='"${CRYPT_'${1}'_KEY}"'
 
-       findmediamount "key" "${keyloc}" "CRYPT_${1}_KEYDEV" "" ${KEYDEVS}
+       findmediamount "key" "${keyloc}" "CRYPT_${1}_KEYDEV" "/mnt/key" ${KEYDEVS}
 }
 
 cache_cd_contents() {
@@ -187,15 +188,14 @@ cache_cd_contents() {
                        good_msg "Copying loop file for caching..."
                        # Verify that the needed directory exists
                        mkdir -p "$(dirname ${NEW_ROOT}/mnt/${LOOP})"
-                       cp -a ${NEW_ROOT}/mnt/cdrom/${LOOP} ${NEW_ROOT}/mnt/${LOOP}
+                       cp -a ${CDROOT_PATH}/${LOOP} ${NEW_ROOT}/mnt/${LOOP}
                        if [ $? -ne 0 ]
                        then
-                               bad_msg "Failed to cache the loop file! Lack of space?"
+                               warn_msg "Failed to cache the loop file! Lack of RAM?"
+                               rm -rf ${NEW_ROOT}/mnt/${LOOP} 2>/dev/null
                                rm -rf ${NEW_ROOT}/mnt/livecd.* 2>/dev/null
                                rm -rf ${NEW_ROOT}/mnt/image.* 2>/dev/null
                                rm -rf ${NEW_ROOT}/mnt/zisofs 2>/dev/null
-                       else
-                               LOOPEXT='../'
                        fi
                fi
        fi
@@ -258,7 +258,7 @@ findnfsmount() {
                        if [ "${CDROOT}" != '0' ]
                        then
                                good_msg "Attempting to mount NFS CD image on ${NFSROOT} with options ${NFSOPTIONS}"
-                               mount -t nfs -o ${NFSOPTIONS} ${NFSROOT} ${NEW_ROOT}/mnt/cdrom
+                               mount -t nfs -o ${NFSOPTIONS} ${NFSROOT} ${CDROOT_PATH}
                                if [ "$?" = '0' ]
                                then
                                        REAL_ROOT="/dev/nfs"
@@ -283,7 +283,7 @@ findnfsmount() {
 }
 
 check_loop() {
-       if [ "${LOOP}" = '' -o ! -e "mnt/cdrom/${LOOP}" ]
+       if [ "${LOOP}" = '' -o ! -e "${CDROOT_PATH}/${LOOP}" ]
        then
        
                bad_msg "Invalid loop location: ${LOOP}"
@@ -298,7 +298,7 @@ run_shell() {
        /bin/ash
 }
 
-runmdev() {
+mount_devfs () {
        # Use devtmpfs if enabled in kernel,
        # else tmpfs. Always run mdev just in case
        devfs=tmpfs
@@ -307,13 +307,16 @@ runmdev() {
        fi
 
        # Options copied from /etc/init.d/udev-mount, should probably be kept in sync
-       mount -t $devfs -o "exec,nosuid,mode=0755,size=10M" udev /dev \
-               || bad_msg "Failed to mount /dev as ${devfs}"
+       if awk 'BEGIN{m=0;} /devtmpfs/{if($3 == "devtmpfs") {m=1;} } END { exit m;}' /proc/mounts; then
+               mount -t $devfs -o "exec,nosuid,mode=0755,size=10M" udev /dev \
+                       || bad_msg "Failed to mount /dev as ${devfs}"
+       fi
 
        # http://git.busybox.net/busybox/plain/docs/mdev.txt
-       mkdir -m 0755 /dev/pts
-       mount -t devpts -o gid=5,mode=0620 devpts /dev/pts  || bad_msg "Failed to mount /dev/pts"
-       mdev -s  || bad_msg "Failed to receive dynamic updates from mdev"
+       if awk 'BEGIN{m=0;} /devpts/{if($3 == "devpts") {m=1;} } END { exit m;}' /proc/mounts; then
+               mkdir -m 0755 /dev/pts
+               mount -t devpts -o gid=5,mode=0620 devpts /dev/pts  || bad_msg "Failed to mount /dev/pts"
+       fi
 }
 
 test_success() {
@@ -362,10 +365,12 @@ crypt_filter() {
        else
                splash 'verbose' > /dev/null &
                eval $1
-               if [ $? -eq 0 ]
+               res=$?
+               if [ ${res} -eq 0 ]
                then
                        splash set_msg 'Disk unlocked.'
                fi
+               return ${res}
        fi
 }
 
@@ -403,13 +408,6 @@ prompt_user(){
        esac
 }
 
-setup_hotplug() {
-       if [ "${KV_2_6_OR_GREATER}" ]
-       then
-               echo /sbin/mdev > /proc/sys/kernel/hotplug
-       fi
-}
-
 setup_slowusb() {
        # This function removes unset DO_slowusb if there is no usb-storage attached.
        # If noslowusb is set, skip this function
@@ -431,16 +429,6 @@ setup_slowusb() {
        done
 }
 
-start_dev_mgr() {
-       if [ "${KV_2_6_OR_GREATER}" ]
-       then
-               cd /sys
-               good_msg 'Activating mdev'
-               runmdev
-               cd /
-       fi
-}
-
 cmdline_hwopts() {
        # Scan CMDLINE for any "doscsi" or "noscsi"-type arguments
        local FOUND
@@ -573,6 +561,7 @@ chooseKeymap() {
                        40|uk) keymap=uk ;;
                        41|us) keymap=us ;;
                        42|wangbe) keymap=wangbe ;;
+                       43|sf|ch*) keymap=sf ;;
                esac
        fi
        if [ -e /lib/keymaps/${keymap}.map ]
@@ -583,7 +572,7 @@ chooseKeymap() {
 #              echo ${keymap} | egrep -e "[0-9]+" >/dev/null 2>&1
 #              if [ $? -eq 0 ]
 #              then
-#                      xkeymap=`tail -n 7 /lib/keymaps/keymapList | grep ${keymap} | sed -r "s/.*\s+${keymap}\s+([a-z-]+).*/\1/g" | egrep -v 1`
+#                      xkeymap=`tail -n 8 /lib/keymaps/keymapList | grep ${keymap} | sed -r "s/.*\s+${keymap}\s+([a-z-]+).*/\1/g" | egrep -v 1`
 #              fi
                mkdir -p /etc/sysconfig
 #              echo "XKEYBOARD=${xkeymap}" > /etc/sysconfig/keyboard
@@ -614,7 +603,12 @@ startVolumes() {
        
        if [ "${USE_MDADM}" = '1' ]
        then
-               /sbin/mdadm --assemble --scan
+               if [ -e '/sbin/mdadm' ]
+               then
+                       /sbin/mdadm --assemble --scan
+               else
+                       bad_msg "mdadm not found: skipping mdadm raid assembly!"
+               fi
        fi
 
        if [ "${USE_DMRAID_NORMAL}" = '1' ]
@@ -648,6 +642,11 @@ startVolumes() {
 
                        # To activate volumegroups on all devices in the cache
                        lvm_commands="${lvm_commands} \nvgchange -ay --sysinit"
+                       
+                       # To create symlinks so users can use real_root=/dev/vg/root
+                       # This needs to run after vgchange, using vgchange --mknodes is too
+                       # early.
+                       lvm_commands="${lvm_commands} \nvgmknodes --ignorelockingfailure"
 
                        # And finally execute it all (/proc/... needed if lvm is compiled without readline)
                        good_msg "Scanning for and activating Volume Groups"
@@ -656,6 +655,47 @@ startVolumes() {
                        bad_msg "vgscan or vgchange not found: skipping LVM volume group activation!"
                fi
        fi
+       
+       if [ "${USE_ZFS}" = '1' ]
+       then
+               if [ -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
+
+                       if [ "$(zpool list -H -o name ${ZFS_POOL} 2>&1)" = "$ZFS_POOL" ]
+                       then
+                               good_msg "ZFS pool ${ZFS_POOL} already imported."
+
+                               if [ -n "${CRYPT_ROOT}" -o -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 "Importing ${ZFS_POOL} succeeded"
+                               else
+                                       bad_msg "Importing ${ZFS_POOL} failed"
+                               fi
+                       fi
+               fi
+       fi
 }
 
 startiscsi() {
@@ -731,7 +771,7 @@ openLUKS() {
                        ;;
        esac
 
-       eval local LUKS_DEVICE='"${CRYPT_'${TYPE}'}"' LUKS_NAME="$1" LUKS_KEY='"${CRYPT_'${TYPE}'_KEY}"' LUKS_KEYDEV='"${CRYPT_'${TYPE}'_KEYDEV}"'
+       eval local LUKS_DEVICE='"${CRYPT_'${TYPE}'}"' LUKS_NAME="$1" LUKS_KEY='"${CRYPT_'${TYPE}'_KEY}"' LUKS_KEYDEV='"${CRYPT_'${TYPE}'_KEYDEV}"' LUKS_TRIM='"${CRYPT_'${TYPE}'_TRIM}"'
        local DEV_ERROR=0 KEY_ERROR=0 KEYDEV_ERROR=0
        local mntkey="/mnt/key/" cryptsetup_options=''
 
@@ -772,7 +812,7 @@ openLUKS() {
                                        fi
 
                                        if [ "${retval}" -ne 0 ]; then
-                                               REAL_LUKS=`blkid -l -t "${LUKS_DEVICE}" | cut -d ":" -f 1 2>/dev/null`
+                                               REAL_LUKS=`blkid -o device -l -t "${LUKS_DEVICE}"`
                                                retval=$?
                                        fi
 
@@ -792,6 +832,12 @@ openLUKS() {
                                continue
                        else
                                # Handle keys
+                               if [ "x${LUKS_TRIM}" = "xyes" ]
+                               then
+                                       good_msg "Enabling TRIM support for ${LUKS_NAME}." ${CRYPT_SILENT}
+                                       cryptsetup_options="${cryptsetup_options} --allow-discards"
+                               fi
+
                                if [ -n "${LUKS_KEY}" ] 
                                then
                                        if [ ! -e "${mntkey}${LUKS_KEY}" ] 
@@ -861,7 +907,13 @@ openLUKS() {
                                fi
                                # At this point, keyfile or not, we're ready!
                                crypt_filter "${gpg_cmd}cryptsetup ${cryptsetup_options} luksOpen ${LUKS_DEVICE} ${LUKS_NAME}"
-                               if [ $? -eq 0 ]
+                               crypt_filter_ret=$?
+
+                               [ -e /dev/tty.org ] \
+                                       && rm -f /dev/tty \
+                                       && mv /dev/tty.org /dev/tty
+
+                               if [ ${crypt_filter_ret} -eq 0 ]
                                then
                                        good_msg "LUKS device ${LUKS_DEVICE} opened" ${CRYPT_SILENT}
                                        break
@@ -942,10 +994,10 @@ verbose_kmsg() {
 cdupdate() {
        if [ "${CDROOT}" = '1' ]
        then
-               if [ -x /${NEW_ROOT}/mnt/cdrom/cdupdate.sh ]
+               if [ -x /${CDROOT_PATH}/cdupdate.sh ]
                then
                        good_msg "Running cdupdate.sh"
-                       ${NEW_ROOT}/mnt/cdrom/cdupdate.sh
+                       ${CDROOT_PATH}/cdupdate.sh
                        if [ "$?" != '0' ]
                        then
                                bad_msg "Executing cdupdate.sh failed!"
@@ -981,12 +1033,16 @@ setup_md_device() {
        fi
 }
 
+do_rundebugshell() {
+       good_msg 'Type "exit" to continue with normal bootup.'
+       [ -x /bin/sh ] && /bin/sh || /bin/ash
+}
+
 rundebugshell() {
        if [ -n "$DEBUG" ]
        then
                good_msg 'Starting debug shell as requested by "debug" option.'
-               good_msg 'Type "exit" to continue with normal bootup.'
-               [ -x /bin/sh ] && /bin/sh || /bin/ash
+               do_rundebugshell
        fi
 }
 
@@ -1060,10 +1116,9 @@ tuxonice_resume() {
 }
 
 find_loop() {
-       CDROM="${NEW_ROOT}/mnt/cdrom"
        for loop in ${LOOPS}
        do
-               if [ -e "${CDROM}""${loop}" ]
+               if [ -e "${CDROOT_PATH}""${loop}" ]
                then
                        LOOP="${loop}"
                fi
@@ -1093,8 +1148,7 @@ setup_squashfs_aufs() {
        good_msg "Loading aufs"
        modprobe aufs > /dev/null 2>&1
 
-       cd "${NEW_ROOT}"
-       mount -t squashfs -o loop,ro "mnt/cdrom${LOOPEXT}${LOOP}" "${static}"
+       mount -t squashfs -o loop,ro "${CDROOT_PATH}/${LOOPEXT}${LOOP}" "${static}"
        mount -t tmpfs none "${overlay}"
        mount -t aufs -o br:${overlay}:${static} aufs "${NEW_ROOT}"
 
@@ -1103,9 +1157,10 @@ setup_squashfs_aufs() {
        echo "aufs / aufs defaults 0 0" > "${NEW_ROOT}"/etc/fstab
        for i in "${overlay}" "${static}"; do mount --move "${i}" "${NEW_ROOT}${i}"; done
 
-       # have handy /mnt/cdrom as well
-       [ ! -d "${NEW_ROOT}/mnt/cdrom" ] && mkdir -p "${NEW_ROOT}/mnt/cdrom"
-       mount --bind mnt/cdrom "${NEW_ROOT}/mnt/cdrom"
+       # have handy /mnt/cdrom (CDROOT_PATH) as well
+       local new_cdroot="${NEW_ROOT}${CDROOT_PATH}"
+       [ ! -d "${new_cdroot}" ] && mkdir -p "${new_cdroot}"
+       mount --bind "${CDROOT_PATH}" "${new_cdroot}"
        )
 }
 
@@ -1166,7 +1221,54 @@ setup_unionfs() {
                        bad_msg "Can't setup union mount!"
                        USE_UNIONFS_NORMAL=0
                fi
+               [ ! -d "${NEW_ROOT}${CDROOT_PATH}" ] && mkdir -p "${NEW_ROOT}${CDROOT_PATH}"
+               mount --bind "${CDROOT_PATH}" "${NEW_ROOT}${CDROOT_PATH}"
        else
                USE_UNIONFS_NORMAL=0
        fi
 }
+
+get_mounts_list()
+{
+       awk '
+               /^[[:blank:]]*#/ { next }
+               { print $1 }
+               ' ${NEW_ROOT}/etc/initramfs.mounts
+}
+
+get_mount_fstype()
+{
+       [ -e "${NEW_ROOT}"/etc/fstab ] || return 1
+       awk -v fs="$1" '
+               /^[[:blank:]]*#/ { next }
+               $2 == fs { print $3 }
+               ' ${NEW_ROOT}/etc/fstab
+}
+
+get_mount_options()
+{
+       [ -e "${NEW_ROOT}"/etc/fstab ] || return 1
+       awk -v fs="$1" '
+               /^[[:blank:]]*#/ { next }
+               $2 == fs { print $4 }
+               ' ${NEW_ROOT}/etc/fstab
+}
+
+get_mount_device()
+{
+       [ -e "${NEW_ROOT}"/etc/fstab ] || return 1
+       awk -v fs="$1" '
+               /^[[:blank:]]*#/ { next }
+               $2 == fs { print $1 }
+               ' ${NEW_ROOT}/etc/fstab
+}
+
+# If the kernel is handed a mount option is does not recognize, it WILL fail to
+# mount. util-linux handles auto/noauto, but busybox passes it straight to the kernel
+# which then rejects the mount.
+# To make like a little easier, busybox mount does not care about leading,
+# trailing or duplicate commas.
+strip_mount_options()
+{
+       sed -r 's/(,|^)(no)?auto(,|$)/,/g'
+}