Advertisement
Guest User

Untitled

a guest
May 30th, 2017
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 28.74 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # set -e
  4.  
  5. export PATH=/usr/bin:/usr/sbin:/bin:/sbin
  6.  
  7. mountpoint=/cdrom
  8. LIVE_MEDIA_PATH=casper
  9. [ -f /conf/uuid.conf ] && UUID="$(cat /conf/uuid.conf)"
  10.  
  11. root_persistence="casper-rw"
  12. home_persistence="home-rw"
  13. root_snapshot_label="casper-sn"
  14. home_snapshot_label="home-sn"
  15.  
  16. USERNAME=casper
  17. USERFULLNAME="Live session user"
  18. HOST=live
  19. BUILD_SYSTEM=Custom
  20.  
  21. mkdir -p $mountpoint
  22. tried=/tmp/tried
  23.  
  24. [ -f /etc/casper.conf ] && . /etc/casper.conf
  25. export USERNAME USERFULLNAME HOST BUILD_SYSTEM
  26.  
  27. . /scripts/casper-helpers
  28.  
  29. if [ ! -f /casper.vars ]; then
  30.     touch /casper.vars
  31. fi
  32.  
  33. parse_cmdline() {
  34.     for x in $(cat /proc/cmdline); do
  35.         case $x in
  36.             netboot=*)
  37.                 export NETBOOT="${x#netboot=}";;
  38.             fetch=*)
  39.                 export URL="${x#fetch=}";;
  40.             showmounts|show-cow)
  41.                 export SHOWMOUNTS='Yes' ;;
  42.             persistent)
  43.                 export PERSISTENT="Yes" ;;
  44.             nopersistent)
  45.                 export PERSISTENT="" ;;
  46.             persistent-path=*)
  47.                 export PERSISTENT_PATH="${x#persistent-path=}" ;;
  48.             union=*)
  49.                 export UNIONFS="${x#union=}";;
  50.             ip=*)
  51.                 STATICIP=${x#ip=}
  52.                 if [ "${STATICIP}" = "" ]; then
  53.                     STATICIP="frommedia"
  54.                 fi
  55.                 export STATICIP ;;
  56.             uuid=*)
  57.                 UUID=${x#uuid=} ;;
  58.             ignore_uuid)
  59.                 UUID="" ;;
  60.             live-media=*)
  61.                 LIVEMEDIA="${x#live-media=}"
  62.                 export LIVEMEDIA
  63.                 echo "export LIVEMEDIA=\"$LIVEMEDIA\"" >> /etc/casper.conf ;;
  64.             live-media-path=*)
  65.                 LIVE_MEDIA_PATH="${x#live-media-path=}"
  66.                 export LIVE_MEDIA_PATH
  67.                 echo "export LIVE_MEDIA_PATH=\"$LIVE_MEDIA_PATH\"" >> /etc/casper.conf ;;
  68.             toram)
  69.                 export TORAM="Yes" ;;
  70.             todisk=*)
  71.                 export TODISK="${x#todisk=}" ;;
  72.             hostname=*)
  73.                 export CMD_HOST="${x#hostname=}" ;;
  74.             userfullname=*)
  75.                 export CMD_USERFULLNAME="${x#userfullname=}" ;;
  76.             username=*)
  77.                 export CMD_USERNAME="${x#username=}" ;;
  78.         esac
  79.     done
  80.     if [ "${UNIONFS}" = "" ]; then
  81.         export UNIONFS="DEFAULT"
  82.     fi
  83. }
  84.  
  85. is_casper_path() {
  86.     path=$1
  87.     if [ -d "$path/$LIVE_MEDIA_PATH" ]; then
  88.         if [ "$(echo $path/$LIVE_MEDIA_PATH/*.squashfs)" != "$path/$LIVE_MEDIA_PATH/*.squashfs" ] ||
  89.             [ "$(echo $path/$LIVE_MEDIA_PATH/*.ext2)" != "$path/$LIVE_MEDIA_PATH/*.ext2" ] ||
  90.             [ "$(echo $path/$LIVE_MEDIA_PATH/*.dir)" != "$path/$LIVE_MEDIA_PATH/*.dir" ]; then
  91.             return 0
  92.         fi
  93.     fi
  94.     return 1
  95. }
  96.  
  97. matches_uuid() {
  98.     if [ -z "$UUID" ]; then
  99.         return 0
  100.     fi
  101.     path="$1"
  102.     for try_uuid_file in "$path/.disk/casper-uuid"*; do
  103.         [ -e "$try_uuid_file" ] || continue
  104.         try_uuid="$(cat "$try_uuid_file")"
  105.         if [ "$UUID" = "$try_uuid" ]; then
  106.             return 0
  107.         fi
  108.     done
  109.     return 1
  110. }
  111.  
  112. get_backing_device() {
  113.     case "$1" in
  114.         *.squashfs|*.ext2)
  115.             echo $(setup_loop "$1" "loop" "/sys/block/loop*")
  116.             ;;
  117.         *.dir)
  118.             echo "directory"
  119.             ;;
  120.         *)
  121.             panic "Unrecognized casper filesystem: $1"
  122.             ;;
  123.     esac
  124. }
  125.  
  126. match_files_in_dir() {
  127.     # Does any files match pattern $1 ?
  128.  
  129.     local pattern="$1"
  130.     if [ "$(echo $pattern)" != "$pattern" ]; then
  131.         return 0
  132.     fi
  133.     return 1
  134. }
  135.  
  136. mount_images_in_directory() {
  137.     directory="$1"
  138.     rootmnt="$2"
  139.     if match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.squashfs" ||
  140.         match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.ext2" ||
  141.         match_files_in_dir "$directory/$LIVE_MEDIA_PATH/*.dir"; then
  142.         setup_unionfs "$directory/$LIVE_MEDIA_PATH" "$rootmnt"
  143.     else
  144.         :
  145.     fi
  146. }
  147.  
  148. is_nice_device() {
  149.     sysfs_path="${1#/sys}"
  150.     if /sbin/udevadm info --query=all --path="${sysfs_path}" | egrep -q "DEVTYPE=disk"; then
  151.         return 0
  152.     fi
  153.     if echo ${sysfs_path} | grep -q "^/block/dm-"; then
  154.         return 0
  155.     fi
  156.     return 1
  157. }
  158.  
  159. copy_live_to() {
  160.     copyfrom="${1}"
  161.     copytodev="${2}"
  162.     copyto="${copyfrom}_swap"
  163.  
  164.     size=$(fs_size "" ${copyfrom} "used")
  165.  
  166.     if [ "${copytodev}" = "ram" ]; then
  167.         # copying to ram:
  168.         freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
  169.         mount_options="-o size=${size}k"
  170.         free_string="memory"
  171.         fstype="tmpfs"
  172.         dev="/dev/shm"
  173.     else
  174.         # it should be a writable block device
  175.         if [ -b "${copytodev}" ]; then
  176.             dev="${copytodev}"
  177.             free_string="space"
  178.             fstype=$(get_fstype "${dev}")
  179.             freespace=$(fs_size "${dev}")
  180.         else
  181.             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
  182.             return 1
  183.         fi
  184.     fi
  185.     if [ "${freespace}" -lt "${size}" ] ; then
  186.         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k < ${size}k) to copy live media in ${copytodev}."
  187.         return 1
  188.     fi
  189.  
  190.     # begin copying..
  191.     mkdir "${copyto}"
  192.     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
  193.     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
  194.     cp -a ${copyfrom}/* ${copyto}
  195.     if [ -e ${copyfrom}/.disk ]; then
  196.         cp -a ${copyfrom}/.disk ${copyto}
  197.     fi
  198.     umount ${copyfrom}
  199.     mount -r -o move ${copyto} ${copyfrom}
  200.     rmdir ${copyto}
  201.     return 0
  202. }
  203.  
  204. do_netmount() {
  205.     rc=1
  206.  
  207.     modprobe "${MP_QUIET}" af_packet # For DHCP
  208.  
  209.     udevadm trigger
  210.     udevadm settle
  211.  
  212.     configure_networking
  213.     export DEVICE
  214.  
  215.     if [ "${NFSROOT}" = "auto" ]; then
  216.         NFSROOT=${ROOTSERVER}:${ROOTPATH}
  217.     fi
  218.  
  219.         case ${NETBOOT} in
  220.                 nfs)
  221.                         [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
  222.                         if do_nfsmount ; then rc=0; fi  ;;
  223.                 cifs)
  224.                         [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
  225.                         if do_cifsmount ; then rc=0; fi ;;
  226.                 http|httpfs)
  227.                         [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${URL}"
  228.                         if do_httpmount ; then rc=0; fi ;;
  229.         esac
  230.  
  231.  
  232.     [ "$quiet" != "y" ] && log_end_msg
  233.     return ${rc}
  234. }
  235.  
  236. do_httpmount() {
  237.                 rc=1
  238.                 mkdir -p ${mountpoint}
  239.                 mount -t tmpfs -o size=`wget ${URL} --spider --server-response -O - 2>&1 | sed -ne '/Content-Length/{s/.*: //;p}'` tmpfs ${mountpoint}
  240.                 mkdir -p ${mountpoint}/casper
  241.         if wget ${URL} -O ${mountpoint}/casper/root.squashfs; then rc=0; fi
  242.                 return ${rc}
  243. }
  244.  
  245.  
  246.  
  247. do_nfsmount() {
  248.     rc=1
  249.     modprobe "${MP_QUIET}" nfs
  250.     if [ -z "${NFSOPTS}" ]; then
  251.         NFSOPTS=""
  252.     else
  253.         NFSOPTS=",${NFSOPTS}"
  254.     fi
  255.  
  256.     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
  257.     # FIXME: This while loop is an ugly HACK round an nfs bug
  258.     i=0
  259.     while [ "$i" -lt 60 ]; do
  260.         nfsmount -o nolock -o ro${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
  261.         sleep 1
  262.         i="$(($i + 1))"
  263.     done
  264.     return ${rc}
  265. }
  266.  
  267. do_cifsmount() {
  268.     rc=1
  269.     if [ -x "/sbin/mount.cifs" ]; then
  270.         if [ -z "${NFSOPTS}" ]; then
  271.             CIFSOPTS="-ouser=root,password="
  272.         else
  273.             CIFSOPTS="${NFSOPTS}"
  274.         fi
  275.  
  276.         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
  277.         modprobe "${MP_QUIET}" cifs
  278.  
  279.         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
  280.             rc=0
  281.         fi
  282.     fi
  283.     return ${rc}
  284. }
  285.  
  286. do_snap_copy ()
  287. {
  288.     fromdev="${1}"
  289.     todir="${2}"
  290.     snap_type="${3}"
  291.  
  292.     size=$(fs_size "${fromdev}" "" "used")
  293.  
  294.     if [ -b "${fromdev}" ]; then
  295.         # look for free mem
  296.         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
  297.             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
  298.             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
  299.         else
  300.             freespace=$(awk '/^MemFree:/{f=$2} /^Cached:/{c=$2} END{print f+c}' /proc/meminfo)
  301.         fi
  302.  
  303.         tomount="/mnt/tmpsnap"
  304.         if [ ! -d "${tomount}" ] ; then
  305.             mkdir -p "${tomount}"
  306.         fi
  307.  
  308.         fstype=$(get_fstype "${fromdev}")
  309.         if [ -n "${fstype}" ]; then
  310.             # Copying stuff...
  311.             mount -t "${fstype}" -o ro,noatime "${fromdev}" "${tomount}"
  312.             cp -a "${tomount}"/* ${todir}
  313.             umount "${tomount}"
  314.         else
  315.             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
  316.         fi
  317.  
  318.         rmdir "${tomount}"
  319.         if echo ${fromdev} | grep -qs loop; then
  320.            losetup -d "${fromdev}"
  321.         fi
  322.         return 0
  323.     else
  324.         return 1
  325.         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
  326.     fi
  327. }
  328.  
  329. try_snap ()
  330. {
  331.     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
  332.     #   and remember the device and filename for resync on exit in casper.init
  333.  
  334.     snap_label="${1}"
  335.     snap_mount="${2}"
  336.     snap_type="${3}"
  337.  
  338.     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
  339.     if [ ! -z "${snapdata}" ]; then
  340.         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
  341.         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
  342.         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
  343.         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
  344.             # squashfs or ext2 snapshot
  345.             dev=$(get_backing_device "${snapback}/${snapfile}")
  346.             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
  347.                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
  348.                  return 1
  349.             fi
  350.         else
  351.             # cpio.gz snapshot
  352.             # Unfortunately klibc's cpio is incompatible with the rest of
  353.             # the world; everything else requires -u -d, while klibc doesn't
  354.             # implement them. Try to detect whether it's in use.
  355.             cpiopath="$(which cpio)" || true
  356.             if [ "$cpiopath" ] && grep -aq /lib/klibc "$cpiopath"; then
  357.                 cpioargs=
  358.             else
  359.                 cpioargs='-u -d'
  360.             fi
  361.             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i $cpioargs 2>/dev/null) ; then
  362.                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
  363.                 return 1
  364.             fi
  365.         fi
  366.         umount "${snapback}"
  367.     else
  368.         dev=$(find_cow_device "${snap_label}")
  369.         if [ -b "${dev}" ]; then
  370.             if echo "${dev}" | grep -qs loop; then
  371.                 # strange things happens, user confused?
  372.                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
  373.                 snapfile=$(basename ${snaploop})
  374.                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
  375.             else
  376.                 snapdev="${dev}"
  377.             fi
  378.             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
  379.                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
  380.                 return 1
  381.             else
  382.                 if [ -n "${snapfile}" ]; then
  383.                      # it was a loop device, user confused
  384.                      umount ${snapdev}
  385.                 fi
  386.             fi
  387.         else
  388.             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
  389.             return 1
  390.         fi
  391.     fi
  392.     echo "export ${snap_type}SNAP="/cow${snap_mount#$rootmnt}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
  393.     return 0
  394. }
  395.  
  396. cow_write_format() {
  397.     {
  398.         echo "# This records the union filesystem format used for this cow medium; do not delete or alter."
  399.         echo "UNIONFS=$1"
  400.     } >"$2"
  401. }
  402.  
  403. setup_unionfs() {
  404.     image_directory="$1"
  405.     rootmnt="$2"
  406.  
  407.     # Mount up the writable layer, if it is persistent then it may well
  408.     # tell us what format we should be using.
  409.     mkdir -p /cow
  410.     cowdevice="tmpfs"
  411.     cow_fstype="tmpfs"
  412.     cow_mountopt="rw,noatime,mode=755"
  413.  
  414.     # Looking for "${root_persistence}" device or file
  415.     if [ -n "${PERSISTENT}" ]; then
  416.         cowprobe=$(find_cow_device "${root_persistence}")
  417.         if [ -b "${cowprobe}" ]; then
  418.             cowdevice=${cowprobe}
  419.             cow_fstype=$(get_fstype "${cowprobe}")
  420.             cow_mountopt="rw,noatime"
  421.         else
  422.             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
  423.         fi
  424.     fi
  425.  
  426.     mount -t ${cow_fstype} -o ${cow_mountopt} ${cowdevice} /cow || panic "Can not mount $cowdevice on /cow"
  427.  
  428.     # Work out if this is an existing persistance partition and if so
  429.     # move its existing "upper" into a new upper directory.
  430.     if [ ! -d /cow/upper ]; then
  431.         mkdir -p /cow/upper
  432.         for cow_content in /cow/*
  433.         do
  434.             case "$cow_content" in
  435.             /cow/lost+found|/cow/upper)     continue ;;
  436.             esac
  437.  
  438.             mv "$cow_content" /cow/upper
  439.  
  440.             # We have no format information, so fake some up.  If they
  441.             # have not specified any then we simply assume it was
  442.             # overlayfs (V1 format).
  443.             cow_write_format "overlayfs" /cow/format
  444.         done
  445.     fi
  446.     mkdir -p /cow/work
  447.  
  448.     # Look and see if we have a current format, it is essential we use
  449.     # a compatible format.  Relies on the recovery above having cleaned
  450.     # out the top level.
  451.     [ -f /cow/format ] && . /cow/format
  452.  
  453.     # See if we have the appropriate format, pick the first one if
  454.     # unspecified.
  455.     if [ "${UNIONFS}" = 'DEFAULT' ]; then
  456.         for union in 'overlay' 'overlayfs' 'aufs' 'unionfs'
  457.         do
  458.             modprobe "${MP_QUIET}" -b ${union} || true
  459.             if cut -f2 /proc/filesystems | grep -q "^${union}\$"; then
  460.                 UNIONFS="${union}"
  461.                 break
  462.             fi
  463.         done
  464.     fi
  465.     if [ "${UNIONFS}" = 'DEFAULT' -a -x /bin/unionfs-fuse ]; then
  466.         UNIONFS="unionfs-fuse"
  467.     fi
  468.     # If all else fails fall back to aufs.
  469.     if [ "${UNIONFS}" = 'DEFAULT' ]; then
  470.         UNIONFS='aufs'
  471.     fi
  472.     # Confirm the final format was valid.
  473.     if [ "${UNIONFS}" != "unionfs-fuse" ]; then
  474.         modprobe "${MP_QUIET}" -b "${UNIONFS}" || true
  475.         if cut -f2 /proc/filesystems | grep -q "^${UNIONFS}\$"; then
  476.             :
  477.         else
  478.             panic "/cow format specified as ${UNIONFS} and no support found"
  479.         fi
  480.     fi
  481.  
  482.     # run-init can't deal with images in a subdir, but we're going to
  483.     # move all of these away before it runs anyway.  No, we're not,
  484.     # put them in / since move-mounting them into / breaks mono and
  485.     # some other apps.
  486.  
  487.     croot="/"
  488.  
  489.     # Let's just mount the read-only file systems first
  490.     rofsstring=""
  491.     rofslist=""
  492.     if [ "${UNIONFS}" = "aufs" ]; then
  493.         roopt="rr"
  494.     elif [ "${UNIONFS}" = "unionfs-fuse" ]; then
  495.         roopt="RO"
  496.     else
  497.         roopt="ro"
  498.     fi
  499.  
  500.     mkdir -p "${croot}"
  501.     for image_type in "ext2" "squashfs" "dir" ; do
  502.         for image in "${image_directory}"/*."${image_type}"; do
  503.             imagename=$(basename "${image}")
  504.  
  505.             # Skip Edubuntu's extra squashfs
  506.             if [ "$imagename" = "ltsp.squashfs" ] ||
  507.                [ "$imagename" = "server.squashfs" ]; then
  508.                 continue
  509.             fi
  510.  
  511.             if [ -d "${image}" ]; then
  512.                 # it is a plain directory: do nothing
  513.                 rofsstring="${image}=${roopt}:${rofsstring}"
  514.                 rofslist="${image} ${rofslist}"
  515.             elif [ -f "${image}" ]; then
  516.                 backdev=$(get_backing_device "$image")
  517.                 fstype=$(get_fstype "${backdev}")
  518.                 if [ "${fstype}" = "unknown" ]; then
  519.                     panic "Unknown file system type on ${backdev} (${image})"
  520.                 fi
  521.                 mkdir -p "${croot}/${imagename}"
  522.                 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}"
  523.             fi
  524.         done
  525.     done
  526.     rofsstring=${rofsstring%:}
  527.  
  528.     # Record the format we are using for this mount.
  529.     [ ! -f /cow/format ] && cow_write_format "${UNIONFS}" /cow/format
  530.     case ${UNIONFS} in
  531.         unionfs-fuse)
  532.             (ulimit -n 16384; unionfs-fuse -o cow -o noinitgroups -o default_permissions -o allow_other -o use_ino -o suid /cow/upper=RW:$rofsstring "$rootmnt" || panic "${UNIONFS} mount failed")
  533.             mkdir -p /dev/.initramfs/varrun
  534.             pidof unionfs-fuse >> /dev/.initramfs/varrun/sendsigs.omit || true
  535.             ;;
  536.         aufs|unionfs)
  537.             mount -t ${UNIONFS} -o noatime,dirs=/cow/upper=rw:$rofsstring ${UNIONFS} "$rootmnt" || panic "${UNIONFS} mount failed"
  538.             ;;
  539.         overlay|overlayfs)
  540.             # Mount the layers pairwise from the bottom onto rootmnt,
  541.             # for the second and later layers rootmnt forms the lower layer.
  542.             mounts=""
  543.             for mount in /cow $rofslist
  544.             do
  545.                 mounts="$mount $mounts"
  546.             done
  547.             lower=""
  548.             for mount in $mounts
  549.             do
  550.                 if [ "$lower" = "" ]; then
  551.                     lower="$mount"
  552.                     continue
  553.                 fi
  554.                 mount -t ${UNIONFS} -o "upperdir=$mount/upper,lowerdir=$lower,workdir=$mount/work" \
  555.                     "$mount" "$rootmnt" || \
  556.                   mount -t ${UNIONFS} -o "upperdir=$mount/upper,lowerdir=$lower" \
  557.                       "$mount" "$rootmnt"
  558.                 lower="$rootmnt"
  559.             done
  560.             ;;
  561.     esac
  562.  
  563.     # Adding other custom mounts
  564.     if [ -n "${PERSISTENT}" ]; then
  565.         # directly mount /home
  566.         # FIXME: add a custom mounts configurable system
  567.         homecow=$(find_cow_device "${home_persistence}" )
  568.         if [ -b "${homecow}" ]; then
  569.             mount -t $(get_fstype "${homecow}") -o rw,noatime "${homecow}" "${rootmnt}/home"
  570.             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
  571.         else
  572.             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
  573.         fi
  574.         # Look for other snapshots to copy in
  575.         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
  576.         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
  577.     fi
  578.  
  579.     if [ -n "${SHOWMOUNTS}" ]; then
  580.         for d in ${rofslist}; do
  581.             mkdir -p "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
  582.             case d in
  583.                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
  584.                     ;;
  585.                 *)
  586.                     if [ "${UNIONFS}" = unionfs-fuse ]; then
  587.                         mount -o bind "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
  588.                     else
  589.                         mount -o move "${d}" "${rootmnt}/${LIVE_MEDIA_PATH}/${d##*/}"
  590.                     fi
  591.                     ;;
  592.             esac
  593.         done
  594.         # shows cow fs on /cow for use by casper-snapshot
  595.         mkdir -p "${rootmnt}/cow"
  596.         mount -o bind /cow "${rootmnt}/cow"
  597.     fi
  598.  
  599.     # move the first mount; no head in busybox-initramfs
  600.     for d in $(mount -t squashfs | cut -d\  -f 3); do
  601.         mkdir -p "${rootmnt}/rofs"
  602.         if [ "${UNIONFS}" = unionfs-fuse ]; then
  603.             mount -o bind "${d}" "${rootmnt}/rofs"
  604.         else
  605.             mount -o move "${d}" "${rootmnt}/rofs"
  606.         fi
  607.         break
  608.     done
  609. }
  610.  
  611. check_dev ()
  612. {
  613.     sysdev="${1}"
  614.     devname="${2}"
  615.     skip_uuid_check="${3}"
  616.     if [ -z "${devname}" ]; then
  617.         devname=$(sys2dev "${sysdev}")
  618.     fi
  619.  
  620.     if [ -d "${devname}" ]; then
  621.         mount -o bind "${devname}" $mountpoint || continue
  622.         if is_casper_path $mountpoint; then
  623.             echo $mountpoint
  624.             return 0
  625.         else
  626.             umount $mountpoint
  627.         fi
  628.     fi
  629.     [ -e "$devname" ] || continue
  630.  
  631.     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
  632.         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}")
  633.         devname="${loopdevname}"
  634.     fi
  635.  
  636.     fstype=$(get_fstype "${devname}")
  637.     if is_supported_fs ${fstype}; then
  638.         devuid=$(blkid -o value -s UUID "$devname")
  639.         [ -n "$devuid" ] && grep -qs "\<$devuid\>" $tried && continue
  640.         mount -t ${fstype} -o ro,noatime "${devname}" $mountpoint || continue
  641.         [ -n "$devuid" ] && echo "$devuid" >> $tried
  642.         if is_casper_path $mountpoint && \
  643.            ([ "$skip_uuid_check" ] || [ "$UUID" = "$devuid" ] || matches_uuid $mountpoint); then
  644.             echo $mountpoint
  645.             return 0
  646.         else
  647.             umount $mountpoint
  648.         fi
  649.     fi
  650.  
  651.     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
  652.         losetup -d "${loopdevname}"
  653.     fi
  654.     return 1
  655. }
  656.  
  657. is_mapper() {
  658.     dev="$1"
  659.     # we don't consider control a mapper device
  660.     echo "$dev" |grep "/dev/mapper/control" >/dev/null && return 1
  661.     echo "$dev" |grep "/dev/mapper/.\+" >/dev/null && return 0
  662.     return 1
  663. }
  664.  
  665. is_md() {
  666.     dev="$1"
  667.     echo "$dev" |grep "/dev/md[0-9]" >/dev/null && return 0
  668.     return 1
  669. }
  670.  
  671. find_livefs() {
  672.     timeout="${1}"
  673.     # first look at the one specified in the command line
  674.     if [ ! -z "${LIVEMEDIA}" ]; then
  675.         if check_dev "null" "${LIVEMEDIA}" "skip_uuid_check"; then
  676.             return 0
  677.         fi
  678.     fi
  679.     # don't start autodetection before timeout has expired
  680.     if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then
  681.         if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then
  682.             return 1
  683.         fi
  684.     fi
  685.     # or do the scan of block devices
  686.     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | egrep -v "/(loop|ram|fd|md)"); do
  687.         devname=$(sys2dev "${sysblock}")
  688.         [ -e "$devname" ] || continue
  689.         fstype=$(get_fstype "${devname}")
  690.         if /lib/udev/cdrom_id ${devname} > /dev/null; then
  691.             if check_dev "null" "${devname}" ; then
  692.                 return 0
  693.             fi
  694.         elif is_nice_device "${sysblock}" ; then
  695.             for dev in $(subdevices "${sysblock}"); do
  696.                 if check_dev "${dev}" ; then
  697.                     return 0
  698.                 fi
  699.             done
  700.         elif is_md "${devname}" || is_mapper "${devname}" ; then
  701.             if check_dev "null" "${devname}" ; then
  702.                 return 0
  703.             fi
  704.         elif [ "${fstype}" = "squashfs" -o \
  705.                 "${fstype}" = "ext4" -o \
  706.                 "${fstype}" = "ext3" -o \
  707.                 "${fstype}" = "ext2" -o \
  708.                 "${fstype}" = "btrfs" ]; then
  709.             # This is an ugly hack situation, the block device has
  710.             # an image directly on it.  It's hopefully
  711.             # casper, so take it and run with it.
  712.             ln -s "${devname}" "${devname}.${fstype}"
  713.             echo "${devname}.${fstype}"
  714.             return 0
  715.         fi
  716.     done
  717.     return 1
  718. }
  719.  
  720. mountroot() {
  721.     exec 6>&1
  722.     exec 7>&2
  723.     exec > casper.log
  724.     exec 2>&1
  725.     tail -f casper.log >&7 &
  726.     tailpid="$!"
  727.  
  728.     parse_cmdline
  729.  
  730.     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
  731.     run_scripts /scripts/casper-premount
  732.     [ "$quiet" != "y" ] && log_end_msg
  733.  
  734.     if [ ! -z "${NETBOOT}" ]; then
  735.         if do_netmount ; then
  736.             livefs_root="${mountpoint}"
  737.         else
  738.             panic "Unable to find a live file system on the network"
  739.         fi
  740.     else
  741.         # Scan local devices for the image
  742.         i=0
  743.         while [ "$i" -lt 60 ]; do
  744.             livefs_root=$(find_livefs $i)
  745.             if [ "${livefs_root}" ]; then
  746.                 break
  747.             fi
  748.             sleep 1
  749.             i="$(($i + 1))"
  750.         done
  751.     fi
  752.  
  753.     if [ -z "${livefs_root}" ]; then
  754.         panic "Unable to find a medium containing a live file system"
  755.     fi
  756.  
  757.     if [ "${TORAM}" ]; then
  758.         live_dest="ram"
  759.     elif [ "${TODISK}" ]; then
  760.         live_dest="${TODISK}"
  761.     fi
  762.     if [ "${live_dest}" ]; then
  763.         log_begin_msg "Copying live_media to ${live_dest}"
  764.         copy_live_to "${livefs_root}" "${live_dest}"
  765.         log_end_msg
  766.     fi
  767.  
  768.     mount_images_in_directory "${livefs_root}" "${rootmnt}"
  769.  
  770.     # initialize the /var/crash directory in overlayfs so that inotify for
  771.     # /var/crash works and update-notifier will notify of crashes
  772.     touch /root/var/crash/crash.init
  773.     rm /root/var/crash/crash.init
  774.  
  775.     log_end_msg
  776.  
  777.     # Allow to override USERNAME and HOST based on media information
  778.     # make it skipable by setting FLAVOUR= in casper.conf
  779.     if [ -f /cdrom/.disk/info ] && [ -z "$FLAVOUR" ]; then
  780.         FLAVOUR="$(cut -d' ' -f1 "/cdrom/.disk/info" 2>/dev/null | tr '[A-Z]' '[a-z]')" || FLAVOUR=
  781.         if [ -n "$FLAVOUR" ]; then
  782.             HOST=$FLAVOUR
  783.             USERNAME=$FLAVOUR
  784.             export HOST USERNAME
  785.             sed -i "s,USERNAME=.*,USERNAME=\"$FLAVOUR\",g; s,HOST=.*,HOST=\"$FLAVOUR\",g" /etc/casper.conf
  786.         fi
  787.     fi
  788.  
  789.     # Apply command lines override of HOST, USERNAME and USERFULLNAME
  790.     [ -n "$CMD_HOST" ] && HOST=$CMD_HOST && export HOST
  791.     [ -n "$CMD_USERNAME" ] && USERNAME=$CMD_USERNAME && export USERNAME
  792.     [ -n "$CMD_USERFULLNAME" ] && USERFULLNAME=$CMD_USERFULLNAME && export USERFULLNAME
  793.     if [ -n "$CMD_HOST" ] || [ -n "$CMD_USERNAME" ] || [ -n "$CMD_USERFULLNAME" ]; then
  794.         sed -i "s,USERNAME=.*,USERNAME=\"$USERNAME\",g; s,USERFULLNAME=.*,USERFULLNAME=\"$USERFULLNAME\",g; s,HOST=.*,HOST=\"$HOST\",g" /etc/casper.conf
  795.     fi
  796.  
  797.     # unionfs-fuse needs /dev to be bind-mounted for the duration of
  798.     # casper-bottom; udev's init script will take care of things after that
  799.     if [ "${UNIONFS}" = unionfs-fuse ]; then
  800.         mount -n -o bind /dev "${rootmnt}/dev"
  801.     fi
  802.  
  803.     # Open up two fifo's fd's for debconf-communicate to use. Speeds up
  804.     # the Casper process considerably.
  805.     log_begin_msg "Creating debconf-communicate fifo mechanism"
  806.     mkfifo /tmp/debconf-in.fifo
  807.     mkfifo /tmp/debconf-out.fifo
  808.  
  809.     # Make the template database read-only, so that passthrough debconf
  810.     # instances can write to it directly; otherwise templates are only
  811.     # passed through when necessary.  Use temporary config databases as
  812.     # well; we'll copy their contents back at the end.
  813.     DEBCONF_TMPDIR="$(chroot /root mktemp -dt debconf.XXXXXX)"
  814.     cp -a /root/var/cache/debconf/config.dat "/root$DEBCONF_TMPDIR/"
  815.     cp -a /root/var/cache/debconf/passwords.dat "/root$DEBCONF_TMPDIR/"
  816.     sed "s,^Filename: /var/cache/debconf/\(config\|passwords\).dat$,Filename: $DEBCONF_TMPDIR/\1.dat,; /^Name: templatedb/a\
  817. Readonly: true" /root/etc/debconf.conf >"/root$DEBCONF_TMPDIR/debconf.conf"
  818.  
  819.     DEBCONF_SYSTEMRC="$DEBCONF_TMPDIR/debconf.conf" chroot /root debconf-communicate -fnoninteractive casper > /tmp/debconf-out.fifo < /tmp/debconf-in.fifo &
  820.     debconfpid="$!"
  821.  
  822.     if [ ! -p /tmp/debconf-in.fifo ] || [ ! -p /tmp/debconf-out.fifo ]; then
  823.         log_warning_msg "failed to setup debconf-communicate channel"
  824.     fi
  825.     log_end_msg
  826.  
  827.     # Order matters!
  828.     # These file descriptors must stay open until we're finished with
  829.     # debconf-communicate.
  830.     exec 4</tmp/debconf-out.fifo 3>/tmp/debconf-in.fifo
  831.  
  832.     maybe_break casper-bottom
  833.     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
  834.  
  835.     run_scripts /scripts/casper-bottom
  836.     [ "$quiet" != "y" ] && log_end_msg
  837.  
  838.     if [ "${UNIONFS}" = unionfs-fuse ]; then
  839.         umount "${rootmnt}/dev"
  840.     fi
  841.  
  842.     # Close the fd's associated with debconf-communicate.
  843.     exec 3>&- 4<&-
  844.     rm -f /tmp/debconf-in.fifo
  845.     rm -f /tmp/debconf-out.fifo
  846.     wait $debconfpid
  847.  
  848.     # Copy config database changes back to the master files.
  849.     chroot /root debconf-copydb tmpdb config \
  850.         --config=Name:tmpdb --config=Driver:File \
  851.         --config="Filename:$DEBCONF_TMPDIR/config.dat"
  852.     chroot /root debconf-copydb tmpdb passwords \
  853.         --config=Name:tmpdb --config=Driver:File \
  854.         --config="Filename:$DEBCONF_TMPDIR/passwords.dat"
  855.     rm -rf "$DEBCONF_TMPDIR"
  856.  
  857.     exec 1>&6 6>&-
  858.     exec 2>&7 7>&-
  859.     kill "$tailpid"
  860.     cp casper.log "${rootmnt}/var/log/"
  861.     if [ -f /etc/casper.conf ]; then
  862.         cp /etc/casper.conf "${rootmnt}/etc/"
  863.     fi
  864. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement