Subversion Repositories livecd

Rev

Rev 246 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

#!/bin/ash
#
# Functions library :: for Linux Live scripts 5.x.y
# Author: Tomas M. <http://www.linux-live.org>
#
# modified by Urs Beyerle
# - to allow LiveCD mounted over nfs
# - add scsi_mod, sd_mod for usb-storage module
# - only with boot option "automount", all devices in fstab are rw automounted
# - remove detect of CD and Floppy (done by fstab-sync)
# - add sr_mod (USB CDROM support)
# - add SATA to modprobe_usb_modules -> modprobe_usb_sata_modules
# - add fscache (for SL5) 
# - add ide-cd, sr_mod, cdrom (for SL5 cdrom support)
# - add aufs (unionfs replacement)
# - to allow LiveCD mounted over NFS (for diskless client)
# - add functions get_dhcp_lease() and load_network_modules()
# - add ata_piix to modprobe_usb_sata_modules
# - works with unionfs 2.x
# - better detection of network card in case of diskless client
# - add sg, sata_nv module
# - to allow LiveCD mounted over CIFS (for diskless client)
# - add function find_changes
# - add sata_svw
# - add $DHCPDELAY parameter
# - add $USBDELAY parameter
#

# ===========================================================
# user interface functions
# ===========================================================

# echolog
# $1 = text to show and to write to /var/log/messages
#
echolog()
{
   echo "LIVECD:" "$@" >>/var/log/livedbg
   echo "$@"
}

# debug
# commands executed when debug boot parameter is present
#
debug()
{
   echo
   echo "====="
   echo ": Debugging started. Here is the root shell for you."
   echo ": Type your desired command or hit Ctrl+D to continue booting."
   echo
   ash
}

# header
# $1 = text to show
#
header()
{
   echolog "$1"
}

fatal()
{
   header "Fatal error occured - $1"
   echolog "Something went wrong and we can't continue booting :("
   echolog "You may explore the system by using simple commands like ls, lsmod, mount, etc."
   echolog "You may also try to hit Ctrl+D. Booting will continue. Use at your own risk."
   echolog "To be safe, hit Ctrl+Alt+Delete to reboot."
   echolog
   ash
}

# ===========================================================
# text processing functions
# ===========================================================

# egrep_o is a replacement for "egrep -o". It prints only the last
# matching text
# $1 = regular expression
#
egrep_o()
{
   cat | egrep "$1" | sed -r "s/.*($1).*/\\1/"
}

# look into cmdline and echo $1 back if $1 is set
# $1 = value name, case sensitive, for example livecd_subdir
# $2 = file to use instead /proc/cmdline, optional
#
cmdline_parameter()
{
   CMDLINE=/proc/cmdline
   if [ "$2" != "" ]; then CMDLINE="$2"; fi
   cat "$CMDLINE" | egrep_o "(^|[[:space:]]+)$1(\$|=|[[:space:]]+)" | egrep_o "$1"
}

# look into cmdline and echo value of $1 option
# $1 = value name, case sensitive, for example livecd_subdir
# $2 = file to use instead /proc/cmdline, optional
#
cmdline_value()
{
   CMDLINE=/proc/cmdline
   if [ "$2" != "" ]; then CMDLINE="$2"; fi
   cat "$CMDLINE" | egrep_o "(^|[[:space:]]+)$1=([^[:space:]]+)" | egrep_o "=.*" | cut -b 2- | tail -n 1
}

# ===========================================================
# system functions
# ===========================================================

# modprobe module $1, including all dependencies, suppress all messages
# (own function because modprobe in busybox doesn't work with gzipped modules)
# $1 = module name, eg. ehci-hcd
# $2 = optional argument
#
modprobe_module()
{
  if [ "$1" = "" ]; then return 1; fi
  PRINTK=`cat /proc/sys/kernel/printk`
  echo "0" >/proc/sys/kernel/printk

  KERNEL="`uname -r`"; LSMOD=/tmp/lsmod
  MODULEDEPS="`cat /lib/modules/$KERNEL/modules.dep | egrep \"$1\\.ko(\\.gz)?:\"`"

  for MODULE in `echo $MODULEDEPS | cut -d ":" -f 2-` `echo $MODULEDEPS | cut -d ":" -f 1`; do
     MODULE=${MODULE%%:};   # remove : at the end, a bug 
     TMPMOD="/tmp/`basename $MODULE .gz`";
     # if the module is not loaded already
     if [ "`cat $LSMOD 2>/dev/null | egrep \"^$TMPMOD\\\$\"`" = "" ]; then
        gunzip -c $MODULE 2>/dev/null >$TMPMOD
        if [ $? -ne 0 ]; then cp $MODULE $TMPMOD; fi # can't gunzip? copy
        insmod $TMPMOD $2; err=$?
        ### insmod $TMPMOD $2 >/dev/null 2>/dev/null; err=$?
        if [ "$err" -eq 0 ]; then echo $TMPMOD >>$LSMOD; fi # module log
        rm $TMPMOD
     fi
  done

  echo "$PRINTK" >/proc/sys/kernel/printk
  if [ "$err" -ne 0 ]; then echolog "error inserting module $1 ($err)"; fi
  return $err
}

