LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1291|回复: 1

大家一起来分析这个initrd.gz里面的启动iso镜像的脚本

[复制链接]
发表于 2010-3-23 16:28:27 | 显示全部楼层 |阅读模式
大家一起来分析这个initrd.gz里面的启动iso镜像的脚本suddy,解压开initrd.gz后,是在scripts目录下



  1. #!/bin/sh

  2. # set -e

  3. export PATH=/usr/bin:/usr/sbin:/bin:/sbin

  4. mountpoint=/cdrom

  5. root_persistence="casper-rw"
  6. home_persistence="home-rw"
  7. root_snapshot_label="casper-sn"
  8. home_snapshot_label="home-sn"

  9. USERNAME=casper
  10. USERFULLNAME="Live session user"
  11. HOST=live
  12. BUILD_SYSTEM=Custom

  13. mkdir -p $mountpoint

  14. [ -f /etc/casper.conf ] && . /etc/casper.conf
  15. export USERNAME USERFULLNAME HOST BUILD_SYSTEM

  16. . /scripts/casper-helpers

  17. if [ ! -f /casper.vars ]; then
  18.     touch /casper.vars
  19. fi

  20. parse_cmdline() {
  21.     for x in $(cat /proc/cmdline); do
  22.         case $x in
  23.        casperroot=*)
  24.       export CASPERROOT="${x#casperroot=}";;
  25.             persistent)
  26.                 export PERSISTENT="Yes" ;;
  27.             nopersistent)
  28.                 export PERSISTENT="" ;;
  29.             union=*)
  30.                 export UNIONFS="${x#union=}";;
  31.             ip*)
  32.                 STATICIP=${x#ip=}
  33.                 if [ "${STATICIP}" = "" ]; then
  34.                     STATICIP="frommedia"
  35.                 fi
  36.                 export STATICIP ;;
  37.             ignore_uuid)
  38.                 IGNORE_UUID="Yes" ;;
  39.         esac
  40.     done
  41.     if [ "${UNIONFS}" = "" ]; then
  42.         export UNIONFS="aufs"
  43.     fi
  44. }

  45. is_casper_path() {
  46.     if [ "${CASPERROOT}" = "" ]; then
  47.         path=$1
  48.     else
  49.    path="$1/${CASPERROOT}"
  50.     fi
  51.     # modify the path to $1 if it's a iso file
  52.     if [ "$ISISO" != "" ]; then
  53.         path=$1
  54.     fi
  55.     if [ -d "$path/casper" ]; then
  56.         if [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
  57.             [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
  58.             [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
  59.             return 0
  60.         fi
  61.     fi
  62.     return 1
  63. }

  64. matches_uuid() {
  65.     if [ "$IGNORE_UUID" ] || [ ! -e /conf/uuid.conf ]; then
  66.         return 0
  67.     fi
  68.     path="$1"
  69.     uuid="$(cat /conf/uuid.conf)"
  70.     for try_uuid_file in "$path/.disk/casper-uuid"*; do
  71.         [ -e "$try_uuid_file" ] || continue
  72.         try_uuid="$(cat "$try_uuid_file")"
  73.         if [ "$uuid" = "$try_uuid" ]; then
  74.             return 0
  75.         fi
  76.     done
  77.     return 1
  78. }

  79. get_backing_device() {
  80.     case "$1" in
  81.    # add the ext of *.fs to doing with storefilesystem sth like Backtrack.fs
  82.         *.squashfs|*.ext2|*.fs)
  83.             echo $(setup_loop "$1" "loop" "/sys/block/loop*")
  84.             ;;
  85.         *.dir)
  86.             echo "directory"
  87.             ;;
  88.         *)
  89.             panic "Unrecognized casper filesystem: $1"
  90.             ;;
  91.     esac
  92. }

  93. match_files_in_dir() {
  94.     # Does any files match pattern $1 ?

  95.     local pattern="$1"
  96.     if [ "$(echo $pattern)" != "$pattern" ]; then
  97.         return 0
  98.     fi
  99.     return 1
  100. }

  101. mount_images_in_directory() {
  102.     if [ "${CASPERROOT}" = "" ]; then
  103.         directory="$1"
  104.     else
  105.    directory="$1/${CASPERROOT}"
  106.     fi
  107.     if [ "$1" = "/iso9660" ]; then
  108.         directory="$1"
  109.     fi
  110.     rootmnt="$2"
  111.     if match_files_in_dir "$directory/casper/*.squashfs" ||
  112.         match_files_in_dir "$directory/casper/*.ext2" ||
  113.         match_files_in_dir "$directory/casper/*.dir"; then
  114.         setup_unionfs "$directory/casper" "$rootmnt"
  115.     else
  116.         :
  117.     fi
  118. }

  119. is_nice_device() {
  120.     sysfs_path="${1#/sys}"
  121.     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-(ide|scsi|usb))"; then
  122.         return 0
  123.     fi
  124.     return 1
  125. }

  126. copy_live_to() {
  127.     copyfrom="${1}"
  128.     copytodev="${2}"
  129.     copyto="${copyfrom}_swap"

  130.     size=$(fs_size "" ${copyfrom} "used")

  131.     if [ "${copytodev}" = "ram" ]; then
  132.         # copying to ram:
  133.         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
  134.         mount_options="-o size=${size}k"
  135.         free_string="memory"
  136.         fstype="tmpfs"
  137.         dev="/dev/shm"
  138.     else
  139.         # it should be a writable block device
  140.         if [ -b "${copytodev}" ]; then
  141.             dev="${copytodev}"
  142.             free_string="space"
  143.             fstype=$(get_fstype "${dev}")
  144.             freespace=$(fs_size "${dev}")
  145.         else
  146.             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
  147.             return 1
  148.         fi
  149.     fi
  150.     if [ "${freespace}" -lt "${size}" ] ; then
  151.         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
  152.         return 1
  153.     fi

  154.     # begin copying..
  155.     mkdir "${copyto}"
  156.     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
  157.     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
  158.     cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
  159.     umount ${copyfrom}
  160.     mount -r -o move ${copyto} ${copyfrom}
  161.     rmdir ${copyto}
  162.     return 0
  163. }

  164. do_netmount() {
  165.     rc=1

  166.     modprobe "${MP_QUIET}" af_packet # For DHCP

  167.     /sbin/udevadm trigger
  168.     /sbin/udevadm settle

  169.     ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config

  170.     if [ "${NFSROOT}" = "auto" ]; then
  171.         NFSROOT=${ROOTSERVER}:${ROOTPATH}
  172.     fi

  173.     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"

  174.     if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
  175.         rc=0
  176.     elif do_nfsmount ; then
  177.         NETBOOT="nfs"
  178.         export NETBOOT
  179.         rc=0
  180.     fi

  181.     [ "$quiet" != "y" ] && log_end_msg
  182.     return ${rc}
  183. }

  184. do_nfsmount() {
  185.     rc=1
  186.     modprobe "${MP_QUIET}" nfs
  187.     if [ -z "${NFSOPTS}" ]; then
  188.         NFSOPTS=""
  189.     fi

  190.     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
  191.     # FIXME: This while loop is an ugly HACK round an nfs bug
  192.     i=0
  193.     while [ "$i" -lt 60 ]; do
  194.         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
  195.         sleep 1
  196.         i="$(($i + 1))"
  197.     done
  198.     return ${rc}
  199. }

  200. do_cifsmount() {
  201.     rc=1
  202.     if [ -x "/sbin/mount.cifs" ]; then
  203.         if [ -z "${NFSOPTS}" ]; then
  204.             CIFSOPTS="-ouser=root,password="
  205.         else
  206.             CIFSOPTS="${NFSOPTS}"
  207.         fi

  208.         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
  209.         modprobe "${MP_QUIET}" cifs

  210.         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
  211.             rc=0
  212.         fi
  213.     fi
  214.     return ${rc}
  215. }

  216. do_snap_copy ()
  217. {
  218.     fromdev="${1}"
  219.     todir="${2}"
  220.     snap_type="${3}"

  221.     size=$(fs_size "${fromdev}" "" "used")

  222.     if [ -b "${fromdev}" ]; then
  223.         # look for free mem
  224.         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
  225.             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
  226.             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
  227.         else
  228.             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
  229.         fi

  230.         tomount="/mnt/tmpsnap"
  231.         if [ ! -d "${tomount}" ] ; then
  232.             mkdir -p "${tomount}"
  233.         fi

  234.         fstype=$(get_fstype "${fromdev}")
  235.         if [ -n "${fstype}" ]; then
  236.             # Copying stuff...
  237.             mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
  238.             cp -a "${tomount}"/* ${todir}
  239.             umount "${tomount}"
  240.         else
  241.             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
  242.         fi

  243.         rmdir "${tomount}"
  244.         if echo ${fromdev} | grep -qs loop; then
  245.            losetup -d "${fromdev}"
  246.         fi
  247.         return 0
  248.     else
  249.         return 1
  250.         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
  251.     fi
  252. }

  253. try_snap ()
  254. {
  255.     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
  256.     #   and remember the device and filename for resync on exit in casper.init

  257.     snap_label="${1}"
  258.     snap_mount="${2}"
  259.     snap_type="${3}"

  260.     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
  261.     if [ ! -z "${snapdata}" ]; then
  262.         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
  263.         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
  264.         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
  265.         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
  266.             # squashfs or ext2 snapshot
  267.             dev=$(get_backing_device "${snapback}/${snapfile}")
  268.             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
  269.                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
  270.                  return 1
  271.             fi
  272.         else
  273.             # cpio.gz snapshot
  274.             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
  275.                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
  276.                 return 1
  277.             fi
  278.         fi
  279.         umount "${snapback}"
  280.     else
  281.         dev=$(find_cow_device "${snap_label}")
  282.         if [ -b ${dev} ]; then
  283.             if echo "${dev}" | grep -qs loop; then
  284.                 # strange things happens, user confused?
  285.                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
  286.                 snapfile=$(basename ${snaploop})
  287.                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
  288.             else
  289.                 snapdev="${dev}"
  290.             fi
  291.             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
  292.                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
  293.                 return 1
  294.             else
  295.                 if [ -n "${snapfile}" ]; then
  296.                      # it was a loop device, user confused
  297.                      umount ${snapdev}
  298.                 fi
  299.             fi
  300.         else
  301.             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
  302.             return 1
  303.         fi
  304.     fi
  305.     echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
  306.     return 0
  307. }

  308. setup_unionfs() {
  309.     image_directory="$1"
  310.     rootmnt="$2"

  311.     modprobe "${MP_QUIET}" -b ${UNIONFS}

  312.     # run-init can't deal with images in a subdir, but we're going to
  313.     # move all of these away before it runs anyway.  No, we're not,
  314.     # put them in / since move-mounting them into / breaks mono and
  315.     # some other apps.

  316.     croot="/"

  317.     # Let's just mount the read-only file systems first
  318.     rofsstring=""
  319.     rofslist=""
  320.     if [ "${NETBOOT}" = "nfs" ] ; then
  321.         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
  322.     elif [ "${UNIONFS}" = "aufs" ]; then
  323.         roopt="rr"
  324.     else
  325.         roopt="ro"
  326.     fi

  327.     mkdir -p "${croot}"
  328.     for image_type in "ext2" "squashfs" "dir" ; do
  329.         for image in "${image_directory}"/*."${image_type}"; do
  330.             imagename=$(basename "${image}")
  331.             if [ -d "${image}" ]; then
  332.                 # it is a plain directory: do nothing
  333.                 rofsstring="${image}=${roopt}:${rofsstring}"
  334.                 rofslist="${image} ${rofslist}"
  335.             elif [ -f "${image}" ]; then
  336.                 backdev=$(get_backing_device "$image")
  337.                 fstype=$(get_fstype "${backdev}")
  338.                 if [ "${fstype}" = "unknown" ]; then
  339.                     panic "Unknown file system type on ${backdev} (${image})"
  340.                 fi
  341.                 mkdir -p "${croot}/${imagename}"
  342.                 mount -t "${fstype}" -o ro,noatime "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
  343.             fi
  344.         done
  345.     done
  346.     rofsstring=${rofsstring%:}

  347.     mkdir -p /cow
  348.     cowdevice="tmpfs"
  349.     cow_fstype="tmpfs"
  350.     cow_mountopt="rw,noatime,mode=755"

  351.     # Looking for "${root_persistence}" device or file
  352.     if [ -n "${PERSISTENT}" ]; then
  353.         cowprobe=$(find_cow_device "${root_persistence}")
  354.         if [ -b "${cowprobe}" ]; then
  355.             cowdevice=${cowprobe}
  356.             cow_fstype=$(get_fstype "${cowprobe}")
  357.        cow_mountopt="rw,noatime"
  358.         else
  359.             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
  360.         fi
  361.     fi

  362.     mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || panic "Can not mount $cowdevice on /cow"

  363.     # mkdir a filesystem to mount the *.fs
  364.     if [ "${CASPERROOT}" = "" ]; then
  365.         storefs="$mountpoint"
  366.     else
  367.    storefs="$mountpoint/${CASPERROOT}"
  368.     fi
  369.     # if a *.fs in the patch of the boot directory, use unionfs to mount to /
  370.     if [ "$(echo $storefs/*.fs)" != "$storefs/*.fs" ] ; then
  371.    rwfs="$(echo $storefs/*.fs)"
  372.    #mkdir -p /storefs
  373.    #storefs_fstype=$(get_fstype $rwfs)
  374.    #storefs_mountopt="rw,noatime"
  375.    #mount -t  $storefs_fstype -o $storefs_mountopt $rwfs /storefs

  376.    storefs_backdev=$(get_backing_device "$rwfs")
  377.         storefs_fstype=$(get_fstype "${storefs_backdev}")
  378.         if [ "${storefs_fstype}" = "unknown" ]; then
  379.       panic "Unknown file system type on ${storefs_backdev} ($rwfs)"
  380.    fi
  381.         mkdir -p "/storefs"
  382.         mount -t "${storefs_fstype}" -o rw,noatime "${storefs_backdev}" "/storefs" || panic "Can not mount $storefs_backdev ($rwfs) on /storefs"
  383.            
  384.    #mkdir -p "/root1"
  385.    # mount with storefs to store the change
  386.    mount -t ${UNIONFS} -o noatime,dirs=//storefs=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
  387.    #mount -t ${UNIONFS} -o noatime,dirs=//storefs=rw:/cow=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
  388.    #mount -t ${UNIONFS} -o noatime,dirs=//storefs=rw:/cow=rw:$rofsstring ${UNIONFS} "/root1" || panic "${UNIONFS} mount failed"
  389.     else
  390.    mount -t ${UNIONFS} -o noatime,dirs=/cow=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
  391.     fi

  392.     # if a *.swap in the patch of the boot directory, use it like a swap file
  393.     if [ "$(echo $storefs/*.swap)" != "$storefs/*.swap" ] ; then
  394.    swapfs="$(echo $storefs/*.swap)"
  395.    swapon $swapfs
  396.    echo "$swapfs swap  swap  defaults  0  0">> "$rootmnt"/etc/swap_fstab
  397.     fi

  398.     # Adding other custom mounts
  399.     if [ -n "${PERSISTENT}" ]; then
  400.         # directly mount /home
  401.         # FIXME: add a custom mounts configurable system
  402.         homecow=$(find_cow_device "${home_persistence}" )
  403.         if [ -b "${homecow}" ]; then
  404.             mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
  405.             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
  406.         else
  407.             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
  408.         fi
  409.         # Look for other snapshots to copy in
  410.         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
  411.         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
  412.     fi

  413.     if [ -n "${SHOWMOUNTS}" ]; then
  414.         for d in ${rofslist}; do
  415.             mkdir -p "${rootmnt}/casper/${d##*/}"
  416.             case d in
  417.                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/casper/${d##*/}"
  418.                     ;;
  419.                 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
  420.                     ;;
  421.             esac
  422.         done
  423.         # shows cow fs on /cow for use by casper-snapshot
  424.         mkdir -p "${rootmnt}/cow"
  425.         mount -o bind /cow "${rootmnt}/cow"
  426.     fi

  427.     # move the stofs ; no head in busybox-initramfs
  428.     if [ "${storefs_fstype}" != "" ]; then
  429.    mkdir -p "${rootmnt}/rwfs"
  430.    mount -o move "/storefs" "${rootmnt}/rwfs"
  431.     fi

  432.     for d in $(mount -t iso9660 | cut -d\  -f 3); do
  433.        mkdir -p "$rootmnt/iso9660"
  434.        mount -o move "${d}" "$rootmnt/iso9660"
  435.     done

  436.     # move the first mount; no head in busybox-initramfs
  437.     for d in $(mount -t squashfs | cut -d\  -f 3); do
  438.         mkdir -p "${rootmnt}/rofs"
  439.         mount -o move "${d}" "${rootmnt}/rofs"
  440.         break
  441.     done
  442. }

  443. find_iso()
  444. {
  445.    local isoroot="/iso9660"
  446.    local tmproot=$1
  447.    if [ "${CASPERROOT}" = "" ]; then
  448.         isos=`ls ${tmproot}/*.iso 2>/dev/null` || return 1
  449.    else
  450.    isos=`ls ${tmproot}/${CASPERROOT}/*.iso 2>/dev/null` || return 1
  451.    fi
  452.    modprobe loop
  453.    mkdir -p "$isoroot"
  454.    for iso in ${isos}
  455.    do
  456.        mount -t iso9660 -o loop $iso $isoroot 2>/dev/null || continue
  457.        #if casper_path "$isoroot"; then
  458.        #send a sign to the function that is a iso file
  459.        ISISO="YES";
  460.        if is_casper_path "$isoroot" ; then
  461.            #echo "$isoroot"/casper/filesystem.squashfs
  462.            #in_iso_squashfs="$isoroot"/casper/filesystem.squashfs
  463.       in_iso_squashfs="$isoroot"
  464.            return 0        
  465.    else
  466.       ISISO=""
  467.            umount "$isoroot"
  468.        fi
  469.    done
  470.    unset isos
  471.    unset iso
  472.    return 1
  473. }

  474. check_dev ()
  475. {
  476.     sysdev="${1}"
  477.     devname="${2}"
  478.     skip_uuid_check="${3}"
  479.     if [ -z "${devname}" ]; then
  480.         devname=$(sys2dev "${sysdev}")
  481.     fi

  482.     if [ -d "${devname}" ]; then
  483.         mount -o bind "${devname}" $mountpoint || continue
  484.         if is_casper_path $mountpoint; then
  485.             echo $mountpoint
  486.             return 0
  487.         else
  488.             umount $mountpoint
  489.         fi
  490.     fi

  491.     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
  492.         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}")
  493.         devname="${loopdevname}"
  494.     fi

  495.     fstype=$(get_fstype "${devname}")
  496.     if is_supported_fs ${fstype}; then
  497.    if [ ${fstype}="ntfs" ]; then
  498.       mount -t ntfs-3g -o rw,noatime "${devname}" $mountpoint || continue
  499.    else
  500.       mount -t ${fstype} -o rw,noatime "${devname}" $mountpoint || continue
  501.    fi
  502.    if find_iso $mountpoint ; then
  503.       echo $in_iso_squashfs
  504.       return 0
  505.    fi
  506.         if is_casper_path $mountpoint && \
  507.            ([ "$skip_uuid_check" ] || matches_uuid $mountpoint); then
  508.             echo $mountpoint
  509.             return 0
  510.         else
  511.             umount $mountpoint
  512.         fi
  513.     fi

  514.     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
  515.         losetup -d "${loopdevname}"
  516.     fi
  517.     return 1
  518. }

  519. find_livefs() {
  520.     timeout="${1}"
  521.     # first look at the one specified in the command line
  522.     if [ ! -z "${LIVEMEDIA}" ]; then
  523.         if check_dev "null" "${LIVEMEDIA}" "skip_uuid_check"; then
  524.             return 0
  525.         fi
  526.     fi
  527.     # don't start autodetection before timeout has expired
  528.     if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then
  529.         if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then
  530.             return 1
  531.         fi
  532.     fi
  533.     # or do the scan of block devices
  534.     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -vE "/(loop|ram|fd)"); do
  535.         devname=$(sys2dev "${sysblock}")
  536.         fstype=$(get_fstype "${devname}")
  537.         if /lib/udev/cdrom_id ${devname} > /dev/null; then
  538.             if check_dev "null" "${devname}" ; then
  539.                 return 0
  540.             fi
  541.         elif is_nice_device "${sysblock}" ; then
  542.             for dev in $(subdevices "${sysblock}"); do
  543.                 if check_dev "${dev}" ; then
  544.                     return 0
  545.                 fi
  546.             done
  547.         elif [ "${fstype}" = "squashfs" -o \
  548.                 "${fstype}" = "ext3" -o \
  549.                 "${fstype}" = "ext2" ]; then
  550.             # This is an ugly hack situation, the block device has
  551.             # an image directly on it.  It's hopefully
  552.             # casper, so take it and run with it.
  553.             ln -s "${devname}" "${devname}.${fstype}"
  554.             echo "${devname}.${fstype}"
  555.             return 0
  556.         fi
  557.     done
  558.     return 1
  559. }

  560. set_usplash_timeout() {
  561.     if [ -x /sbin/usplash_write ]; then
  562.         /sbin/usplash_write "TIMEOUT 120"
  563.     fi
  564. }

  565. mountroot() {
  566.     exec 6>&1
  567.     exec 7>&2
  568.     exec > casper.log
  569.     exec 2>&1

  570.     parse_cmdline

  571.     set_usplash_timeout
  572.     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
  573.     run_scripts /scripts/casper-premount
  574.     [ "$quiet" != "y" ] && log_end_msg

  575.     # Needed here too because some things (*cough* udev *cough*)
  576.     # changes the timeout

  577.     set_usplash_timeout

  578.     if [ ! -z "${NETBOOT}" ]; then
  579.         if do_netmount ; then
  580.             livefs_root="${mountpoint}"
  581.         else
  582.             panic "Unable to find a live file system on the network"
  583.         fi
  584.     else
  585.         # Scan local devices for the image
  586.         i=0
  587.         while [ "$i" -lt 60 ]; do
  588.             livefs_root=$(find_livefs $i)
  589.             if [ "${livefs_root}" ]; then
  590.                 break
  591.             fi
  592.             sleep 1
  593.             i="$(($i + 1))"
  594.         done
  595.     fi

  596.     if [ -z "${livefs_root}" ]; then
  597.         panic "Unable to find a medium containing a live file system"
  598.     fi

  599.     if [ "${TORAM}" ]; then
  600.         live_dest="ram"
  601.     elif [ "${TODISK}" ]; then
  602.         live_dest="${TODISK}"
  603.     fi
  604.     if [ "${live_dest}" ]; then
  605.         log_begin_msg "Copying live_media to ${live_dest}"
  606.         copy_live_to "${livefs_root}" "${live_dest}"
  607.         log_end_msg
  608.     fi
  609.     mount_images_in_directory "${livefs_root}" "${rootmnt}"

  610.     log_end_msg

  611.     maybe_break casper-bottom
  612.     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"

  613.     run_scripts /scripts/casper-bottom
  614.     [ "$quiet" != "y" ] && log_end_msg

  615.     exec 1>&6 6>&-
  616.     exec 2>&7 7>&-
  617.     cp casper.log "${rootmnt}/var/log/"
  618. }
复制代码
发表于 2010-3-24 09:57:41 | 显示全部楼层
查找root   启动root。。。

关键要看楼主实现什么功能。。。

这里面些函数还是可以利用起来的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表