# Mount device $1 to $2
# $1 = /dev device to mount, eg. /dev/hda1
# $2 = mountpoint, eg. /mnt/hda1
# $3 = mount options, for example "loop", "ro", or "remount,rw"
#
mount_device()
{
  mkdir -p $2
  if [ "$3" != "" ]; then OPTIONS="-o $3"; else OPTIONS=""; fi

  PRINTK=`cat /proc/sys/kernel/printk`
  echo "0" >/proc/sys/kernel/printk

  mount -t auto $1 $2 $OPTIONS >/dev/null 2>/dev/null
  err=$?

  if [ "$err" -ne 0 ]; then rmdir $2 2>/dev/null; fi
  echo "$PRINTK" >/proc/sys/kernel/printk
  return $err
}

# ===========================================================
# live module functions
# ===========================================================

# Create module
# call mksquashfs with apropriate arguments
# $1 = directory which will be compressed to squashfs module
# $2 = output .mo file
# $3 = optional -keep-as-directory argument
#
create_module()
{
   mksquashfs $1 $2 $3 >/dev/null
   if [ $? -ne 0 ]; then return 1; fi
   chmod oga-x $2 # remove execute attrib
}

# Mount .mo module to destination directory
# $1 = path to .mo livecd compressed module
# $2 = destination folder
#
mount_module()
{
   mount -t squashfs -o loop,ro $1 $2
}

# Insert a directory tree $2 to an union specified by $1
# Top-level read-write branch is specified by it's index 0
# $1 = union absolute path (starting with /)
# $2 = path to data directory
#
union_insert_dir()
{
   which unionctl >/dev/null 2>&1
   if [ $? -eq 0 ]; then
       # unionfs 1.x or aufs
       unionctl "$1" --add --after 0 --mode ro "$2"
   else
       # unionfs 2.x
       mount -t unionfs -o remount,add=:${2}=ro none "$1"
   fi
}

# List all modules in all directories (base, modules, optional)
# and filter out unneeded optional modules (not specified by load= kernel parameter)
# $1 = root directory of mounted DATAdir
#
list_modules()
{
   LOAD="`cmdline_value load`"
   ls -A1d $1/*.mo $1/*/*.mo 2>/dev/null | while read LINE; do
      MODNAME="`basename $LINE .mo`"
      if [ "$LOAD" != "*" -a "`echo $LINE | grep optional`" != "" -a "`echo $LOAD | egrep \"(^|,)$MODNAME(\\\$|,)\"`" = "" ]; then continue
        else echo $LINE; fi
   done
}

# Insert one single .mo module to the union
# $1 = union absolute path (starting with /)
# $2 = module.mo full path
# $3 = destination folder, where images will be mounted to
#
union_insert_module()
{
   TARGET="$3/`basename $2`"
   while [ -e $TARGET ]; do TARGET=$TARGET.X; done
   mkdir -p $TARGET
   mount_module $2 $TARGET
   if [ $? -ne 0 ]; then echo "can't read module data"; return 1; fi
   union_insert_dir $1 $TARGET; 
}

# Insert all .mo modules, in $2 directory and subdirectories, to the union
# $1 = union absolute path (starting with /)
# $2 = LiveCD data dir (with directories /base, /modules, etc.)
# $3 = destination folder, where images will be mounted to
#
union_insert_modules()
{
   PRINTK=`cat /proc/sys/kernel/printk`
   echo "0" >/proc/sys/kernel/printk
 
   list_modules $2 | while read MODULE; do
      echo " -> `basename $MODULE`"
      union_insert_module $1 $MODULE $3
   done

   echo "$PRINTK" >/proc/sys/kernel/printk
}

# Copy modules to RAM directory
# $1 = data directory
# $2 = target directory in RAM
#
copy_to_ram()
{
   cp -R $1/* $2
   if [ $? -ne 0 ]; then fatal "can't copy to RAM, not enough memory?"; fi
}

# Copy content of "rootcopy" directory on the CD to $2 (union, usually)
# $1 = source
# $2 = destination
#
copy_rootchanges()
{
   cp -a $1/rootcopy/* $2 2>/dev/null # could be empty
}

# ===========================================================
# discovery functions
# ===========================================================

# List all CD-ROMs
# by using /proc entries
#
list_cdrom_devices()
{
   if [ "`cmdline_parameter nocd`" != "" ]; then return 1; fi
   for CDDEVICE in `cat /proc/sys/dev/cdrom/info | head -n 3 | tail -n 1 | cut -d ":" -f 2`; do
      echo "/dev/$CDDEVICE"
   done
}

# List all partition devices
# take list of all partitions and output unique disks.
# Return empty result when nohd parameter was given.
#
list_partition_devices()
{
   if [ "`cmdline_parameter nohd`" != "" ]; then return 1; fi
   cat /proc/partitions | grep -v loop | sed -r "s/^[0-9[:space:]]+/\/dev\//" | grep /dev/
}

# List all disk devices
#
list_disk_devices()
{
   list_partition_devices | egrep -v "[0-9]"
}

# List all block devices
#
list_block_devices()
{
   list_cdrom_devices
   list_partition_devices
}

# Try to mount all disks, partitions and cdroms and Find where the LiveCD is.
# If LiveCD found in the device, echo dirname of it's directory,
# and leave the device mounted. Mounting is not ro, but without any argument.
# $1 = directory where devices will be mounted
# added: mount $NFSROOT to /$1/nfs if NFSROOT is set. and search there for LiveCD
#
find_live_data_dir()
{
   if [ "$NFSROOT" != "" ]; then 
      DIR="/$1/nfs"
      mkdir -p $DIR
      # try to mount nfs dir
      mount -t nfs $NFSROOT $DIR -o $NFSOPTS 
   fi

   if [ "$CIFSROOT" != "" ]; then
      DIR="/$1/cifs"
      mkdir -p $DIR
      # try to mount cifs dir
      mount -t cifs $CIFSROOT $DIR -o ro,$CIFSOPTS
   fi

   if [ "$DIR" = "" ]; then
       list_block_devices | while read DEVICE; do
           DIR="/$1/`basename $DEVICE`"
           mount_device $DEVICE $DIR
           if [ $? -ne 0 ]; then continue; fi
           # FOUND=`find $DIR -name livecd.sgn -type f | head -n 1`
           FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
           if [ "$FOUND" = "" ]; then umount $DIR 2>/dev/null; rmdir $DIR 2>/dev/null
           else dirname "$FOUND"; return 1; fi
       done
   else
       # FOUND=`find $DIR -name livecd.sgn -type f | head -n 1`
       FOUND=`ls -A1d $DIR/livecd.sgn $DIR/*/livecd.sgn 2>/dev/null | head -n 1`
       if [ "$FOUND" != "" ]; then 
           dirname "$FOUND"
       fi
   fi
}

# ===========================================================
# hardware preparation functions
# ===========================================================

# Create block devices to /dev described by /sys entries
#
create_block_devices()
{
   echolog "creating /dev entries for block devices"
   ls -A1d /sys/block/*/dev /sys/block/*/*/dev 2>/dev/null | grep -v loop | while read BLOCK; do
      DEVICE="/dev/`basename \`dirname $BLOCK\``"
      if [ ! -b $DEVICE ]; then
         MINORMAJOR="`head -n 1 $BLOCK | tr ':' ' '`"
         mknod $DEVICE b $MINORMAJOR
      fi
   done
}

# modprobe kernel modules needed for the LiveCD
#
modprobe_essential_modules()
{
   echolog "starting loop device support"
   modprobe_module loop max_loop=32
   echolog "starting cdrom support"
   modprobe_module ide_cd
   modprobe_module ide-cd
   modprobe_module sr_mod
   modprobe_module cdrom
   echolog "starting cdrom filesystem support"
   modprobe_module isofs
   echolog "starting squashfs support"
   modprobe_module squashfs
   echolog "starting unionfs/aufs support"
   modprobe_module unionfs
   modprobe_module aufs
   echolog "starting vfat support"
   # modprobe_module nls_cp437
   # modprobe_module nls_iso8859-1
   # modprobe_module nls_iso8859-2
   modprobe_module vfat
   echolog "starting ntfs support"
   modprobe_module ntfs
   modprobe_module nls_utf8
   create_block_devices
}


# modprobe kernel modules needed for USB masstorage devices
#
modprobe_usb_sata_modules()
{
   echolog "starting USB and SATA support"
   modprobe_module ehci-hcd
   modprobe_module ohci-hcd
   modprobe_module uhci-hcd
   modprobe_module scsi_mod

   modprobe_module libata
   modprobe_module ata_piix
   modprobe_module ahci
   modprobe_module sata_nv
   modprobe_module sata_svw
   modprobe_module sg

   modprobe_module sd_mod
   modprobe_module sr_mod
   modprobe_module usb-storage
   echolog "waiting $USBDELAY seconds for USB devices"
   sleep $USBDELAY
   create_block_devices
}

# modprobe nfs kernel modules
#
modprobe_nfs_modules()
{
   echolog "starting nfs support"
   modprobe_module lockd
   modprobe_module fscache
   modprobe_module nfs_acl
   modprobe_module nfs
}

# modprobe cifs kernel module
#
modprobe_cifs_modules()
{
  echolog "starting cifs support"
  modprobe_module cifs
}

# enable/disable CD autoejecting when unmounted
# $1 = 1|0 ... enable|disable
#
cd_autoeject()
{
   echo $1 >/proc/sys/dev/cdrom/autoeject
}

# Disable DMA if nodma boot parameter is present
#
setup_dma()
{
   if [ ! "`cmdline_parameter nodma`" = "" ]; then
      for DEVICE in `list_cdrom_devices` `list_disk_devices`; do
         echolog "setting DMA support off for $DEVICE"
         hdparm -d 0 $DEVICE
      done
   fi
}

# create correct fstab file in $1/etc/fstab and create apropriate
# mount directories in $1
# $1 = root directory (union)
#
activate_fstab()
{
   mkdir -p $1/etc
   FSTAB="$1/etc/fstab"
   echo "tmpfs            /                tmpfs       defaults         0   0" >$FSTAB
   echo "devpts           /dev/pts         devpts      gid=5,mode=620   0   0" >>$FSTAB
   echo "proc             /proc            proc        defaults         0   0" >>$FSTAB

   # all the rest will be done by runlast or by fstab-sync
   # search for SWAP done later in /etc/rc.sysinit
}


# find partition and/or folder that stores the changes
# return false, if changes not set or changes folder/partition not found
# Urs Beyerle
#
find_changes()
{
    echolog "looking for changes in $CHANGESVAL"

    CHANGESDEV="`echo "$CHANGESVAL" | egrep -o "^/dev/[^/]+"`"
    CHANGESFOLDER="`echo "$CHANGESVAL" | sed -r 's:^/dev/[^/]+(.*):\1:'`"
    # (re-)define CHANGES if CHANGESFOLDER is set
    if [ "$CHANGESFOLDER" != "" ]; then CHANGES=$MEMORY/$CHANGESFOLDER; fi

    # we may need usb and sata support
    modprobe_usb_sata_modules

    # device defined?
    if [ "$CHANGESDEV" != "" ]; then
        mount_device $CHANGESDEV $MEMORY
        if [ $? -eq 0 ]; then 
            echolog "mounted $CHANGESDEV to $MEMORY"
        else
            echolog "WARNING: device $CHANGESDEV could not be mounted! Continue booting in 10 sec. ..."
            CHANGESVAL=""
            sleep 10
        fi
        return
    fi
    
    # if no device is specified, search all devices for folder CHANGESFOLDER
    list_block_devices | while read DEVICE; do
        mount_device $DEVICE $MEMORY
        if [ $? -ne 0 ]; then continue; fi
        FOUND_FOLDER=`ls -1d $MEMORY/$CHANGESFOLDER 2>/dev/null`
        if [ "$FOUND_FOLDER" = "" ]; then 
            umount $MEMORY 2>/dev/null
        else
            echolog "found folder $CHANGESFOLDER on $DEVICE"
            break
        fi
    done

    FOUND_FOLDER=`ls -1d $MEMORY/$CHANGESFOLDER 2>/dev/null`

    if [ "$FOUND_FOLDER" = "" ]; then 
        echolog "WARNING: changes=$CHANGESVAL not found! Continue booting in 10 sec. ..."
        CHANGESVAL=""
        sleep 10
    fi

}


# ===========================================================
# functions used for LiveCD on NFS 
# ===========================================================

# load network modules, if NFSROOT is set
# Urs Beyerle
#

# network devices found ?
#
found_nic()
{
    found="1"
    for iface in eth0 eth1 eth2 eth3 eth4 eth5; do
        # nic already found ?
        echo $FOUND_IFACE | grep -q $iface 
        if [ $? -ne 0 ]; then
            ifconfig $iface up > /dev/null 2>&1
            if [ $? -eq 0 ]; then 
                FOUND_IFACE="$FOUND_IFACE $iface"
                found="0"
            fi
        fi
    done
    return $found
}

# 1. load network driver defined by kernel parameter nic
#
load_network_module_nic()
{
    NIC="`cmdline_value nic`"
    if [ -n "$NIC" ]; then
        echolog "load module $NIC"
        modprobe_module $NIC
    fi
}


# 2. auto probe for network card drivers
#
load_network_module_auto()
{
    echolog "auto-detection of network driver ..."
    FOUND_NICS=""
    KERNEL="`uname -r`"
    NETDRIVERS="`find /lib/modules/$KERNEL/kernel/drivers/net | grep -v mii | grep -v 8021q | grep .ko | cut -d"/" -f8 | cut -d"." -f1 | uniq`"
    for driver in $NETDRIVERS; do
        modprobe_module $driver > /dev/null 2>&1
        found_nic
        if [ $? -eq 0 ]; then
            echolog "found network card $driver"
            echolog "load module $driver"
            FOUND_NICS="$FOUND_NICS $driver"
        else
            rmmod $driver > /dev/null 2>&1
        fi
    done

    # clean up: remove unused modules until driver "mii"
    LOADED_DRIVERS="`lsmod | grep -v Module | cut -d" " -f1`"
    for driver in $LOADED_DRIVERS; do
        [ "$driver" = "mii" ]  && break
        [ "$driver" = "ntfs" ] && break
        [ "$driver" = "vfat" ] && break
        do_not_remove=""
        for nic in $FOUND_NICS; do
            echo $driver | grep -q $nic 
            [ $? -eq 0 ] && do_not_remove="$driver"
        done
        [ ! $do_not_remove ] && rmmod $driver > /dev/null 2>&1
    done
}

# 3. detecting network card from pcitable list
#
load_network_module_pcitable()
{
    PCITABLE=/bin/pcitable
    NICS=`lspci -n | awk '/Class 0200/ {print $4}' | tr ':' ' ' \
        | while read x y ; do grep "0x$x.*0x$y" $PCITABLE \
        | awk '$3 !~ /"unknown"/ {print $3}' | tr -d '"' ; done`
    
    if [ -n "$NICS" ]; then
        echolog "found network card(s): $NICS"
        for driver in $NICS; do
            echolog "load module $driver"
            modprobe_module $driver
        done
    fi
}

# 4. ask the user for a network driver
#
load_network_module_ask()
{
    echo "ERROR: No network card detected!"
    echo "Type in your network card driver (e.g. tg3, e1000). "
    echo "Or ask your system administrator for help."
    echo -n "Network card driver: "
    read driver
    echo "load module $driver"
    modprobe_module $driver
}

# Try to find a network driver
#
load_network_modules()
{
    echolog "load network modules"

    # mii and 8021q maybe need by NIC driver
    modprobe_module 8021q
    modprobe_module mii

    FOUND_IFACE=""
    load_network_module_nic
    found_nic          || load_network_module_auto
    [ "$FOUND_IFACE" ] || load_network_module_pcitable
    [ "$FOUND_IFACE" ] || load_network_module_ask

    # remove mii and 8021q, if not needed
    rmmod 8021q > /dev/null 2>&1
    rmmod mii   > /dev/null 2>&1
}


# get DHCP lease
# Urs Beyerle
#
get_dhcp_lease()
{
    # create /dev/urandom (needed by udhcpc)
    mknod /dev/urandom c 1 9
    
    # wait for interface coming up
    if [ $DHCPDELAY -gt 0 ]; then 
        echolog "delaying DHCP for $DHCPDELAY seconds (dhcpdelay=$DHCPDELAY) ..."
        sleep $DHCPDELAY
        echolog "if getting dhcp lease freezes, try define a higher dhcpdelay value as boot parameter."
    fi
    for iface in eth0 eth1 eth2 eth3 eth4 eth5; do
        # interface up ?
        ifconfig $iface > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            # get dhcp lease
            echolog "try to get DHCP lease on $iface"
            udhcpc --now --quit --interface=$iface --script=/bin/udhcpc.script
            [ $? -eq 0 ] && return
            echolog "ERROR: couldn't get DHCP lease on $iface."
        fi
    done
}