Advertisement
Guest User

Untitled

a guest
Jan 28th, 2012
636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 171.69 KB | None | 0 0
  1. #================
  2. # FILE          : KIWILinuxRC.sh
  3. #----------------
  4. # PROJECT       : OpenSUSE Build-Service
  5. # COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH, Germany
  6. #               :
  7. # AUTHOR        : Marcus Schaefer <ms@suse.de>
  8. #               :
  9. # BELONGS TO    : Operating System images
  10. #               :
  11. # DESCRIPTION   : This module contains common used functions
  12. #               : for the suse linuxrc and preinit boot image
  13. #               : files
  14. #               :
  15. #               :
  16. # STATUS        : Development
  17. #----------------
  18. #======================================
  19. # Exports (General)
  20. #--------------------------------------
  21. export ELOG_FILE=/var/log/boot.kiwi
  22. export TRANSFER_ERRORS_FILE=/tmp/transfer.errors
  23. export UFONT=/usr/share/fbiterm/fonts/b16.pcf.gz
  24. export HYBRID_PERSISTENT_FS=ext3
  25. export HYBRID_PERSISTENT_ID=83
  26. export HYBRID_PERSISTENT_DIR=/read-write
  27. export UTIMER_INFO=/dev/utimer
  28.  
  29. #======================================
  30. # Exports (General)
  31. #--------------------------------------
  32. test -z "$ELOG_CONSOLE"       && export ELOG_CONSOLE=/dev/tty3
  33. test -z "$ELOG_BOOTSHELL"     && export ELOG_BOOTSHELL=/dev/tty2
  34. test -z "$ELOG_EXCEPTION"     && export ELOG_EXCEPTION=/dev/tty1
  35. test -z "$KLOG_CONSOLE"       && export KLOG_CONSOLE=4
  36. test -z "$KLOG_DEFAULT"       && export KLOG_DEFAULT=1
  37. test -z "$ELOG_STOPPED"       && export ELOG_STOPPED=0
  38. test -z "$PARTITIONER"        && export PARTITIONER=parted
  39. test -z "$DEFAULT_VGA"        && export DEFAULT_VGA=0x314
  40. test -z "$HAVE_MODULES_ORDER" && export HAVE_MODULES_ORDER=1
  41. test -z "$DIALOG_LANG"        && export DIALOG_LANG=ask
  42. test -z "$TERM"               && export TERM=linux
  43. test -z "$LANG"               && export LANG=en_US.utf8
  44. test -z "$UTIMER"             && export UTIMER=0
  45. test -z "$VGROUP"             && export VGROUP=kiwiVG
  46. test -z "$PARTED_HAVE_ALIGN"  && export PARTED_HAVE_ALIGN=0
  47. test -z "$PARTED_HAVE_MACHINE"&& export PARTED_HAVE_MACHINE=0
  48. test -z "$DHCPCD_HAVE_PERSIST"&& export DHCPCD_HAVE_PERSIST=1
  49. if [ -x /sbin/blogd ];then
  50.     test -z "$CONSOLE"            && export CONSOLE=/dev/console
  51.     test -z "$REDIRECT"           && export REDIRECT=/dev/tty1
  52. fi
  53. if parted -h | grep -q '\-\-align';then
  54.     export PARTED_HAVE_ALIGN=1
  55. fi
  56. if parted -h | grep -q '\-\-machine';then
  57.     export PARTED_HAVE_MACHINE=1
  58. fi
  59. if [ $PARTED_HAVE_MACHINE -eq 0 ];then
  60.     export PARTITIONER=sfdisk
  61. fi
  62. if dhcpcd -p 2>&1 | grep -q 'Usage';then
  63.     export DHCPCD_HAVE_PERSIST=0
  64. fi
  65.  
  66. #======================================
  67. # Dialog
  68. #--------------------------------------
  69. function Dialog {
  70.     local code=1
  71.     export DIALOG_CANCEL=1
  72.     if [ -e /dev/fb0 ];then
  73.         cat > /tmp/fbcode <<- EOF
  74.             dialog \
  75.                 --ok-label "$TEXT_OK" \
  76.                 --cancel-label "$TEXT_CANCEL" \
  77.                 --yes-label "$TEXT_YES" \
  78.                 --no-label "$TEXT_NO" \
  79.                 --exit-label "$TEXT_EXIT" \
  80.                 $@
  81.             echo \$? > /tmp/fbcode
  82.         EOF
  83.         fbiterm -m $UFONT -- bash /tmp/fbcode
  84.         code=$(cat /tmp/fbcode)
  85.     else
  86.         eval dialog \
  87.             --ok-label "$TEXT_OK" \
  88.             --cancel-label "$TEXT_CANCEL" \
  89.             --yes-label "$TEXT_YES" \
  90.             --no-label "$TEXT_NO" \
  91.             --exit-label "$TEXT_EXIT" \
  92.             $@
  93.         code=$?
  94.     fi
  95.     return $code
  96. }
  97. #======================================
  98. # Debug
  99. #--------------------------------------
  100. function Debug {
  101.     # /.../
  102.     # print message if variable DEBUG is set to 1
  103.     # -----
  104.     if test "$DEBUG" = 1;then
  105.         echo "+++++> $1"
  106.     fi
  107. }
  108. #======================================
  109. # Echo
  110. #--------------------------------------
  111. function Echo {
  112.     # /.../
  113.     # print a message to the controling terminal
  114.     # ----
  115.     if [ $ELOG_STOPPED = 0 ];then
  116.         set +x
  117.     fi
  118.     if [ ! $UTIMER = 0 ] && kill -0 $UTIMER &>/dev/null;then
  119.         kill -HUP $UTIMER
  120.         local prefix=$(cat $UTIMER_INFO)
  121.     else
  122.         local prefix="===>"
  123.     fi
  124.     local option=""
  125.     local optn=""
  126.     local opte=""
  127.     while getopts "bne" option;do
  128.         case $option in
  129.             b) prefix="    " ;;
  130.             n) optn="-n" ;;
  131.             e) opte="-e" ;;
  132.             *) echo "Invalid argument: $option" ;;
  133.         esac
  134.     done
  135.     shift $(($OPTIND - 1))
  136.     if [ $ELOG_STOPPED = 0 ];then
  137.         set -x
  138.     fi
  139.     echo $optn $opte "$prefix $1"
  140.     if [ $ELOG_STOPPED = 0 ];then
  141.         set +x
  142.     fi
  143.     OPTIND=1
  144.     if [ $ELOG_STOPPED = 0 ];then
  145.         set -x
  146.     fi
  147. }
  148. #======================================
  149. # WaitKey
  150. #--------------------------------------
  151. function WaitKey {
  152.     # /.../
  153.     # if DEBUG is set wait for ENTER to continue
  154.     # ----
  155.     if test "$DEBUG" = 1;then
  156.         Echo -n "Press ENTER to continue..."
  157.         read
  158.     fi
  159. }
  160. #======================================
  161. # closeKernelConsole
  162. #--------------------------------------
  163. function closeKernelConsole {
  164.     # /.../
  165.     # close the kernel console, set level to 1
  166.     # ----
  167.     if [ -x /usr/sbin/klogconsole ];then
  168.         klogconsole -l 1
  169.     fi
  170. }
  171. #======================================
  172. # openKernelConsole
  173. #--------------------------------------
  174. function openKernelConsole {
  175.     # /.../
  176.     # move the kernel console to terminal 3 as you can't see the messages
  177.     # now directly it looks like the kernel console is switched off
  178.     # but it isn't really. If DEBUG is set the logging remains on
  179.     # the first console
  180.     # ----
  181.     if [ ! -x /usr/sbin/klogconsole ];then
  182.         return
  183.     fi
  184.     if test "$DEBUG" = 0;then
  185.         Echo "Kernel logging enabled on: /dev/tty$KLOG_CONSOLE"
  186.         setctsid /dev/tty$KLOG_CONSOLE \
  187.             klogconsole -l 7 -r$KLOG_CONSOLE
  188.     fi
  189. }
  190. #======================================
  191. # reopenKernelConsole
  192. #--------------------------------------
  193. function reopenKernelConsole {
  194.     # /.../
  195.     # reopen kernel console to be able to see kernel messages
  196.     # while the system is booting
  197.     # ----
  198.     if [ ! -x /usr/sbin/klogconsole ];then
  199.         return
  200.     fi
  201.     Echo "Kernel logging enabled on: /dev/tty$KLOG_DEFAULT"
  202.     klogconsole -l 7 -r$KLOG_DEFAULT
  203. }
  204. #======================================
  205. # importFile
  206. #--------------------------------------
  207. function importFile {
  208.     # /.../
  209.     # import the config.<MAC> style format. the function
  210.     # will export each entry of the file as variable into
  211.     # the current shell environment
  212.     # ----
  213.     IFS="
  214.     "
  215.     while read line;do
  216.         echo $line | grep -qi "^#" && continue
  217.         key=`echo "$line" | cut -d '=' -f1`
  218.         item=`echo "$line" | cut -d '=' -f2-`
  219.         if [ -z "$key" ] || [ -z "$item" ];then
  220.             continue
  221.         fi
  222.         if ! echo $item | grep -E -q "^(\"|')";then
  223.             item="'"$item"'"
  224.         fi
  225.         Debug "$key=$item"
  226.         eval export "$key\=$item"
  227.     done
  228.     if [ ! -z "$ERROR_INTERRUPT" ];then
  229.         Echo -e "$ERROR_INTERRUPT"
  230.         systemException "*** interrupted ****" "shell"
  231.     fi
  232.     IFS=$IFS_ORIG
  233. }
  234. #======================================
  235. # systemException
  236. #--------------------------------------
  237. function systemException {
  238.     # /.../
  239.     # print a message to the controling terminal followed
  240.     # by an action. Possible actions are reboot, wait
  241.     # and opening a shell
  242.     # ----
  243.     set +x
  244.     local what=$2
  245.     local nuldev=/dev/null
  246.     local ttydev=$ELOG_EXCEPTION
  247.     if [ ! -e $nuldev ];then
  248.         nuldev=/mnt/$nuldev
  249.     fi
  250.     if [ ! -e $ttydev ];then
  251.         ttydev=/mnt/$ttydev
  252.     fi
  253.     test -e /proc/splash && echo verbose > /proc/splash
  254.     if [ $what = "reboot" ];then
  255.         if cat /proc/cmdline | grep -qi "kiwidebug=1";then
  256.             what="shell"
  257.         fi
  258.     fi
  259.     Echo "$1"
  260.     case "$what" in
  261.     "reboot")
  262.         Echo "rebootException: error consoles at Alt-F3/F4"
  263.         Echo "rebootException: reboot in 120 sec..."; sleep 120
  264.         /sbin/reboot -f -i >$nuldev
  265.     ;;
  266.     "wait")
  267.         Echo "waitException: waiting for ever..."
  268.         while true;do sleep 100;done
  269.     ;;
  270.     "shell")
  271.         Echo "shellException: providing shell..."
  272.         setctsid $ttydev /bin/bash -i
  273.     ;;
  274.     "user_reboot")
  275.         Echo "reboot triggered by user: consoles at Alt-F3/F4"
  276.         Echo "reboot in 30 sec..."; sleep 30
  277.         /sbin/reboot -f -i >$nuldev
  278.     ;;
  279.     *)
  280.         Echo "unknownException..."
  281.     ;;
  282.     esac
  283. }
  284. #======================================
  285. # copyDevices
  286. #--------------------------------------
  287. function copyDeviceNodes {
  288.     local search=$1
  289.     local prefix=$2
  290.     local dtype
  291.     local major
  292.     local minor
  293.     local perms
  294.     if [ -z "$search" ];then
  295.         search=/dev
  296.     fi
  297.     pushd $search >/dev/null
  298.     for i in *;do
  299.         if [ -e $prefix/$i ];then
  300.             continue
  301.         fi
  302.         if [ -b $i ];then
  303.             dtype=b
  304.         elif [ -c $i ];then
  305.             dtype=c
  306.         elif [ -p $i ];then
  307.             dtype=p
  308.         else
  309.             continue
  310.         fi
  311.         info=`stat $i -c "0%a:0x%t:0x%T"`
  312.         major=`echo $info | cut -f2 -d:`
  313.         minor=`echo $info | cut -f3 -d:`
  314.         perms=`echo $info | cut -f1 -d:`
  315.         if [ $dtype = "p" ];then
  316.             mknod -m $perms $prefix/$i $dtype
  317.         else
  318.             mknod -m $perms $prefix/$i $dtype $major $minor
  319.         fi
  320.     done
  321.     popd >/dev/null
  322. }
  323. #======================================
  324. # copyDevices
  325. #--------------------------------------
  326. function createInitialDevices {
  327.     local prefix=$1
  328.     mkdir -p $prefix
  329.     if [ ! -d $prefix ];then
  330.         return
  331.     fi
  332.     if [ -e $prefix/null ];then
  333.         rm -f $prefix/null
  334.     fi
  335.     test -c $prefix/tty      || mknod -m 0666 $prefix/tty      c 5 0
  336.     test -c $prefix/tty1     || mknod -m 0666 $prefix/tty1     c 4 1
  337.     test -c $prefix/tty2     || mknod -m 0666 $prefix/tty2     c 4 2
  338.     test -c $prefix/tty3     || mknod -m 0666 $prefix/tty3     c 4 3
  339.     test -c $prefix/tty4     || mknod -m 0666 $prefix/tty4     c 4 4
  340.     test -c $prefix/console  || mknod -m 0600 $prefix/console  c 5 1
  341.     test -c $prefix/ptmx     || mknod -m 0666 $prefix/ptmx     c 5 2
  342.     exec < $prefix/console > $prefix/console
  343.     test -c $prefix/null     || mknod -m 0666 $prefix/null     c 1 3
  344.     test -c $prefix/kmsg     || mknod -m 0600 $prefix/kmsg     c 1 11
  345.     test -c $prefix/snapshot || mknod -m 0660 $prefix/snapshot c 10 231
  346.     test -c $prefix/random   || mknod -m 0666 $prefix/random   c 1 8
  347.     test -c $prefix/urandom  || mknod -m 0644 $prefix/urandom  c 1 9
  348.     test -b $prefix/loop0    || mknod -m 0640 $prefix/loop0    b 7 0
  349.     test -b $prefix/loop1    || mknod -m 0640 $prefix/loop1    b 7 1
  350.     test -b $prefix/loop2    || mknod -m 0640 $prefix/loop2    b 7 2
  351.     mkdir -p -m 0755 $prefix/pts
  352.     mkdir -p -m 1777 $prefix/shm
  353.     test -L $prefix/fd     || ln -s /proc/self/fd $prefix/fd
  354.     test -L $prefix/stdin  || ln -s fd/0 $prefix/stdin
  355.     test -L $prefix/stdout || ln -s fd/1 $prefix/stdout
  356.     test -L $prefix/stderr || ln -s fd/2 $prefix/stderr
  357. }
  358. #======================================
  359. # mountSystemFilesystems
  360. #--------------------------------------
  361. function mountSystemFilesystems {
  362.     mount -t proc  proc   /proc
  363.     mount -t sysfs sysfs  /sys
  364. }
  365. #======================================
  366. # umountSystemFilesystems
  367. #--------------------------------------
  368. function umountSystemFilesystems {
  369.     umount /dev/pts &>/dev/null
  370.     umount /sys     &>/dev/null
  371.     umount /proc    &>/dev/null
  372. }
  373. #======================================
  374. # createFramebufferDevices
  375. #--------------------------------------
  376. function createFramebufferDevices {
  377.     if [ -f /proc/fb ]; then
  378.         Echo "Creating framebuffer devices"
  379.         while read fbnum fbtype; do
  380.             if [ $(($fbnum < 32)) ] ; then
  381.                 [ -c /dev/fb$fbnum ] || mknod -m 0660 /dev/fb$fbnum c 29 $fbnum
  382.             fi
  383.         done < /proc/fb
  384.     fi
  385. }
  386. #======================================
  387. # errorLogStop
  388. #--------------------------------------
  389. function errorLogStop {
  390.     set +x
  391.     export ELOG_STOPPED=1
  392.     exec 2>$ELOG_EXCEPTION
  393. }
  394. #======================================
  395. # errorLogContinue
  396. #--------------------------------------
  397. function errorLogContinue {
  398.     exec 2>>$ELOG_FILE
  399.     export ELOG_STOPPED=0
  400.     set -x
  401. }
  402. #======================================
  403. # errorLogStart
  404. #--------------------------------------
  405. function errorLogStart {
  406.     # /.../
  407.     # Log all errors up to now to the terminal specified
  408.     # by ELOG_CONSOLE
  409.     # ----
  410.     if [ ! -f $ELOG_FILE ];then
  411.         #======================================
  412.         # Header for main stage log
  413.         #--------------------------------------
  414.         echo "KIWI Log:" >> $ELOG_FILE
  415.     else
  416.         #======================================
  417.         # Header for pre-init stage log
  418.         #--------------------------------------
  419.         startUtimer
  420.         echo "KIWI PreInit Log" >> $ELOG_FILE
  421.         cat /iprocs | grep -v TAIL_PID > /iprocs
  422.     fi
  423.     echo "Boot-Logging enabled on $ELOG_CONSOLE"
  424.     setctsid -f $ELOG_CONSOLE tail -f $ELOG_FILE &
  425.     exec 2>>$ELOG_FILE
  426.     if [ -f .profile ];then
  427.         echo "KIWI .profile contents:" 1>&2
  428.         cat .profile 1>&2
  429.     fi
  430.     set -x 1>&2
  431.     local DTYPE=`stat -f -c "%T" /proc 2>/dev/null`
  432.     if test "$DTYPE" != "proc" ; then
  433.         mount -t proc proc /proc
  434.     fi
  435.     TAIL_PID=$(fuser $ELOG_CONSOLE | tr -d " ")
  436.     echo TAIL_PID=$TAIL_PID >> /iprocs
  437. }
  438. #======================================
  439. # udevPending
  440. #--------------------------------------
  441. function udevPending {
  442.     local timeout=30
  443.     if [ -x /sbin/udevadm ];then
  444.         /sbin/udevadm trigger
  445.         /sbin/udevadm settle --timeout=$timeout
  446.     else
  447.         /sbin/udevtrigger
  448.         /sbin/udevsettle --timeout=$timeout
  449.     fi
  450. }
  451. #======================================
  452. # udevSystemStart
  453. #--------------------------------------
  454. function udevSystemStart {
  455.     # /.../
  456.     # start udev while in pre-init phase. This means we can
  457.     # run udev from the standard runlevel script
  458.     # ----
  459.     /etc/init.d/boot.udev start
  460.     echo
  461. }
  462. #======================================
  463. # udevSystemStop
  464. #--------------------------------------
  465. function udevSystemStop {
  466.     # /.../
  467.     # stop udev while in pre-init phase.
  468.     # ----
  469.     /etc/init.d/boot.udev stop
  470.     echo
  471. }
  472. #======================================
  473. # udevStart
  474. #--------------------------------------
  475. function udevStart {
  476.     # /.../
  477.     # start the udev daemon.
  478.     # ----
  479.     echo "Creating device nodes with udev"
  480.     # disable hotplug helper, udevd listens to netlink
  481.     if [ -e /proc/sys/kernel/hotplug ];then
  482.         echo "" > /proc/sys/kernel/hotplug
  483.     fi
  484.     if ! ls /lib/modules/*/modules.order &>/dev/null;then
  485.         # /.../
  486.         # without modules.order in place we prevent udev from loading
  487.         # the storage modules because it does not make a propper
  488.         # choice if there are multiple possible modules available.
  489.         # Example:
  490.         # udev prefers ata_generic over ata_piix but the hwinfo
  491.         # order is ata_piix first which also seems to make more
  492.         # sense.
  493.         # -----
  494.         rm -f /etc/udev/rules.d/*-drivers.rules
  495.         rm -f /lib/udev/rules.d/*-drivers.rules
  496.         HAVE_MODULES_ORDER=0
  497.     fi
  498.     # nodes in a tmpfs
  499.     mount -t tmpfs -o mode=0755 udev /dev
  500.     # static nodes
  501.     createInitialDevices /dev
  502.     # terminal devices
  503.     mount -t devpts devpts /dev/pts
  504.     # start the udev daemon
  505.     udevd udev_log="debug" &
  506.     echo UDEVD_PID=$! >> /iprocs
  507.     # wait for pending triggered udev events.
  508.     udevPending
  509.     # start splashy if configured
  510.     startSplashy
  511. }
  512. #======================================
  513. # udevKill
  514. #--------------------------------------
  515. function udevKill {
  516.     . /iprocs ; kill $UDEVD_PID
  517. }
  518. #======================================
  519. # startSplashy
  520. #--------------------------------------
  521. function startSplashy {
  522.     if [ -x /usr/sbin/splashy ];then
  523.         splashy boot
  524.     fi
  525. }
  526. #======================================
  527. # startBlogD
  528. #--------------------------------------
  529. function startBlogD {
  530.     if test -n "$REDIRECT" ; then
  531.         mkdir -p /var/log
  532.         > /dev/shm/initrd.msg
  533.         ln -sf /dev/shm/initrd.msg /var/log/boot.msg
  534.         mkdir -p /var/run
  535.         /sbin/blogd $REDIRECT
  536.         BLOGD_PID=$(cat /var/run/blogd.pid)
  537.         echo BLOGD_PID=$BLOGD_PID >> /iprocs
  538.     fi
  539. }
  540. #======================================
  541. # killBlogD
  542. #--------------------------------------
  543. function killBlogD {
  544.     # /.../
  545.     # kill blogd on /dev/console
  546.     # ----
  547.     if test -n "$REDIRECT" ; then
  548.         local umountProc=0
  549.         if [ ! -e /proc/mounts ];then
  550.             mount -t proc proc /proc
  551.             umountProc=1
  552.         fi
  553.         Echo "Stopping boot logging"
  554.         . /iprocs ; kill $BLOGD_PID
  555.         if [ $umountProc -eq 1 ];then
  556.             umount /proc
  557.         fi
  558.     fi
  559. }
  560. #======================================
  561. # installBootLoader
  562. #--------------------------------------
  563. function installBootLoader {
  564.     # /.../
  565.     # generic function to install the boot loader.
  566.     # The selection of the bootloader happens according to
  567.     # the architecture of the system
  568.     # ----
  569.     local arch=`uname -m`
  570.     if [ -z "$loader" ];then
  571.         loader="grub"
  572.     fi
  573.     case $arch-$loader in
  574.         i*86-grub)       installBootLoaderGrub ;;
  575.         x86_64-grub)     installBootLoaderGrub ;;
  576.         ppc*)            installBootLoaderLilo ;;
  577.         i*86-syslinux)   installBootLoaderSyslinux ;;
  578.         x86_64-syslinux) installBootLoaderSyslinux ;;
  579.         i*86-extlinux)   installBootLoaderSyslinux ;;
  580.         x86_64-extlinux) installBootLoaderSyslinux ;;
  581.         s390-zipl)       installBootLoaderS390 ;;
  582.         s390x-zipl)      installBootLoaderS390 ;;
  583.         *)
  584.         systemException \
  585.             "*** boot loader install for $arch-$loader not implemented ***" \
  586.         "reboot"
  587.     esac
  588. }
  589. #======================================
  590. # installBootLoaderRecovery
  591. #--------------------------------------
  592. function installBootLoaderRecovery {
  593.     # /.../
  594.     # generic function to install the boot loader into
  595.     # the recovery partition. The selection of the bootloader
  596.     # happens according to the architecture of the system
  597.     # ----
  598.     local arch=`uname -m`
  599.     if [ -z "$loader" ];then
  600.         loader="grub"
  601.     fi
  602.     case $arch-$loader in
  603.         i*86-grub)       installBootLoaderGrubRecovery ;;
  604.         x86_64-grub)     installBootLoaderGrubRecovery ;;
  605.         i*86-syslinux)   installBootLoaderSyslinuxRecovery ;;
  606.         x86_64-syslinux) installBootLoaderSyslinuxRecovery ;;
  607.         i*86-extlinux)   installBootLoaderSyslinuxRecovery ;;
  608.         x86_64-extlinux) installBootLoaderSyslinuxRecovery ;;
  609.         s390-zipl)       installBootLoaderS390Recovery ;;
  610.         s390x-zipl)      installBootLoaderS390Recovery ;;
  611.         *)
  612.         systemException \
  613.             "*** boot loader setup for $arch-$loader not implemented ***" \
  614.         "reboot"
  615.     esac
  616. }
  617. #======================================
  618. # installBootLoaderS390
  619. #--------------------------------------
  620. function installBootLoaderS390 {
  621.     if [ -x /sbin/zipl ];then
  622.         Echo "Installing boot loader..."
  623.         zipl -c /etc/zipl.conf 1>&2
  624.         if [ ! $? = 0 ];then
  625.             Echo "Failed to install boot loader"
  626.         fi
  627.     else
  628.         Echo "Image doesn't have zipl installed"
  629.         Echo "Can't install boot loader"
  630.     fi
  631. }
  632. #======================================
  633. # installBootLoaderSyslinux
  634. #--------------------------------------
  635. function installBootLoaderSyslinux {
  636.     local syslmbr=/usr/share/syslinux/mbr.bin
  637.     if [ -e $syslmbr ];then
  638.         Echo "Installing boot loader..."
  639.         if [ $loader = "syslinux" ];then
  640.             syslinux $imageBootDevice
  641.         else
  642.             extlinux --install /boot/syslinux
  643.         fi
  644.         dd if=$syslmbr of=$imageDiskDevice bs=512 count=1 conv=notrunc
  645.     else
  646.         Echo "Image doesn't have syslinux (mbr.bin) installed"
  647.         Echo "Can't install boot loader"
  648.     fi
  649. }
  650. #======================================
  651. # installBootLoaderGrub
  652. #--------------------------------------
  653. function installBootLoaderGrub {
  654.     # /.../
  655.     # install the grub according to the contents of
  656.     # /etc/grub.conf and /boot/grub/menu.lst
  657.     # ----
  658.     if [ -x /usr/sbin/grub ];then
  659.         Echo "Installing boot loader..."
  660.         /usr/sbin/grub --batch --no-floppy < /etc/grub.conf 1>&2
  661.         if [ ! $? = 0 ];then
  662.             Echo "Failed to install boot loader"
  663.         fi
  664.     else
  665.         Echo "Image doesn't have grub installed"
  666.         Echo "Can't install boot loader"
  667.     fi
  668. }
  669. #======================================
  670. # installBootLoaderLilo
  671. #--------------------------------------
  672. function installBootLoaderLilo {
  673.     # /.../
  674.     # install the lilo according to the contents of
  675.     # /etc/lilo.conf
  676.     # ----
  677.     if [ -x /sbin/lilo ];then
  678.         Echo "Installing boot loader..."
  679.         /sbin/lilo 1>&2
  680.         if [ ! $? = 0 ];then
  681.             Echo "Failed to install boot loader"
  682.         fi
  683.     else
  684.         Echo "Image doesn't have lilo installed"
  685.         Echo "Can't install boot loader"
  686.     fi
  687. }
  688. #======================================
  689. # installBootLoaderS390Recovery
  690. #--------------------------------------
  691. function installBootLoaderS390Recovery {
  692.     systemException \
  693.         "*** zipl: recovery boot not implemented ***" \
  694.     "reboot"
  695. }
  696. #======================================
  697. # installBootLoaderSyslinuxRecovery
  698. #--------------------------------------
  699. function installBootLoaderSyslinuxRecovery {
  700.     local syslmbr=/usr/share/syslinux/mbr.bin
  701.     if [ -e $syslmbr ];then
  702.         if [ $loader = "syslinux" ];then
  703.             syslinux $imageRecoveryDevice
  704.         else
  705.             extlinux --install /reco-save/boot/syslinux
  706.         fi
  707.     else
  708.         Echo "Image doesn't have syslinux (mbr.bin) installed"
  709.         Echo "Can't install boot loader"
  710.     fi
  711. }
  712. #======================================
  713. # installBootLoaderGrubRecovery
  714. #--------------------------------------
  715. function installBootLoaderGrubRecovery {
  716.     # /.../
  717.     # install the grub into the recovery partition.
  718.     # By design the recovery partition is always the
  719.     # fourth primary partition of the disk
  720.     # ----
  721.     local input=/grub.input
  722.     local gdevreco=$(expr $recoid - 1)
  723.     echo "device (hd0) $imageDiskDevice" > $input
  724.     echo "root (hd0,$gdevreco)"  >> $input
  725.     echo "setup (hd0,$gdevreco)" >> $input
  726.     echo "quit"          >> $input
  727.     if [ -x /usr/sbin/grub ];then
  728.         /usr/sbin/grub --batch < $input 1>&2
  729.         rm -f $input
  730.     else
  731.         Echo "Image doesn't have grub installed"
  732.         Echo "Can't install boot loader"
  733.         systemException \
  734.             "recovery grub setup failed" \
  735.         "reboot"
  736.     fi
  737. }
  738. #======================================
  739. # setupSUSEInitrd
  740. #--------------------------------------
  741. function setupSUSEInitrd {
  742.     # /.../
  743.     # call mkinitrd on suse systems to create the distro initrd
  744.     # based on /etc/sysconfig/kernel
  745.     # ----
  746.     bootLoaderOK=1
  747.     local umountProc=0
  748.     local umountSys=0
  749.     local systemMap=0
  750.     local haveVMX=0
  751.     local params
  752.     local running
  753.     local rlinux
  754.     local rinitrd
  755.     for i in `find /boot/ -name "System.map*"`;do
  756.         systemMap=1
  757.     done
  758.     if [ $systemMap -eq 1 ];then
  759.         if [ ! -e /proc/mounts ];then
  760.             mount -t proc proc /proc
  761.             umountProc=1
  762.         fi
  763.         if [ ! -e /sys/block ];then
  764.             mount -t sysfs sysfs /sys
  765.             umountSys=1
  766.         fi
  767.         if [ -f /etc/init.d/boot.device-mapper ];then
  768.             /etc/init.d/boot.device-mapper start
  769.         fi
  770.         if grep -qi param_B /sbin/mkinitrd;then
  771.             params="-B"
  772.         fi
  773.         if [ $bootLoaderOK = "1" ];then
  774.             if [ -f /boot/initrd.vmx ];then
  775.                 rm -f /boot/initrd.vmx
  776.                 rm -f /boot/linux.vmx
  777.                 haveVMX=1
  778.             fi
  779.         fi
  780.         if ! mkinitrd $params;then
  781.             Echo "Can't create initrd"
  782.             systemIntegrity=unknown
  783.             bootLoaderOK=0
  784.         fi
  785.         if [ -f /etc/init.d/boot.device-mapper ];then
  786.             /etc/init.d/boot.device-mapper stop
  787.         fi
  788.         if [ $bootLoaderOK = "1" ] && [ $haveVMX = "1" ];then
  789.             running=$(uname -r)
  790.             rlinux=vmlinuz-$running
  791.             rinitrd=initrd-$running
  792.             ln -s $rlinux  /boot/linux.vmx
  793.             ln -s $rinitrd /boot/initrd.vmx
  794.         fi
  795.         if [ $umountSys -eq 1 ];then
  796.             umount /sys
  797.         fi
  798.         if [ $umountProc -eq 1 ];then
  799.             umount /proc
  800.         fi
  801.     else
  802.         Echo "Image doesn't include kernel system map"
  803.         Echo "Can't create initrd"
  804.         systemIntegrity=unknown
  805.         bootLoaderOK=0
  806.     fi
  807. }
  808. #======================================
  809. # callSUSEInitrdScripts
  810. #--------------------------------------
  811. function callSUSEInitrdScripts {
  812.     # /.../
  813.     # create initrd with mkinitrd and extract the run_all.sh script
  814.     # after that call the script in /lib/mkinitrd. The mkinitrd
  815.     # package must be installed in the system image to do that
  816.     # ----
  817.     local prefix=$1
  818.     if [ ! -d $prefix/lib/mkinitrd ];then
  819.         Echo "No mkinitrd package installed"
  820.         Echo "Can't call initrd scripts"
  821.         return
  822.     fi
  823.     mkinitrd >/dev/null
  824.     if [ ! -f $prefix/boot/initrd ];then
  825.         Echo "No initrd file found"
  826.         Echo "Can't call initrd scripts"
  827.         return
  828.     fi
  829.     mkdir $prefix/tmp/suse
  830.     cd $prefix/tmp/suse && gzip -cd $prefix/boot/initrd | cpio -i
  831.     if [ ! -f $prefix/tmp/suse/run_all.sh ];then
  832.         Echo "No run_all.sh script in initrd"
  833.         Echo "Can't call initrd scripts"
  834.         return
  835.     fi
  836.     Echo "Calling SUSE initrd scripts"
  837.     chroot . bash ./run_all.sh
  838. }
  839. #======================================
  840. # setupBootLoader
  841. #--------------------------------------
  842. function setupBootLoader {
  843.     # /.../
  844.     # generic function to setup the boot loader configuration.
  845.     # The selection of the bootloader happens according to
  846.     # the architecture of the system
  847.     # ----
  848.     local arch=`uname -m`
  849.     local para=""
  850.     while [ $# -gt 0 ];do
  851.         para="$para \"$1\""
  852.         shift
  853.     done
  854.     if [ -z "$loader" ];then
  855.         loader="grub"
  856.     fi
  857.     case $arch-$loader in
  858.         i*86-grub)       eval setupBootLoaderGrub $para ;;
  859.         x86_64-grub)     eval setupBootLoaderGrub $para ;;
  860.         i*86-syslinux)   eval setupBootLoaderSyslinux $para ;;
  861.         x86_64-syslinux) eval setupBootLoaderSyslinux $para ;;
  862.         i*86-extlinux)   eval setupBootLoaderSyslinux $para ;;
  863.         x86_64-extlinux) eval setupBootLoaderSyslinux $para ;;
  864.         s390-zipl)       eval setupBootLoaderS390 $para ;;
  865.         s390x-zipl)      eval setupBootLoaderS390 $para ;;
  866.         ppc*)            eval setupBootLoaderLilo $para ;;
  867.         *)
  868.         systemException \
  869.             "*** boot loader setup for $arch-$loader not implemented ***" \
  870.         "reboot"
  871.     esac
  872. }
  873. #======================================
  874. # setupBootLoaderRecovery
  875. #--------------------------------------
  876. function setupBootLoaderRecovery {
  877.     # /.../
  878.     # generic function to setup the boot loader configuration
  879.     # for the recovery partition. The selection of the bootloader
  880.     # happens according to the architecture of the system
  881.     # ----
  882.     local arch=`uname -m`
  883.     local para=""
  884.     while [ $# -gt 0 ];do
  885.         para="$para \"$1\""
  886.         shift
  887.     done
  888.     if [ -z "$loader" ];then
  889.         loader="grub"
  890.     fi
  891.     case $arch-$loader in
  892.         i*86-grub)       eval setupBootLoaderGrubRecovery $para ;;
  893.         x86_64-grub)     eval setupBootLoaderGrubRecovery $para ;;
  894.         i*86-syslinux)   eval setupBootLoaderSyslinuxRecovery $para ;;
  895.         x86_64-syslinux) eval setupBootLoaderSyslinuxRecovery $para ;;
  896.         i*86-extlinux)   eval setupBootLoaderSyslinuxRecovery $para ;;
  897.         x86_64-extlinux) eval setupBootLoaderSyslinuxRecovery $para ;;
  898.         s390-zipl)       eval setupBootLoaderS390Recovery $para ;;
  899.         s390x-zipl)      eval setupBootLoaderS390Recovery $para ;;
  900.         *)
  901.         systemException \
  902.             "*** boot loader setup for $arch-$loader not implemented ***" \
  903.         "reboot"
  904.     esac
  905. }
  906. #======================================
  907. # setupBootLoaderS390Recovery
  908. #--------------------------------------
  909. function setupBootLoaderS390Recovery {
  910.     systemException \
  911.         "*** zipl: recovery boot not implemented ***" \
  912.     "reboot"
  913. }
  914. #======================================
  915. # setupBootLoaderSyslinuxRecovery
  916. #--------------------------------------
  917. function setupBootLoaderSyslinuxRecovery {
  918.     # /.../
  919.     # create syslinux configuration for the recovery boot system
  920.     # ----
  921.     local mountPrefix=$1  # mount path of the image
  922.     local destsPrefix=$2  # base dir for the config files
  923.     local gnum=$3         # boot partition ID
  924.     local rdev=$4         # root partition
  925.     local gfix=$5         # syslinux title postfix
  926.     local swap=$6         # optional swap partition
  927.     local conf=$destsPrefix/boot/syslinux/syslinux.cfg
  928.     local kernel=""
  929.     local initrd=""
  930.     local fbmode=$vga
  931.     if [ -z "$fbmode" ];then
  932.         fbmode=$DEFAULT_VGA
  933.     fi
  934.     #======================================
  935.     # import syslinux into recovery
  936.     #--------------------------------------
  937.     cp -a $mountPrefix/boot/syslinux $destsPrefix/boot
  938.     #======================================
  939.     # setup config file name
  940.     #--------------------------------------
  941.     if [ $loader = "extlinux" ];then
  942.         conf=$destsPrefix/boot/syslinux/extlinux.conf
  943.     fi
  944.     #======================================
  945.     # create syslinux.cfg file
  946.     #--------------------------------------
  947.     echo "implicit 1"                   > $conf
  948.     echo "prompt   1"                  >> $conf
  949.     echo "TIMEOUT $KIWI_BOOT_TIMEOUT"  >> $conf
  950.     if \
  951.         [ -f "$destsPrefix/boot/syslinux/gfxboot.com" ] || \
  952.         [ -f "$destsPrefix/boot/syslinux/gfxboot.c32" ]
  953.     then
  954.         echo "ui gfxboot bootlogo isolinux.msg" >> $conf
  955.     else
  956.         echo "gfxboot  bootlogo"                >> $conf
  957.         echo "display  isolinux.msg"            >> $conf
  958.     fi
  959.     kernel=linux.vmx   # this is a copy of the kiwi linux.vmx file
  960.     initrd=initrd.vmx  # this is a copy of the kiwi initrd.vmx file
  961.     #======================================
  962.     # create recovery entry
  963.     #--------------------------------------
  964.     if [ ! -z "$OEM_RECOVERY" ];then
  965.         #======================================
  966.         # Reboot
  967.         #--------------------------------------
  968.         title=$(makeLabel "Cancel/Reboot")
  969.         echo "DEFAULT $title"                              >> $conf
  970.         echo "label $title"                                >> $conf
  971.         echo " localboot 0x80"                             >> $conf
  972.         #======================================
  973.         # Recovery
  974.         #--------------------------------------
  975.         title=$(makeLabel "Recover/Repair System")
  976.         echo "label $title"                                >> $conf
  977.         if xenServer $kernel $mountPrefix;then
  978.             systemException \
  979.                 "*** $loader: Xen dom0 boot not implemented ***" \
  980.             "reboot"
  981.         else
  982.             echo "kernel /boot/$kernel"                    >> $conf
  983.             echo -n "append initrd=/boot/$initrd"          >> $conf
  984.             echo -n " root=$diskByID"                      >> $conf
  985.             if [ ! -z "$imageDiskDevice" ];then
  986.                 echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
  987.             fi
  988.             echo -n " vga=$fbmode loader=$loader"          >> $conf
  989.             echo -n " splash=silent"                       >> $conf
  990.             echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
  991.             echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
  992.             if [ "$haveLVM" = "yes" ];then
  993.                 echo -n " VGROUP=$VGROUP"                  >> $conf
  994.             fi
  995.             echo " KIWI_RECOVERY=$recoid"                  >> $conf
  996.             echo " showopts"                               >> $conf
  997.         fi
  998.         #======================================
  999.         # Restore
  1000.         #--------------------------------------
  1001.         title=$(makeLabel "Restore Factory System")
  1002.         echo "label $title"                                >> $conf
  1003.         if xenServer $kernel $mountPrefix;then
  1004.             systemException \
  1005.                 "*** $loader: Xen dom0 boot not implemented ***" \
  1006.             "reboot"
  1007.         else
  1008.             echo "kernel /boot/$kernel"                    >> $conf
  1009.             echo -n "append initrd=/boot/$initrd"          >> $conf
  1010.             echo -n " root=$diskByID"                      >> $conf
  1011.             if [ ! -z "$imageDiskDevice" ];then
  1012.                 echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
  1013.             fi
  1014.             echo -n " vga=$fbmode loader=$loader"          >> $conf
  1015.             echo -n " splash=silent"                       >> $conf
  1016.             echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
  1017.             echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
  1018.             if [ "$haveLVM" = "yes" ];then
  1019.                 echo -n " VGROUP=$VGROUP"                  >> $conf
  1020.             fi
  1021.             echo " KIWI_RECOVERY=$recoid RESTORE=1"        >> $conf
  1022.             echo " showopts"                               >> $conf
  1023.         fi
  1024.     fi
  1025. }
  1026. #======================================
  1027. # setupBootLoaderGrubRecovery
  1028. #--------------------------------------
  1029. function setupBootLoaderGrubRecovery {
  1030.     # /.../
  1031.     # create menu.lst file for the recovery boot system
  1032.     # ----
  1033.     local mountPrefix=$1  # mount path of the image
  1034.     local destsPrefix=$2  # base dir for the config files
  1035.     local gfix=$3         # grub title
  1036.     local menu=$destsPrefix/boot/grub/menu.lst
  1037.     local kernel=""
  1038.     local initrd=""
  1039.     local fbmode=$vga
  1040.     local gdevreco=$(expr $recoid - 1)
  1041.     if [ -z "$fbmode" ];then
  1042.         fbmode=$DEFAULT_VGA
  1043.     fi
  1044.     gdev_recovery="(hd0,$gdevreco)"
  1045.     rdev_recovery=$OEM_RECOVERY
  1046.     diskByID=`getDiskID $rdev_recovery`
  1047.     #======================================
  1048.     # import grub stages into recovery
  1049.     #--------------------------------------
  1050.     mkdir -p $destsPrefix/boot/grub
  1051.     cp $mountPrefix/boot/grub/stage1 $destsPrefix/boot/grub
  1052.     cp $mountPrefix/boot/grub/stage2 $destsPrefix/boot/grub
  1053.     #======================================
  1054.     # create recovery menu.lst
  1055.     #--------------------------------------
  1056.     echo "timeout 30" > $menu
  1057.     echo "gfxmenu $gdev_recovery/boot/message" >> $menu
  1058.     kernel=vmlinuz # this is a copy of the kiwi linux.vmx file
  1059.     initrd=initrd  # this is a copy of the kiwi initrd.vmx file
  1060.     #======================================
  1061.     # create recovery entry
  1062.     #--------------------------------------
  1063.     if [ ! -z "$OEM_RECOVERY" ];then
  1064.         #======================================
  1065.         # Make the cancel option default
  1066.         #--------------------------------------
  1067.         echo "default 0"                                  >> $menu
  1068.         #======================================
  1069.         # Reboot
  1070.         #--------------------------------------
  1071.         title=$(makeLabel "Cancel/Reboot")
  1072.         echo "title $title"                               >> $menu
  1073.         echo " reboot"                                    >> $menu
  1074.         #======================================
  1075.         # Recovery
  1076.         #--------------------------------------
  1077.         title=$(makeLabel "Recover/Repair System")
  1078.         echo "title $title"                               >> $menu
  1079.         if xenServer $kernel $mountPrefix;then
  1080.             echo " root $gdev_recovery"                   >> $menu
  1081.             echo " kernel /boot/xen.gz"                   >> $menu
  1082.             echo -n " module /boot/$kernel"               >> $menu
  1083.             echo -n " root=$diskByID $console"            >> $menu
  1084.             if [ ! -z "$imageDiskDevice" ];then
  1085.                 echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
  1086.             fi
  1087.             echo -n " vga=$fbmode splash=silent"          >> $menu
  1088.             echo -n " $KIWI_INITRD_PARAMS"                >> $menu
  1089.             echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
  1090.             if [ "$haveLVM" = "yes" ];then
  1091.                 echo -n " VGROUP=$VGROUP"                 >> $menu
  1092.             fi
  1093.             echo " KIWI_RECOVERY=$recoid showopts"        >> $menu
  1094.             echo " module /boot/$initrd"                  >> $menu
  1095.         else
  1096.             echo -n " kernel $gdev_recovery/boot/$kernel" >> $menu
  1097.             echo -n " root=$diskByID $console"            >> $menu
  1098.             if [ ! -z "$imageDiskDevice" ];then
  1099.                 echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
  1100.             fi
  1101.             echo -n " vga=$fbmode splash=silent"          >> $menu
  1102.             echo -n " $KIWI_INITRD_PARAMS"                >> $menu
  1103.             echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
  1104.             if [ "$haveLVM" = "yes" ];then
  1105.                 echo -n " VGROUP=$VGROUP"                 >> $menu
  1106.             fi
  1107.             echo " KIWI_RECOVERY=$recoid showopts"        >> $menu
  1108.             echo " initrd $gdev_recovery/boot/$initrd"    >> $menu
  1109.         fi
  1110.         #======================================
  1111.         # Restore
  1112.         #--------------------------------------
  1113.         title=$(makeLabel "Restore Factory System")
  1114.         echo "title $title"                               >> $menu
  1115.         if xenServer $kernel $mountPrefix;then
  1116.             echo " root $gdev_recovery"                   >> $menu
  1117.             echo " kernel /boot/xen.gz"                   >> $menu
  1118.             echo -n " module /boot/$kernel"               >> $menu
  1119.             echo -n " root=$diskByID $console"            >> $menu
  1120.             if [ ! -z "$imageDiskDevice" ];then
  1121.                 echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
  1122.             fi
  1123.             echo -n " vga=$fbmode splash=silent"          >> $menu
  1124.             echo -n " $KIWI_INITRD_PARAMS"                >> $menu
  1125.             echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
  1126.             if [ "$haveLVM" = "yes" ];then
  1127.                 echo -n " VGROUP=$VGROUP"                 >> $menu
  1128.             fi
  1129.             echo " KIWI_RECOVERY=$recoid showopts"        >> $menu
  1130.             echo " module /boot/$initrd"                  >> $menu
  1131.         else
  1132.             echo -n " kernel $gdev_recovery/boot/$kernel" >> $menu
  1133.             echo -n " root=$diskByID $console"            >> $menu
  1134.             if [ ! -z "$imageDiskDevice" ];then
  1135.                 echo -n " disk=$(getDiskID $imageDiskDevice)" >> $menu
  1136.             fi
  1137.             echo -n " vga=$fbmode splash=silent"          >> $menu
  1138.             echo -n " $KIWI_INITRD_PARAMS"                >> $menu
  1139.             echo -n " $KIWI_KERNEL_OPTIONS"               >> $menu
  1140.             if [ "$haveLVM" = "yes" ];then
  1141.                 echo -n " VGROUP=$VGROUP"                 >> $menu
  1142.             fi
  1143.             echo -n " KIWI_RECOVERY=$recoid RESTORE=1"    >> $menu
  1144.             echo " showopts"                              >> $menu
  1145.             echo " initrd $gdev_recovery/boot/$initrd"    >> $menu
  1146.         fi
  1147.     fi
  1148. }
  1149. #======================================
  1150. # setupBootLoaderS390
  1151. #--------------------------------------
  1152. function setupBootLoaderS390 {
  1153.     # /.../
  1154.     # create /etc/zipl.conf used for the
  1155.     # zipl bootloader
  1156.     # ----
  1157.     local mountPrefix=$1  # mount path of the image
  1158.     local destsPrefix=$2  # base dir for the config files
  1159.     local znum=$3         # boot partition ID
  1160.     local rdev=$4         # root partition
  1161.     local zfix=$5         # zipl title postfix
  1162.     local swap=$6         # optional swap partition
  1163.     local conf=$destsPrefix/etc/zipl.conf
  1164.     local sysb=$destsPrefix/etc/sysconfig/bootloader
  1165.     local kname=""
  1166.     local kernel=""
  1167.     local initrd=""
  1168.     local title=""
  1169.     #======================================
  1170.     # check for device by ID
  1171.     #--------------------------------------
  1172.     local diskByID=`getDiskID $rdev`
  1173.     local swapByID=`getDiskID $swap`
  1174.     #======================================
  1175.     # check for boot image .profile
  1176.     #--------------------------------------
  1177.     if [ -f /.profile ];then
  1178.         importFile < /.profile
  1179.     fi
  1180.     #======================================
  1181.     # check for bootloader displayname
  1182.     #--------------------------------------
  1183.     if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
  1184.         kiwi_oemtitle=$kiwi_displayname
  1185.     fi
  1186.     #======================================
  1187.     # check for system image .profile
  1188.     #--------------------------------------
  1189.     if [ -f $mountPrefix/image/.profile ];then
  1190.         importFile < $mountPrefix/image/.profile
  1191.     fi
  1192.     #======================================
  1193.     # check for kernel options
  1194.     #--------------------------------------
  1195.     if [ ! -z "$kiwi_cmdline" ];then
  1196.         KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
  1197.     fi
  1198.     #======================================
  1199.     # check for syslinux title postfix
  1200.     #--------------------------------------
  1201.     if [ -z "$zfix" ];then
  1202.         zfix="unknown"
  1203.     fi
  1204.     #======================================
  1205.     # check for boot TIMEOUT
  1206.     #--------------------------------------
  1207.     if [ -z "$KIWI_BOOT_TIMEOUT" ];then
  1208.         KIWI_BOOT_TIMEOUT=100;
  1209.     fi
  1210.     #======================================
  1211.     # create directory structure
  1212.     #--------------------------------------
  1213.     for dir in $conf $sysb;do
  1214.         dir=`dirname $dir`; mkdir -p $dir
  1215.     done
  1216.     #======================================
  1217.     # create zipl.conf file
  1218.     #--------------------------------------
  1219.     local count
  1220.     local title_default
  1221.     local title_failsafe
  1222.     echo "[defaultboot]"                      > $conf
  1223.     echo "defaultmenu = menu"                >> $conf
  1224.     echo ":menu"                             >> $conf
  1225.     echo "    default = 1"                   >> $conf
  1226.     echo "    prompt = 1"                    >> $conf
  1227.     echo "    target = /boot/zipl"           >> $conf
  1228.     echo "    timeout = $KIWI_BOOT_TIMEOUT"  >> $conf
  1229.     count=1
  1230.     IFS="," ; for i in $KERNEL_LIST;do
  1231.         if test -z "$i";then
  1232.             continue
  1233.         fi
  1234.         kname=${KERNEL_NAME[$count]}
  1235.         if ! echo $zfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
  1236.             if [ "$count" = "1" ];then
  1237.                 title_default=$(makeLabel "$zfix")
  1238.             else
  1239.                 title_default=$(makeLabel "$kname ( $zfix )")
  1240.             fi
  1241.         elif [ -z "$kiwi_oemtitle" ];then
  1242.             title_default=$(makeLabel "$kname ( $zfix )")
  1243.         else
  1244.             if [ "$count" = "1" ];then
  1245.                 title_default=$(makeLabel "$kiwi_oemtitle ( $zfix )")
  1246.             else
  1247.                 title_default=$(makeLabel "$kiwi_oemtitle-$kname ( $zfix )")
  1248.             fi
  1249.         fi
  1250.         title_failsafe=$(makeLabel "Failsafe -- $title_default")
  1251.         echo "    $count = $title_default"  >> $conf
  1252.         count=`expr $count + 1`
  1253.         echo "    $count = $title_failsafe" >> $conf
  1254.         count=`expr $count + 1`
  1255.     done
  1256.     count=1
  1257.     IFS="," ; for i in $KERNEL_LIST;do
  1258.         if test -z "$i";then
  1259.             continue
  1260.         fi
  1261.         kernel=`echo $i | cut -f1 -d:`
  1262.         initrd=`echo $i | cut -f2 -d:`
  1263.         kname=${KERNEL_NAME[$count]}
  1264.         if ! echo $zfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
  1265.             if [ "$count" = "1" ];then
  1266.                 title_default=$(makeLabel "$zfix")
  1267.             else
  1268.                 title_default=$(makeLabel "$kname ( $zfix )")
  1269.             fi
  1270.         elif [ -z "$kiwi_oemtitle" ];then
  1271.             title_default=$(makeLabel "$kname ( $zfix )")
  1272.         else
  1273.             if [ "$count" = "1" ];then
  1274.                 title_default=$(makeLabel "$kiwi_oemtitle ( $zfix )")
  1275.             else
  1276.                 title_default=$(makeLabel "$kiwi_oemtitle-$kname ( $zfix )")
  1277.             fi
  1278.         fi
  1279.         title_failsafe=$(makeLabel "Failsafe -- $title_default")
  1280.         #======================================
  1281.         # create standard entry
  1282.         #--------------------------------------
  1283.         echo "[$title_default]"                  >> $conf
  1284.         echo "target  = /boot/zipl"              >> $conf
  1285.         echo "image   = /boot/$kernel"           >> $conf
  1286.         echo "ramdisk = /boot/$initrd,0x2000000" >> $conf
  1287.         echo -n "parameters = \"root=$diskByID"  >> $conf
  1288.         if [ ! -z "$imageDiskDevice" ];then
  1289.             echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
  1290.         fi
  1291.         if [ ! -z "$swap" ];then
  1292.             echo -n " resume=$swapByID" >> $conf
  1293.         fi
  1294.         if [ "$haveLVM" = "yes" ];then
  1295.             echo -n " VGROUP=$VGROUP" >> $conf
  1296.         fi
  1297.         echo -n " $KIWI_INITRD_PARAMS"  >> $conf
  1298.         echo -n " $KIWI_KERNEL_OPTIONS" >> $conf
  1299.         echo " loader=$loader\""        >> $conf
  1300.         #======================================
  1301.         # create failsafe entry
  1302.         #--------------------------------------
  1303.         echo "[$title_failsafe]"                 >> $conf
  1304.         echo "target  = /boot/zipl"              >> $conf
  1305.         echo "image   = /boot/$kernel"           >> $conf
  1306.         echo "ramdisk = /boot/$initrd,0x2000000" >> $conf
  1307.         echo -n "parameters = \"root=$diskByID"  >> $conf
  1308.         if [ ! -z "$imageDiskDevice" ];then
  1309.             echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
  1310.         fi
  1311.         if [ "$haveLVM" = "yes" ];then
  1312.             echo -n " VGROUP=$VGROUP" >> $conf
  1313.         fi
  1314.         echo -n " $KIWI_INITRD_PARAMS"       >> $conf
  1315.         echo -n " $KIWI_KERNEL_OPTIONS"      >> $conf
  1316.         echo " loader=$loader x11failsafe\"" >> $conf
  1317.         count=`expr $count + 1`
  1318.     done
  1319.     #======================================
  1320.     # create recovery entry
  1321.     #--------------------------------------
  1322.     if [ ! -z "$OEM_RECOVERY" ];then
  1323.         systemException \
  1324.             "*** zipl: recovery chain loading not implemented ***" \
  1325.         "reboot"
  1326.     fi
  1327.     #======================================
  1328.     # create sysconfig/bootloader
  1329.     #--------------------------------------
  1330.     echo "LOADER_TYPE=\"$loader\""                            > $sysb
  1331.     echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
  1332.     echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
  1333.     if [ ! -z "$swap" ];then
  1334.         echo -n " resume=$swapByID"                          >> $sysb
  1335.     fi
  1336.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  1337.     echo " showopts\""                                       >> $sysb
  1338.     echo -n "FAILSAFE_APPEND=\"root=$diskByID"               >> $sysb
  1339.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  1340.     echo -n " x11failsafe noresume\""                        >> $sysb
  1341. }
  1342. #======================================
  1343. # setupBootLoaderSyslinux
  1344. #--------------------------------------
  1345. function setupBootLoaderSyslinux {
  1346.     # /.../
  1347.     # create syslinux.cfg used for the
  1348.     # syslinux bootloader
  1349.     # ----
  1350.     local mountPrefix=$1  # mount path of the image
  1351.     local destsPrefix=$2  # base dir for the config files
  1352.     local gnum=$3         # boot partition ID
  1353.     local rdev=$4         # root partition
  1354.     local gfix=$5         # syslinux title postfix
  1355.     local swap=$6         # optional swap partition
  1356.     local conf=$destsPrefix/boot/syslinux/syslinux.cfg
  1357.     local sysb=$destsPrefix/etc/sysconfig/bootloader
  1358.     local console=""
  1359.     local kname=""
  1360.     local kernel=""
  1361.     local initrd=""
  1362.     local title=""
  1363.     local fbmode=$vga
  1364.     local xencons=$xencons
  1365.     if [ ! -z "$OEM_RECOVERY" ];then
  1366.         local gdevreco=$recoid
  1367.     fi
  1368.     if [ -z "$fbmode" ];then
  1369.         fbmode=$DEFAULT_VGA
  1370.     fi
  1371.     #======================================
  1372.     # check for device by ID
  1373.     #--------------------------------------
  1374.     local diskByID=`getDiskID $rdev`
  1375.     local swapByID=`getDiskID $swap`
  1376.     #======================================
  1377.     # check for boot image .profile
  1378.     #--------------------------------------
  1379.     if [ -f /.profile ];then
  1380.         importFile < /.profile
  1381.     fi
  1382.     #======================================
  1383.     # check for bootloader displayname
  1384.     #--------------------------------------
  1385.     if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
  1386.         kiwi_oemtitle=$kiwi_displayname
  1387.     fi
  1388.     #======================================
  1389.     # check for system image .profile
  1390.     #--------------------------------------
  1391.     if [ -f $mountPrefix/image/.profile ];then
  1392.         importFile < $mountPrefix/image/.profile
  1393.     fi
  1394.     #======================================
  1395.     # check for kernel options
  1396.     #--------------------------------------
  1397.     if [ ! -z "$kiwi_cmdline" ];then
  1398.         KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
  1399.     fi
  1400.     #======================================
  1401.     # setup config file name
  1402.     #--------------------------------------
  1403.     if [ $loader = "extlinux" ];then
  1404.         conf=$destsPrefix/boot/syslinux/extlinux.conf
  1405.     fi
  1406.     #======================================
  1407.     # check for syslinux title postfix
  1408.     #--------------------------------------
  1409.     if [ -z "$gfix" ];then
  1410.         gfix="unknown"
  1411.     fi
  1412.     #======================================
  1413.     # check for boot TIMEOUT
  1414.     #--------------------------------------
  1415.     if [ -z "$KIWI_BOOT_TIMEOUT" ];then
  1416.         KIWI_BOOT_TIMEOUT=100;
  1417.     fi
  1418.     #======================================
  1419.     # create directory structure
  1420.     #--------------------------------------
  1421.     for dir in $conf $sysb;do
  1422.         dir=`dirname $dir`; mkdir -p $dir
  1423.     done
  1424.     #======================================
  1425.     # create syslinux.cfg file
  1426.     #--------------------------------------
  1427.     echo "implicit 1"                   > $conf
  1428.     echo "prompt   1"                  >> $conf
  1429.     echo "TIMEOUT $KIWI_BOOT_TIMEOUT"  >> $conf
  1430.     if \
  1431.         [ -f "$destsPrefix/boot/syslinux/gfxboot.com" ] || \
  1432.         [ -f "$destsPrefix/boot/syslinux/gfxboot.c32" ]
  1433.     then
  1434.         echo "ui gfxboot bootlogo isolinux.msg" >> $conf
  1435.     else
  1436.         echo "gfxboot  bootlogo"                >> $conf
  1437.         echo "display  isolinux.msg"            >> $conf
  1438.     fi
  1439.     local count=1
  1440.     IFS="," ; for i in $KERNEL_LIST;do
  1441.         if test ! -z "$i";then
  1442.             #======================================
  1443.             # setup syslinux requirements
  1444.             #--------------------------------------
  1445.             kernel=`echo $i | cut -f1 -d:`
  1446.             initrd=`echo $i | cut -f2 -d:`
  1447.             kname=${KERNEL_NAME[$count]}
  1448.             #======================================
  1449.             # move to FAT requirements 8+3
  1450.             #--------------------------------------
  1451.             if [ "$loader" = "syslinux" ];then
  1452.                 kernel="linux.$count"
  1453.                 initrd="initrd.$count"
  1454.             fi
  1455.             if ! echo $gfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
  1456.                 if [ "$count" = "1" ];then
  1457.                     title=$(makeLabel "$gfix")
  1458.                 else
  1459.                     title=$(makeLabel "$kname [ $gfix ]")
  1460.                 fi
  1461.             elif [ -z "$kiwi_oemtitle" ];then
  1462.                 title=$(makeLabel "$kname [ $gfix ]")
  1463.             else
  1464.                 if [ "$count" = "1" ];then
  1465.                     title=$(makeLabel "$kiwi_oemtitle [ $gfix ]")
  1466.                 else
  1467.                     title=$(makeLabel "$kiwi_oemtitle-$kname [ $gfix ]")
  1468.                 fi
  1469.             fi
  1470.             #======================================
  1471.             # create standard entry
  1472.             #--------------------------------------
  1473.             echo "DEFAULT $title"                              >> $conf
  1474.             echo "label $title"                                >> $conf
  1475.             if xenServer $kernel $mountPrefix;then
  1476.                 systemException \
  1477.                     "*** $loader: Xen dom0 boot not implemented ***" \
  1478.                 "reboot"
  1479.             else
  1480.                 echo "kernel /boot/$kernel"                    >> $conf
  1481.                 echo -n "append initrd=/boot/$initrd"          >> $conf
  1482.                 echo -n " root=$diskByID $console"             >> $conf
  1483.                 if [ ! -z "$imageDiskDevice" ];then
  1484.                     echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
  1485.                 fi
  1486.                 echo -n " vga=$fbmode loader=$loader"          >> $conf
  1487.                 echo -n " splash=silent"                       >> $conf
  1488.                 if [ ! -z "$swap" ];then
  1489.                     echo -n " resume=$swapByID"                >> $conf
  1490.                 fi
  1491.                 if [ ! -z "$xencons" ]; then
  1492.                     echo -n " xencons=$xencons"                >> $conf
  1493.                 fi
  1494.                 echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
  1495.                 echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
  1496.                 if [ "$haveLVM" = "yes" ];then
  1497.                     echo -n " VGROUP=$VGROUP"                  >> $conf
  1498.                 fi
  1499.                 echo " showopts"                               >> $conf
  1500.             fi
  1501.             #======================================
  1502.             # create Failsafe entry
  1503.             #--------------------------------------
  1504.             title=$(makeLabel "Failsafe -- $title")
  1505.             echo "label $title"                                >> $conf
  1506.             if xenServer $kernel $mountPrefix;then
  1507.                 systemException \
  1508.                     "*** $loader: Xen dom0 boot not implemented ***" \
  1509.                 "reboot"
  1510.             else
  1511.                 echo "kernel /boot/$kernel"                    >> $conf
  1512.                 echo -n "append initrd=/boot/$initrd"          >> $conf
  1513.                 echo -n " root=$diskByID $console"             >> $conf
  1514.                 if [ ! -z "$imageDiskDevice" ];then
  1515.                     echo -n " disk=$(getDiskID $imageDiskDevice)"  >> $conf
  1516.                 fi
  1517.                 echo -n " vga=$fbmode loader=$loader"          >> $conf
  1518.                 echo -n " splash=silent"                       >> $conf
  1519.                 if [ ! -z "$swap" ];then
  1520.                     echo -n " resume=$swapByID"                >> $conf
  1521.                 fi
  1522.                 if [ ! -z "$xencons" ]; then
  1523.                     echo -n " xencons=$xencons"                >> $conf
  1524.                 fi
  1525.                 echo -n " $KIWI_INITRD_PARAMS"                 >> $conf
  1526.                 echo -n " $KIWI_KERNEL_OPTIONS"                >> $conf
  1527.                 if [ "$haveLVM" = "yes" ];then
  1528.                     echo -n " VGROUP=$VGROUP"                  >> $conf
  1529.                 fi
  1530.                 echo -n " showopts ide=nodma apm=off acpi=off" >> $conf
  1531.                 echo -n " noresume selinux=0 nosmp"            >> $conf
  1532.                 echo " noapic maxcpus=0 edd=off"               >> $conf
  1533.             fi
  1534.             count=`expr $count + 1`
  1535.         fi
  1536.     done
  1537.     #======================================
  1538.     # create recovery entry
  1539.     #--------------------------------------
  1540.     if [ ! -z "$OEM_RECOVERY" ];then
  1541.         echo "label Recovery"                             >> $conf
  1542.         echo "kernel chain"                               >> $conf
  1543.         echo "append hd0 $gdevreco"                       >> $conf
  1544.     fi
  1545.     #======================================
  1546.     # create sysconfig/bootloader
  1547.     #--------------------------------------
  1548.     echo "LOADER_TYPE=\"$loader\""                           > $sysb
  1549.     echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
  1550.     echo "DEFAULT_VGA=\"$fbmode\""                           >> $sysb
  1551.     echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
  1552.     if [ ! -z "$swap" ];then
  1553.         echo -n " resume=$swapByID"                          >> $sysb
  1554.     fi
  1555.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  1556.     echo " showopts\""                                       >> $sysb
  1557.     echo "FAILSAFE_VGA=\"$fbmode\""                          >> $sysb
  1558.     echo -n "FAILSAFE_APPEND=\"root=$diskByID splash=silent" >> $sysb
  1559.     if [ ! -z "$swap" ];then
  1560.         echo -n " resume=$swapByID"                          >> $sysb
  1561.     fi
  1562.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  1563.     echo -n " showopts ide=nodma apm=off acpi=off noresume"  >> $sysb
  1564.     echo "selinux=0 nosmp noapic maxcpus=0 edd=off\""        >> $sysb
  1565. }
  1566. #======================================
  1567. # setupBootLoaderGrub
  1568. #--------------------------------------
  1569. function setupBootLoaderGrub {
  1570.     # /.../
  1571.     # create grub.conf and menu.lst file used for
  1572.     # installing the bootloader
  1573.     # ----
  1574.     local mountPrefix=$1  # mount path of the image
  1575.     local destsPrefix=$2  # base dir for the config files
  1576.     local gnum=$3         # grub boot partition ID
  1577.     local rdev=$4         # grub root partition
  1578.     local gfix=$5         # grub title postfix
  1579.     local swap=$6         # optional swap partition
  1580.     local menu=$destsPrefix/boot/grub/menu.lst
  1581.     local conf=$destsPrefix/etc/grub.conf
  1582.     local dmap=$destsPrefix/boot/grub/device.map
  1583.     local sysb=$destsPrefix/etc/sysconfig/bootloader
  1584.     local stage=/boot/grub/stage2
  1585.     local console=""
  1586.     local kname=""
  1587.     local kernel=""
  1588.     local initrd=""
  1589.     local title=""
  1590.     local rdisk=""
  1591.     local fbmode=$vga
  1592.     local xencons=$xencons
  1593.     if [ ! -z "$OEM_RECOVERY" ];then
  1594.         local gdevreco=$(expr $recoid - 1)
  1595.     fi
  1596.     if [ -z "$fbmode" ];then
  1597.         fbmode=$DEFAULT_VGA
  1598.     fi
  1599.     #======================================
  1600.     # check for device by ID
  1601.     #--------------------------------------
  1602.     local diskByID=`getDiskID $rdev`
  1603.     local swapByID=`getDiskID $swap`
  1604.     #======================================
  1605.     # check for boot image .profile
  1606.     #--------------------------------------
  1607.     if [ -f /.profile ];then
  1608.         importFile < /.profile
  1609.     fi
  1610.     #======================================
  1611.     # check for system image .profile
  1612.     #--------------------------------------
  1613.     if [ -f $mountPrefix/image/.profile ];then
  1614.         importFile < $mountPrefix/image/.profile
  1615.     fi
  1616.     #======================================
  1617.     # check for bootloader displayname
  1618.     #--------------------------------------
  1619.     if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
  1620.         kiwi_oemtitle=$kiwi_displayname
  1621.     fi
  1622.     #======================================
  1623.     # check for kernel options
  1624.     #--------------------------------------
  1625.     if [ ! -z "$kiwi_cmdline" ];then
  1626.         KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
  1627.     fi
  1628.     #======================================
  1629.     # check for grub device
  1630.     #--------------------------------------
  1631.     if [ -z "$gnum" ];then
  1632.         gnum=1
  1633.     fi
  1634.     #======================================
  1635.     # check for grub title postfix
  1636.     #--------------------------------------
  1637.     if [ -z "$gfix" ];then
  1638.         gfix="unknown"
  1639.     fi
  1640.     #======================================
  1641.     # check for boot TIMEOUT
  1642.     #--------------------------------------
  1643.     if [ -z "$KIWI_BOOT_TIMEOUT" ];then
  1644.         KIWI_BOOT_TIMEOUT=10;
  1645.     fi
  1646.     #======================================
  1647.     # create directory structure
  1648.     #--------------------------------------
  1649.     for dir in $menu $conf $dmap $sysb;do
  1650.         dir=`dirname $dir`; mkdir -p $dir
  1651.     done
  1652.     #======================================
  1653.     # setup grub device node
  1654.     #--------------------------------------
  1655.     gdev="(hd0,$gnum)"
  1656.     #======================================
  1657.     # create menu.lst file
  1658.     #--------------------------------------
  1659.     echo "timeout $KIWI_BOOT_TIMEOUT"  > $menu
  1660.     if [ -f /image/loader/message ] || [ -f /boot/message ];then
  1661.         echo "gfxmenu $gdev/boot/message" >> $menu
  1662.     fi
  1663.     local count=1
  1664.     IFS="," ; for i in $KERNEL_LIST;do
  1665.         if test ! -z "$i";then
  1666.             #======================================
  1667.             # create grub requirements
  1668.             #--------------------------------------
  1669.             kernel=`echo $i | cut -f1 -d:`
  1670.             initrd=`echo $i | cut -f2 -d:`
  1671.             kname=${KERNEL_NAME[$count]}
  1672.             if ! echo $gfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
  1673.                 if [ "$count" = "1" ];then
  1674.                     title=$(makeLabel "$gfix")
  1675.                 else
  1676.                     title=$(makeLabel "$kname [ $gfix ]")
  1677.                 fi
  1678.             elif [ -z "$kiwi_oemtitle" ];then
  1679.                 title=$(makeLabel "$kname [ $gfix ]")
  1680.             else
  1681.                 if [ "$count" = "1" ];then
  1682.                     title=$(makeLabel "$kiwi_oemtitle [ $gfix ]")
  1683.                 else
  1684.                     title=$(makeLabel "$kiwi_oemtitle-$kname [ $gfix ]")
  1685.                 fi
  1686.             fi
  1687.             #======================================
  1688.             # create standard entry
  1689.             #--------------------------------------
  1690.             echo "title $title"                                   >> $menu
  1691.             if xenServer $kname $mountPrefix;then
  1692.                 echo " root $gdev"                                >> $menu
  1693.                 echo " kernel /boot/xen.gz"                       >> $menu
  1694.                 echo -n " module /boot/$kernel"                   >> $menu
  1695.                 echo -n " root=$diskByID"                         >> $menu
  1696.                 if [ ! -z "$imageDiskDevice" ];then
  1697.                     echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
  1698.                 fi
  1699.                 echo -n " $console vga=$fbmode splash=silent"     >> $menu
  1700.                 if [ ! -z "$swap" ];then
  1701.                     echo -n " resume=$swapByID"                   >> $menu
  1702.                 fi
  1703.                 if [ ! -z "$xencons" ]; then
  1704.                     echo -n " xencons=$xencons"                   >> $menu
  1705.                 fi
  1706.                 echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
  1707.                 echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
  1708.                 if [ "$haveLVM" = "yes" ];then
  1709.                     echo -n " VGROUP=$VGROUP"                     >> $menu
  1710.                 fi
  1711.                 echo " showopts"                                  >> $menu
  1712.                 echo " module /boot/$initrd"                      >> $menu
  1713.             else
  1714.                 echo -n " kernel $gdev/boot/$kernel"              >> $menu
  1715.                 echo -n " root=$diskByID"                         >> $menu
  1716.                 if [ ! -z "$imageDiskDevice" ];then
  1717.                     echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
  1718.                 fi
  1719.                 echo -n " $console vga=$fbmode splash=silent"     >> $menu
  1720.                 if [ ! -z "$swap" ];then
  1721.                     echo -n " resume=$swapByID"                   >> $menu
  1722.                 fi
  1723.                 if [ ! -z "$xencons" ]; then
  1724.                     echo -n " xencons=$xencons"                   >> $menu
  1725.                 fi
  1726.                 echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
  1727.                 echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
  1728.                 if [ "$haveLVM" = "yes" ];then
  1729.                     echo -n " VGROUP=$VGROUP"                     >> $menu
  1730.                 fi
  1731.                 echo " showopts"                                  >> $menu
  1732.                 echo " initrd $gdev/boot/$initrd"                 >> $menu
  1733.             fi
  1734.             #======================================
  1735.             # create failsafe entry
  1736.             #--------------------------------------
  1737.             title=$(makeLabel "Failsafe -- $title")
  1738.             echo "title $title"                                   >> $menu
  1739.             if xenServer $kname $mountPrefix;then
  1740.                 echo " root $gdev"                                >> $menu
  1741.                 echo " kernel /boot/xen.gz"                       >> $menu
  1742.                 echo -n " module /boot/$kernel"                   >> $menu
  1743.                 echo -n " root=$diskByID"                         >> $menu
  1744.                 if [ ! -z "$imageDiskDevice" ];then
  1745.                     echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
  1746.                 fi
  1747.                 echo -n " $console vga=$fbmode splash=silent"     >> $menu
  1748.                 echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
  1749.                 echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
  1750.                 if [ "$haveLVM" = "yes" ];then
  1751.                     echo -n " VGROUP=$VGROUP"                     >> $menu
  1752.                 fi
  1753.                 echo -n " showopts ide=nodma apm=off acpi=off"    >> $menu
  1754.                 echo -n " noresume selinux=0 nosmp"               >> $menu
  1755.                 if [ ! -z "$xencons" ]; then
  1756.                     echo -n " xencons=$xencons"                   >> $menu
  1757.                 fi
  1758.                 echo " noapic maxcpus=0 edd=off"                  >> $menu
  1759.                 echo " module /boot/$initrd"                      >> $menu
  1760.             else
  1761.                 echo -n " kernel $gdev/boot/$kernel"              >> $menu
  1762.                 echo -n " root=$diskByID"                         >> $menu
  1763.                 if [ ! -z "$imageDiskDevice" ];then
  1764.                     echo -n " disk=$(getDiskID $imageDiskDevice)"     >> $menu
  1765.                 fi
  1766.                 echo -n " $console vga=$fbmode splash=silent"     >> $menu
  1767.                 echo -n " $KIWI_INITRD_PARAMS"                    >> $menu
  1768.                 echo -n " $KIWI_KERNEL_OPTIONS"                   >> $menu
  1769.                 if [ "$haveLVM" = "yes" ];then
  1770.                     echo -n " VGROUP=$VGROUP"                     >> $menu
  1771.                 fi
  1772.                 echo -n " showopts ide=nodma apm=off acpi=off"    >> $menu
  1773.                 echo -n " noresume selinux=0 nosmp"               >> $menu
  1774.                 if [ ! -z "$xencons" ]; then
  1775.                     echo -n " xencons=$xencons"                   >> $menu
  1776.                 fi
  1777.                 echo " noapic maxcpus=0 edd=off"                  >> $menu
  1778.                 echo " initrd $gdev/boot/$initrd"                 >> $menu
  1779.             fi
  1780.             count=`expr $count + 1`
  1781.         fi
  1782.     done
  1783.     #======================================
  1784.     # create recovery entry
  1785.     #--------------------------------------
  1786.     if [ ! -z "$OEM_RECOVERY" ];then
  1787.         echo "title Recovery"                             >> $menu
  1788.         echo " rootnoverify (hd0,$gdevreco)"              >> $menu
  1789.         echo " chainloader +1"                            >> $menu
  1790.     fi
  1791.     #======================================
  1792.     # create grub.conf file
  1793.     #--------------------------------------
  1794.     echo "root $gdev" > $conf
  1795.     if dd if=$rdev bs=1 count=512 | file - | grep -q Bootloader;then
  1796.         echo "setup --stage2=$stage $gdev" >> $conf
  1797.     else
  1798.         echo "setup --stage2=$stage (hd0)" >> $conf
  1799.     fi
  1800.     echo "quit" >> $conf
  1801.     #======================================
  1802.     # create grub device map
  1803.     #--------------------------------------
  1804.     rdisk=`echo $rdev | sed -e s"@[0-9]@@g"`
  1805.     if [ ! -z "$imageDiskDevice" ];then
  1806.         rdisk=$imageDiskDevice
  1807.     fi
  1808.     echo "(hd0) $rdisk" > $dmap
  1809.     #======================================
  1810.     # create sysconfig/bootloader
  1811.     #--------------------------------------
  1812.     echo "LOADER_TYPE=\"grub\""                               > $sysb
  1813.     echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
  1814.     echo "DEFAULT_VGA=\"$fbmode\""                           >> $sysb  
  1815.     echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
  1816.     if [ ! -z "$swap" ];then
  1817.         echo -n " resume=$swapByID"                          >> $sysb
  1818.     fi
  1819.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  1820.     echo " showopts\""                                       >> $sysb
  1821.     echo "FAILSAFE_VGA=\"$fbmode\""                          >> $sysb
  1822.     echo -n "FAILSAFE_APPEND=\"root=$diskByID splash=silent" >> $sysb
  1823.     if [ ! -z "$swap" ];then
  1824.         echo -n " resume=$swapByID"                          >> $sysb
  1825.     fi
  1826.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  1827.     echo -n " showopts ide=nodma apm=off acpi=off noresume"  >> $sysb
  1828.     echo "selinux=0 nosmp noapic maxcpus=0 edd=off\""        >> $sysb
  1829. }
  1830. #======================================
  1831. # setupBootLoaderLilo
  1832. #--------------------------------------
  1833. function setupBootLoaderLilo {
  1834.     # /.../
  1835.     # create lilo.conf file used for
  1836.     # installing the bootloader
  1837.     # ----
  1838.     local mountPrefix=$1  # mount path of the image
  1839.     local destsPrefix=$2  # base dir for the config files
  1840.     local lnum=$3         # lilo boot partition ID
  1841.     local rdev=$4         # lilo root partition
  1842.     local lfix=$5         # lilo title postfix
  1843.     local swap=$6         # optional swap partition
  1844.     local conf=$destsPrefix/etc/lilo.conf
  1845.     local sysb=$destsPrefix/etc/sysconfig/bootloader
  1846.     local console=""
  1847.     local kname=""
  1848.     local kernel=""
  1849.     local initrd=""
  1850.     local title=""
  1851.     local rdisk=""
  1852.     local fbmode=$vga
  1853.     local xencons=$xencons
  1854.     if [ -z "$fbmode" ];then
  1855.         fbmode=$DEFAULT_VGA
  1856.     fi
  1857.     #======================================
  1858.     # check for device by ID
  1859.     #--------------------------------------
  1860.     local diskByID=`getDiskID $rdev`
  1861.     local swapByID=`getDiskID $swap`
  1862.     #======================================
  1863.     # check for boot image .profile
  1864.     #--------------------------------------
  1865.     if [ -f /.profile ];then
  1866.         importFile < /.profile
  1867.     fi
  1868.     #======================================
  1869.     # check for bootloader displayname
  1870.     #--------------------------------------
  1871.     if [ -z "$kiwi_oemtitle" ] && [ ! -z "$kiwi_displayname" ];then
  1872.         kiwi_oemtitle=$kiwi_displayname
  1873.     fi
  1874.     #======================================
  1875.     # check for system image .profile
  1876.     #--------------------------------------
  1877.     if [ -f $mountPrefix/image/.profile ];then
  1878.         importFile < $mountPrefix/image/.profile
  1879.     fi
  1880.     #======================================
  1881.     # check for kernel options
  1882.     #--------------------------------------
  1883.     if [ ! -z "$kiwi_cmdline" ];then
  1884.         KIWI_KERNEL_OPTIONS="$KIWI_KERNEL_OPTIONS $kiwi_cmdline"
  1885.     fi
  1886.     #======================================
  1887.     # check for lilo title postfix
  1888.     #--------------------------------------
  1889.     if [ -z "$lfix" ];then
  1890.         lfix="unknown"
  1891.     fi
  1892.     #======================================
  1893.     # check for boot TIMEOUT
  1894.     #--------------------------------------
  1895.     if [ -z "$KIWI_BOOT_TIMEOUT" ];then
  1896.         KIWI_BOOT_TIMEOUT=10;
  1897.     fi
  1898.     #======================================
  1899.     # setup lilo boot device
  1900.     #--------------------------------------
  1901.     rdisk=`echo $rdev | sed -e s"@[0-9]@@g"`
  1902.     if [ ! -z "$imageDiskDevice" ];then
  1903.         rdisk=$imageDiskDevice
  1904.     fi
  1905.     rdev=$rdisk$lnum
  1906.     #======================================
  1907.     # create directory structure
  1908.     #--------------------------------------
  1909.     for dir in $conf $sysb;do
  1910.         dir=`dirname $dir`; mkdir -p $dir
  1911.     done
  1912.     #======================================
  1913.     # create lilo.conf file
  1914.     #--------------------------------------
  1915.     echo "boot=$rdev"                                        >  $conf
  1916.     echo "activate"                                          >> $conf
  1917.     echo "timeout=`expr $KIWI_BOOT_TIMEOUT \* 10`"           >> $conf
  1918.     echo "default=kiwi$count"                                >> $conf
  1919.     local count=1
  1920.     IFS="," ; for i in $KERNEL_LIST;do
  1921.         if test ! -z "$i";then
  1922.             #======================================
  1923.             # create lilo requirements
  1924.             #--------------------------------------
  1925.             kernel=`echo $i | cut -f1 -d:`
  1926.             initrd=`echo $i | cut -f2 -d:`
  1927.             kname=${KERNEL_NAME[$count]}
  1928.             if ! echo $lfix | grep -E -q "OEM|USB|VMX|NET|unknown";then
  1929.                 if [ "$count" = "1" ];then
  1930.                     title=$(makeLabel "$lfix")
  1931.                 else
  1932.                     title=$(makeLabel "$kname [ $lfix ]")
  1933.                 fi
  1934.             elif [ -z "$kiwi_oemtitle" ];then
  1935.                 title=$(makeLabel "$kname [ $lfix ]")
  1936.             else
  1937.                 if [ "$count" = "1" ];then
  1938.                     title=$(makeLabel "$kiwi_oemtitle [ $lfix ]")
  1939.                 else
  1940.                     title=$(makeLabel "$kiwi_oemtitle-$kname [ $lfix ]")
  1941.                 fi
  1942.             fi
  1943.             #======================================
  1944.             # create standard entry
  1945.             #--------------------------------------
  1946.             echo "label=\"$title\""                           >> $conf
  1947.             if xenServer $kname $mountPrefix;then
  1948.                 systemException \
  1949.                     "*** lilo: Xen dom0 boot not implemented ***" \
  1950.                 "reboot"
  1951.             else
  1952.                 echo "image=/boot/$kernel"                    >> $conf
  1953.                 echo "initrd=/boot/$initrd"                   >> $conf
  1954.                 echo -n "append=\"quiet sysrq=1 panic=9"      >> $conf
  1955.                 echo -n " root=$diskByID"                     >> $conf
  1956.                 if [ ! -z "$imageDiskDevice" ];then
  1957.                     echo -n " disk=$(getDiskID $imageDiskDevice)" >> $conf
  1958.                 fi
  1959.                 echo -n " $console vga=$fbmode splash=silent" >> $conf
  1960.                 if [ ! -z "$swap" ];then                    
  1961.                     echo -n " resume=$swapByID"               >> $conf
  1962.                 fi
  1963.                 if [ ! -z "$xencons" ]; then
  1964.                     echo -n " xencons=$xencons"               >> $conf
  1965.                 fi
  1966.                 echo -n " $KIWI_INITRD_PARAMS"                >> $conf
  1967.                 echo -n " $KIWI_KERNEL_OPTIONS"               >> $conf
  1968.                 if [ "$haveLVM" = "yes" ];then
  1969.                     echo -n " VGROUP=$VGROUP"                 >> $conf
  1970.                 fi
  1971.                 echo " showopts\""                            >> $conf
  1972.             fi
  1973.             #======================================
  1974.             # create failsafe entry
  1975.             #--------------------------------------
  1976.             title=$(makeLabel "Failsafe -- $title")
  1977.             echo "label=\"$title\""                           >> $conf
  1978.             if xenServer $kname $mountPrefix;then
  1979.                 systemException \
  1980.                     "*** lilo: Xen dom0 boot not implemented ***" \
  1981.                 "reboot"
  1982.             else
  1983.                 echo "image=/boot/$kernel"                    >> $conf
  1984.                 echo "initrd=/boot/$initrd"                   >> $conf
  1985.                 echo -n "append=\"quiet sysrq=1 panic=9"      >> $conf
  1986.                 echo -n " root=$diskByID"                     >> $conf
  1987.                 if [ ! -z "$imageDiskDevice" ];then
  1988.                     echo -n " disk=$(getDiskID $imageDiskDevice)" >> $conf
  1989.                 fi
  1990.                 echo -n " $console vga=$fbmode splash=silent" >> $conf
  1991.                 if [ ! -z "$swap" ];then
  1992.                     echo -n " resume=$swapByID"               >> $conf
  1993.                 fi
  1994.                 if [ ! -z "$xencons" ]; then
  1995.                     echo -n " xencons=$xencons"               >> $conf
  1996.                 fi
  1997.                 echo -n " $KIWI_INITRD_PARAMS"                >> $conf
  1998.                 echo -n " $KIWI_KERNEL_OPTIONS"               >> $conf
  1999.                 if [ "$haveLVM" = "yes" ];then
  2000.                     echo -n " VGROUP=$VGROUP"                 >> $conf
  2001.                 fi
  2002.                 echo -n " showopts ide=nodma apm=off"         >> $conf
  2003.                 echo " acpi=off noresume selinux=0 nosmp\""   >> $conf
  2004.             fi
  2005.             count=`expr $count + 1`
  2006.         fi
  2007.     done
  2008.     #======================================
  2009.     # create recovery entry
  2010.     #--------------------------------------
  2011.     if [ ! -z "$OEM_RECOVERY" ];then
  2012.         systemException \
  2013.             "*** lilo: recovery chain loading not implemented ***" \
  2014.         "reboot"
  2015.     fi
  2016.     #======================================
  2017.     # create sysconfig/bootloader
  2018.     #--------------------------------------
  2019.     echo "LOADER_TYPE=\"lilo\""                               > $sysb
  2020.     echo "LOADER_LOCATION=\"mbr\""                           >> $sysb
  2021.     echo "DEFAULT_VGA=\"$fbmode\""                           >> $sysb
  2022.     echo -n "DEFAULT_APPEND=\"root=$diskByID splash=silent"  >> $sysb
  2023.     if [ ! -z "$swap" ];then
  2024.         echo -n " resume=$swapByID"                          >> $sysb
  2025.     fi
  2026.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  2027.     echo " showopts\""                                       >> $sysb
  2028.     echo "FAILSAFE_VGA=\"$fbmode\""                          >> $sysb
  2029.     echo -n "FAILSAFE_APPEND=\"root=$diskByID splash=silent" >> $sysb
  2030.     if [ ! -z "$swap" ];then
  2031.         echo -n " resume=$swapByID"                          >> $sysb
  2032.     fi
  2033.     echo -n " $KIWI_INITRD_PARAMS $KIWI_KERNEL_OPTIONS"      >> $sysb
  2034.     echo -n " showopts ide=nodma apm=off acpi=off noresume"  >> $sysb
  2035.     echo "selinux=0 nosmp noapic maxcpus=0 edd=off\""        >> $sysb
  2036. }
  2037. #======================================
  2038. # setupDefaultPXENetwork
  2039. #--------------------------------------
  2040. function setupDefaultPXENetwork {
  2041.     # /.../
  2042.     # create the /sysconfig/network file according to the PXE
  2043.     # boot interface.
  2044.     # ----
  2045.     local prefix=$1
  2046.     local niface=$prefix/etc/sysconfig/network/ifcfg-$PXE_IFACE
  2047.     mkdir -p $prefix/etc/sysconfig/network
  2048.     cat > $niface < /dev/null
  2049.     echo "BOOTPROTO='dhcp'"    >> $niface
  2050.     echo "STARTMODE='ifplugd'" >> $niface
  2051.     echo "USERCONTROL='no'"    >> $niface
  2052. }
  2053. #======================================
  2054. # setupDefaultFstab
  2055. #--------------------------------------
  2056. function setupDefaultFstab {
  2057.     # /.../
  2058.     # create a new /etc/fstab file with the default entries
  2059.     # ----
  2060.     local prefix=$1
  2061.     local nfstab=$prefix/etc/fstab
  2062.     mkdir -p $prefix/etc
  2063.     cat > $nfstab < /dev/null
  2064.     echo "devpts  /dev/pts   devpts  mode=0620,gid=5 0 0"   >> $nfstab
  2065.     echo "proc    /proc      proc    defaults 0 0"          >> $nfstab
  2066.     echo "sysfs   /sys       sysfs   noauto 0 0"            >> $nfstab
  2067. }
  2068. #======================================
  2069. # updateRootDeviceFstab
  2070. #--------------------------------------
  2071. function updateRootDeviceFstab {
  2072.     # /.../
  2073.     # add one line to the fstab file for the root device
  2074.     # ----
  2075.     IFS=$IFS_ORIG
  2076.     local prefix=$1
  2077.     local rdev=$2
  2078.     local nfstab=$prefix/etc/fstab
  2079.     local diskByID=`getDiskID $rdev`
  2080.     if [ ! -z "$NFSROOT" ];then
  2081.         local server=`echo $rdev | cut -f3 -d" "`
  2082.         local option=`echo $rdev | cut -f2 -d" "`
  2083.         echo "$server / nfs $option 0 0" >> $nfstab
  2084.         return
  2085.     fi
  2086.     #======================================
  2087.     # check for device by ID
  2088.     #--------------------------------------
  2089.     if [ -z "$UNIONFS_CONFIG" ]; then
  2090.         echo "$diskByID / $FSTYPE defaults 0 0" >> $nfstab
  2091.     else
  2092.         echo "/dev/root / defaults 0 0" >> $nfstab
  2093.     fi
  2094.     #======================================
  2095.     # check for LVM volume setup
  2096.     #--------------------------------------
  2097.     if [ "$haveLVM" = "yes" ];then
  2098.         for i in /dev/$VGROUP/LV*;do
  2099.             if [ ! -e $i ];then
  2100.                 continue
  2101.             fi
  2102.             local volume=$(echo $i | cut -f4 -d/ | cut -c3-)
  2103.             local mpoint=$(echo $volume | tr _ /)
  2104.             if \
  2105.                 [ ! $volume = "Root" ] && \
  2106.                 [ ! $volume = "Comp" ] && \
  2107.                 [ ! $volume = "Home" ] && \
  2108.                 [ ! $volume = "Swap" ]
  2109.             then
  2110.                 echo "/dev/$VGROUP/LV$volume /$mpoint $FSTYPE defaults 0 0" \
  2111.                 >> $nfstab
  2112.             fi
  2113.         done
  2114.     fi
  2115. }
  2116. #======================================
  2117. # updateSwapDeviceFstab
  2118. #--------------------------------------
  2119. function updateSwapDeviceFstab {
  2120.     # /.../
  2121.     # add one line to the fstab file for the swap device
  2122.     # ----
  2123.     local prefix=$1
  2124.     local sdev=$2
  2125.     local diskByID=`getDiskID $sdev`
  2126.     local nfstab=$prefix/etc/fstab
  2127.     echo "$diskByID swap swap pri=42 0 0" >> $nfstab
  2128. }
  2129. #======================================
  2130. # updateLVMBootDeviceFstab
  2131. #--------------------------------------
  2132. function updateLVMBootDeviceFstab {
  2133.     # /.../
  2134.     # add one line to the fstab file for the /lvmboot
  2135.     # device partition
  2136.     # ----
  2137.     local prefix=$1
  2138.     local sdev=$2
  2139.     local mount=$3
  2140.     if [ -z "$mount" ];then
  2141.         mount=/lvmboot
  2142.     fi
  2143.     local diskByID=`getDiskID $sdev`
  2144.     local nfstab=$prefix/etc/fstab
  2145.     if [ ! -z "$FSTYPE" ];then
  2146.         FSTYPE_SAVE=$FSTYPE
  2147.     fi
  2148.     #======================================
  2149.     # probe filesystem
  2150.     #--------------------------------------
  2151.     probeFileSystem $sdev
  2152.     if [ -z "$FSTYPE" ] || [ "$FSTYPE" = "unknown" ];then
  2153.         FSTYPE="auto"
  2154.     fi
  2155.     echo "$diskByID $mount $FSTYPE defaults 0 0" >> $nfstab
  2156.     echo "$mount/boot /boot none bind 0 0" >> $nfstab
  2157.     if [ ! -z "$FSTYPE_SAVE" ];then
  2158.         FSTYPE=$FSTYPE_SAVE
  2159.     fi
  2160. }
  2161. #======================================
  2162. # updateClicBootDeviceFstab
  2163. #--------------------------------------
  2164. function updateClicBootDeviceFstab {
  2165.     updateLVMBootDeviceFstab $1 $2 "/clicboot"
  2166. }
  2167. #======================================
  2168. # updateLuksBootDeviceFstab
  2169. #--------------------------------------
  2170. function updateLuksBootDeviceFstab {
  2171.     updateLVMBootDeviceFstab $1 $2 "/luksboot"
  2172. }
  2173. #======================================
  2174. # updateBtrBootDeviceFstab
  2175. #--------------------------------------
  2176. function updateBtrBootDeviceFstab {
  2177.     updateLVMBootDeviceFstab $1 $2 "/btrboot"
  2178. }
  2179. #======================================
  2180. # updateSyslinuxBootDeviceFstab
  2181. #--------------------------------------
  2182. function updateSyslinuxBootDeviceFstab {
  2183.     # /.../
  2184.     # add one line to the fstab file for the /syslboot
  2185.     # device partition
  2186.     # ----
  2187.     local prefix=$1
  2188.     local sdev=$2
  2189.     local diskByID=`getDiskID $sdev`
  2190.     local nfstab=$prefix/etc/fstab
  2191.     if [ $loader = "syslinux" ];then
  2192.         echo "$diskByID /syslboot vfat defaults 0 0" >> $nfstab
  2193.     else
  2194.         echo "$diskByID /syslboot ext2 defaults 0 0" >> $nfstab
  2195.     fi
  2196.     echo "/syslboot/boot /boot none bind 0 0" >> $nfstab
  2197. }
  2198. #======================================
  2199. # updateOtherDeviceFstab
  2200. #--------------------------------------
  2201. function updateOtherDeviceFstab {
  2202.     # /.../
  2203.     # check the contents of the $PART variable and
  2204.     # add one line to the fstab file for each partition
  2205.     # which has a mount point defined.
  2206.     # ----
  2207.     local prefix=$1
  2208.     local nfstab=$prefix/etc/fstab
  2209.     local index=0
  2210.     local field=0
  2211.     local count=0
  2212.     local device
  2213.     local IFS=","
  2214.     for i in $PART;do
  2215.         field=0
  2216.         count=$((count + 1))
  2217.         IFS=";" ; for n in $i;do
  2218.         case $field in
  2219.             0) partSize=$n   ; field=1 ;;
  2220.             1) partID=$n     ; field=2 ;;
  2221.             2) partMount=$n;
  2222.         esac
  2223.         done
  2224.         if  [ ! -z "$partMount" ]    && \
  2225.             [ ! "$partMount" = "x" ] && \
  2226.             [ ! "$partMount" = "/" ]
  2227.         then
  2228.             device=$(ddn $DISK $count)
  2229.             probeFileSystem $device
  2230.             echo "$device $partMount $FSTYPE defaults 1 1" >> $nfstab
  2231.         fi
  2232.     done
  2233. }
  2234. #======================================
  2235. # setupKernelModules
  2236. #--------------------------------------
  2237. function setupKernelModules {
  2238.     # /.../
  2239.     # create sysconfig/kernel file which includes the
  2240.     # kernel modules to become integrated into the initrd
  2241.     # if created by the distro mkinitrd tool
  2242.     # ----
  2243.     local destprefix=$1
  2244.     local srcprefix=$2
  2245.     if [ -z "$srcprefix" ];then
  2246.         srcprefix=/mnt
  2247.     fi
  2248.     local ktempl=$srcprefix/var/adm/fillup-templates/sysconfig.kernel
  2249.     local syskernel=$destprefix/etc/sysconfig/kernel
  2250.     mkdir -p $destprefix/etc/sysconfig
  2251.     if [ ! -f $ktempl ];then
  2252.         systemException \
  2253.             "Can't find kernel sysconfig template in system image !" \
  2254.         "reboot"
  2255.     fi
  2256.     cp $ktempl $syskernel
  2257.     if [ ! -e $srcprefix/lib/mkinitrd/scripts/boot-usb.sh ];then
  2258.         # /.../
  2259.         # if boot-usb.sh does not exist we are based on an old
  2260.         # mkinitrd version which requires all modules as part of
  2261.         # sysconfig/kernel. Therefore we include all USB modules
  2262.         # required to support USB storage like USB sticks
  2263.         # ----
  2264.         local USB_MODULES="ehci-hcd ohci-hcd uhci-hcd usbcore usb-storage sd"
  2265.         INITRD_MODULES="$INITRD_MODULES $USB_MODULES"
  2266.     fi
  2267.     sed -i -e \
  2268.         s"@^INITRD_MODULES=.*@INITRD_MODULES=\"$INITRD_MODULES\"@" \
  2269.     $syskernel
  2270.     sed -i -e \
  2271.         s"@^DOMU_INITRD_MODULES=.*@DOMU_INITRD_MODULES=\"$DOMURD_MODULES\"@" \
  2272.     $syskernel
  2273. }
  2274. #======================================
  2275. # kernelCheck
  2276. #--------------------------------------
  2277. function kernelCheck {
  2278.     # /.../
  2279.     # Check this running kernel against the kernel
  2280.     # installed in the image. If the version does not
  2281.     # match we need to reboot to activate the system
  2282.     # image kernel.
  2283.     # ----
  2284.     kactive=`uname -r`
  2285.     kreboot=1
  2286.     prefix=$1
  2287.     for i in $prefix/lib/modules/*;do
  2288.         if [ ! -d $i ];then
  2289.             continue
  2290.         fi
  2291.         kinstname=${i##*/}
  2292.         if [ $kinstname = $kactive ];then
  2293.             kreboot=0
  2294.             break
  2295.         fi
  2296.     done
  2297.     if [ $kreboot = 1 ];then
  2298.         Echo "Kernel versions do not match rebooting in 5 sec..."
  2299.         REBOOT_IMAGE="yes"
  2300.         sleep 5
  2301.     fi
  2302. }
  2303. #======================================
  2304. # probeFileSystem
  2305. #--------------------------------------
  2306. function probeFileSystem {
  2307.     # /.../
  2308.     # probe for the filesystem type. The function will
  2309.     # read 128 kB of the given device and check the
  2310.     # filesystem header data to detect the type of the
  2311.     # filesystem
  2312.     # ----
  2313.     FSTYPE=unknown
  2314.     FSTYPE=$(blkid $1 -s TYPE -o value)
  2315.     case $FSTYPE in
  2316.         btrfs)       FSTYPE=btrfs ;;
  2317.         ext4)        FSTYPE=ext4 ;;
  2318.         ext3)        FSTYPE=ext3 ;;
  2319.         ext2)        FSTYPE=ext2 ;;
  2320.         reiserfs)    FSTYPE=reiserfs ;;
  2321.         squashfs)    FSTYPE=squashfs ;;
  2322.         luks)        FSTYPE=luks ;;
  2323.         crypto_LUKS) FSTYPE=luks ;;
  2324.         vfat)        FSTYPE=vfat ;;
  2325.         clicfs)      FSTYPE=clicfs ;;
  2326.         *)
  2327.             FSTYPE=unknown
  2328.         ;;
  2329.     esac
  2330.     if [ $FSTYPE = "unknown" ];then
  2331.         dd if=$1 of=/tmp/filesystem-$$ bs=128k count=1 >/dev/null
  2332.     fi
  2333.     if [ $FSTYPE = "unknown" ];then
  2334.         if grep -q ^CLIC /tmp/filesystem-$$;then
  2335.             FSTYPE=clicfs
  2336.         fi
  2337.         if grep -q ^hsqs /tmp/filesystem-$$;then
  2338.             FSTYPE=squashfs
  2339.         fi
  2340.     fi
  2341.     export FSTYPE
  2342. }
  2343. #======================================
  2344. # getSystemIntegrity
  2345. #--------------------------------------
  2346. function getSystemIntegrity {
  2347.     # /.../
  2348.     # check the variable SYSTEM_INTEGRITY which contains
  2349.     # information about the status of all image portions
  2350.     # per partition. If a number is given as parameter only
  2351.     # the information from the image assigned to this partition
  2352.     # is returned
  2353.     # ----
  2354.     if [ -z "$SYSTEM_INTEGRITY" ];then
  2355.         echo "clean"
  2356.     else
  2357.         echo $SYSTEM_INTEGRITY | cut -f$1 -d:
  2358.     fi
  2359. }
  2360. #======================================
  2361. # getSystemMD5Status
  2362. #--------------------------------------
  2363. function getSystemMD5Status {
  2364.     # /.../
  2365.     # return the md5 status of the given image number.
  2366.     # the function works similar to getSystemIntegrity
  2367.     # ----
  2368.     echo $SYSTEM_MD5STATUS | cut -f$1 -d:
  2369. }
  2370. #======================================
  2371. # waitForUSBDeviceScan
  2372. #--------------------------------------
  2373. function waitForUSBDeviceScan {
  2374.     local devices=0
  2375.     if [ ! "$HAVE_USB" = "yes" ];then
  2376.         return
  2377.     fi
  2378.     if [ ! "$SCAN_USB" = "complete" ];then
  2379.         Echo -n "Waiting for USB device scan to complete..."
  2380.         while \
  2381.             [ $(dmesg|grep -c 'usb-storage: device scan complete') -lt 1 ] && \
  2382.             [ $devices -lt 15 ]
  2383.         do
  2384.             echo -n .
  2385.             sleep 1
  2386.             devices=$(( $devices + 1 ))
  2387.         done
  2388.         echo
  2389.         udevPending
  2390.         SCAN_USB=complete
  2391.     fi
  2392. }
  2393. #======================================
  2394. # probeUSB
  2395. #--------------------------------------
  2396. function probeUSB {
  2397.     local module=""
  2398.     local stdevs=""
  2399.     local hwicmd="/usr/sbin/hwinfo"
  2400.     export HAVE_USB="no"
  2401.     export SCAN_USB="not-started"
  2402.     if [ $HAVE_MODULES_ORDER = 0 ];then
  2403.         #======================================
  2404.         # load host controller modules
  2405.         #--------------------------------------
  2406.         IFS="%"
  2407.         for i in \
  2408.             `$hwicmd --usb | grep "Driver [IA]" |
  2409.             sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
  2410.         do
  2411.             if echo $i | grep -q "#0";then
  2412.                 module=`echo $i | cut -f2 -d"\"" | tr -d " "`
  2413.                 module=`echo $module | sed -es"@modprobe@@g"`
  2414.                 IFS=";"
  2415.                 for m in $module;do
  2416.                     if ! echo $stdevs | grep -q $m;then
  2417.                         stdevs="$stdevs $m"
  2418.                     fi
  2419.                 done
  2420.             fi
  2421.         done
  2422.         for i in \
  2423.             `$hwicmd --usb-ctrl | grep "Driver [IA]" |
  2424.             sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
  2425.         do
  2426.             if echo $i | grep -q "#0";then
  2427.                 module=`echo $i | cut -f2 -d"\"" | tr -d " "`
  2428.                 module=`echo $module | sed -es"@modprobe@@g"`
  2429.                 IFS=";"
  2430.                 for m in $module;do
  2431.                     if ! echo $stdevs | grep -q $m;then
  2432.                         stdevs="$stdevs $m"
  2433.                     fi
  2434.                 done
  2435.             fi
  2436.         done
  2437.         IFS=$IFS_ORIG
  2438.         stdevs=`echo $stdevs`
  2439.         for module in $stdevs;do
  2440.             Echo "Probing module: $module"
  2441.             modprobe $module >/dev/null
  2442.         done
  2443.         #======================================
  2444.         # check load status for host controller
  2445.         #--------------------------------------
  2446.         if [ -z "$stdevs" ];then
  2447.             return
  2448.         fi
  2449.         #======================================
  2450.         # manually load storage/input drivers
  2451.         #--------------------------------------
  2452.         for i in usbhid usb-storage;do
  2453.             modprobe $i &>/dev/null
  2454.         done
  2455.     fi
  2456.     stdevs=$(ls -1 /sys/bus/usb/devices/ | wc -l)
  2457.     if [ $stdevs -gt 0 ];then
  2458.         export HAVE_USB="yes"
  2459.     fi
  2460.     waitForUSBDeviceScan
  2461. }
  2462. #======================================
  2463. # probeDevices
  2464. #--------------------------------------
  2465. function probeDevices {
  2466.     local skipUSB=$1
  2467.     #======================================
  2468.     # probe USB devices and load modules
  2469.     #--------------------------------------
  2470.     if [ -z "$skipUSB" ];then
  2471.         probeUSB
  2472.     fi
  2473.     #======================================
  2474.     # probe Disk devices and load modules
  2475.     #--------------------------------------
  2476.     if [ $HAVE_MODULES_ORDER = 0 ];then
  2477.         Echo "Including required kernel modules..."
  2478.         IFS="%"
  2479.         local module=""
  2480.         local stdevs=""
  2481.         local hwicmd="/usr/sbin/hwinfo"
  2482.         for i in \
  2483.             `$hwicmd --storage | grep "Driver [IA]" |
  2484.             sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
  2485.         do
  2486.             if echo $i | grep -q "#0";then
  2487.                 module=`echo $i | cut -f2 -d"\"" | tr -d " "`
  2488.                 module=`echo $module | sed -es"@modprobe@@g"`
  2489.                 IFS=";"
  2490.                 for m in $module;do
  2491.                     if ! echo $stdevs | grep -q $m;then
  2492.                         stdevs="$stdevs $m"
  2493.                     fi
  2494.                 done
  2495.             fi
  2496.         done
  2497.         IFS=$IFS_ORIG
  2498.         stdevs=`echo $stdevs`
  2499.         if [ ! -z "$kiwikernelmodule" ];then
  2500.             for module in $kiwikernelmodule;do
  2501.                 Echo "Probing module (cmdline): $module"
  2502.                 INITRD_MODULES="$INITRD_MODULES $module"
  2503.                 modprobe $module >/dev/null
  2504.             done
  2505.         fi
  2506.         for module in $stdevs;do
  2507.             loadok=1
  2508.             for broken in $kiwibrokenmodule;do
  2509.                 if [ $broken = $module ];then
  2510.                     Echo "Prevent loading module: $module"
  2511.                     loadok=0; break
  2512.                 fi
  2513.             done
  2514.             if [ $loadok = 1 ];then
  2515.                 Echo "Probing module: $module"
  2516.                 INITRD_MODULES="$INITRD_MODULES $module"
  2517.                 modprobe $module >/dev/null
  2518.             fi
  2519.         done
  2520.         hwinfo --block &>/dev/null
  2521.         # /.../
  2522.         # older systems require ide-disk to be present at any time
  2523.         # for details on this crappy call see bug: #250241
  2524.         # ----
  2525.         modprobe ide-disk &>/dev/null
  2526.     else
  2527.         if [ ! -z "$kiwikernelmodule" ];then
  2528.             for module in $kiwikernelmodule;do
  2529.                 Echo "Probing module (cmdline): $module"
  2530.                 INITRD_MODULES="$INITRD_MODULES $module"
  2531.                 modprobe $module >/dev/null
  2532.             done
  2533.         fi
  2534.     fi
  2535.     #======================================
  2536.     # Manual loading of modules
  2537.     #--------------------------------------
  2538.     for i in rd brd edd dm-mod xennet xenblk virtio_blk;do
  2539.         modprobe $i &>/dev/null
  2540.     done
  2541.     udevPending
  2542. }
  2543. #======================================
  2544. # CDDevice
  2545. #--------------------------------------
  2546. function CDDevice {
  2547.     # /.../
  2548.     # detect CD/DVD/USB device(s). The function use the information
  2549.     # from hwinfo --cdrom to search for the block device
  2550.     # ----
  2551.     IFS=$IFS_ORIG
  2552.     local count=0
  2553.     local h=/usr/sbin/hwinfo
  2554.     if [ $HAVE_MODULES_ORDER = 0 ];then
  2555.         for module in sg sd_mod sr_mod cdrom ide-cd BusLogic vfat; do
  2556.             /sbin/modprobe $module
  2557.         done
  2558.     fi
  2559.     Echo -n "Waiting for CD/DVD device(s) to appear..."
  2560.     while true;do
  2561.         cddevs=`$h --cdrom | grep "Device File:"|sed -e"s@(.*)@@" | cut -f2 -d:`
  2562.         cddevs=`echo $cddevs`
  2563.         for i in $cddevs;do
  2564.             if [ -b $i ];then
  2565.                 test -z $cddev && cddev=$i || cddev=$cddev:$i
  2566.             fi
  2567.         done
  2568.         if [ ! -z "$cddev" ] || [ $count -eq 12 ]; then
  2569.             break
  2570.         else
  2571.             echo -n .
  2572.             sleep 1
  2573.         fi
  2574.         count=`expr $count + 1`
  2575.     done
  2576.     echo
  2577.     if [ -z "$cddev" ];then
  2578.         USBStickDevice
  2579.         if [ $stickFound = 0 ];then
  2580.             systemException \
  2581.                 "Failed to detect CD/DVD or USB drive !" \
  2582.             "reboot"
  2583.         fi
  2584.         cddev=$stickDevice
  2585.     fi
  2586. }
  2587. #======================================
  2588. # USBStickDevice
  2589. #--------------------------------------
  2590. function USBStickDevice {
  2591.     stickFound=0
  2592.     local mode=$1
  2593.     #======================================
  2594.     # search for USB removable devices
  2595.     #--------------------------------------
  2596.     waitForUSBDeviceScan
  2597.     for device in /sys/bus/usb/drivers/usb-storage/*;do
  2598.         if [ ! -L $device ];then
  2599.             continue
  2600.         fi
  2601.         descriptions=$device/host*/target*/*/block*
  2602.         for description in $descriptions;do
  2603.             if [ ! -d $description ];then
  2604.                 continue
  2605.             fi
  2606.             isremovable=$description/removable
  2607.             storageID=`echo $description | cut -f1 -d: | xargs basename`
  2608.             devicebID=`basename $description | cut -f2 -d:`
  2609.             if [ $devicebID = "block" ];then
  2610.                 devicebID=`ls -1 $description`
  2611.                 isremovable=$description/$devicebID/removable
  2612.             fi
  2613.             serial="/sys/bus/usb/devices/$storageID/serial"
  2614.             device="/dev/$devicebID"
  2615.             if [ ! -b $device ];then
  2616.                 continue;
  2617.             fi
  2618.             if [ ! -f $isremovable ];then
  2619.                 continue;
  2620.             fi
  2621.             if ! partitionSize $device >/dev/null;then
  2622.                 continue;
  2623.             fi
  2624.             if [ ! -f $serial ];then
  2625.                 serial="USB Stick (unknown type)"
  2626.             else
  2627.                 serial=`cat $serial`
  2628.             fi
  2629.             removable=`cat $isremovable`
  2630.             # /.../
  2631.             # don't check the removable flag, it could be wrong
  2632.             # especially for USB hard disks connected via a
  2633.             # USB caddy, details in bug: 535113
  2634.             # ----
  2635.             removable=1
  2636.             if [ $removable -eq 1 ];then
  2637.                 stickRoot=$device
  2638.                 stickDevice=$(ddn $device 1)
  2639.                 for devnr in 1 2;do
  2640.                     dev=$(ddn $stickRoot $devnr)
  2641.                     if ! kiwiMount "$dev" "/mnt" "-o ro";then
  2642.                         continue
  2643.                     fi
  2644.                     if [ "$mode" = "install" ];then
  2645.                         # /.../
  2646.                         # USB stick search for install media
  2647.                         # created with kiwi
  2648.                         # ----
  2649.                         if \
  2650.                             [ ! -e /mnt/config.isoclient ] && \
  2651.                             [ ! -e /mnt/config.usbclient ]
  2652.                         then
  2653.                             umountSystem
  2654.                             continue
  2655.                         fi
  2656.                     elif [ "$mode" = "kexec" ];then
  2657.                         # /.../
  2658.                         # USB stick search for hotfix media
  2659.                         # with kernel/initrd for later kexec
  2660.                         # ----
  2661.                         if \
  2662.                             [ ! -e /mnt/linux.kexec ] && \
  2663.                             [ ! -e /mnt/initrd.kexec ]
  2664.                         then
  2665.                             umountSystem
  2666.                             continue
  2667.                         fi
  2668.                     else
  2669.                         # /.../
  2670.                         # USB stick search for Linux system tree
  2671.                         # created with kiwi
  2672.                         # ----
  2673.                         if [ ! -e /mnt/etc/ImageVersion ]; then
  2674.                             umountSystem
  2675.                             continue
  2676.                         fi
  2677.                     fi
  2678.                     stickFound=1
  2679.                     umountSystem
  2680.                     break
  2681.                 done
  2682.                 if [ "$stickFound" = 0 ];then
  2683.                     continue
  2684.                 fi
  2685.                 stickSerial=$serial
  2686.                 return
  2687.             fi
  2688.         done
  2689.     done
  2690. }
  2691. #======================================
  2692. # CDMountOption
  2693. #--------------------------------------
  2694. function CDMountOption {
  2695.     # /.../
  2696.     # checks for the ISO 9660 extension and prints the
  2697.     # mount option required to mount the device in full
  2698.     # speed mode
  2699.     # ----
  2700.     local id=$(blkid -o value -s TYPE $1)
  2701.     if [ "$id" = "iso9660" ];then
  2702.         echo "-t iso9660"
  2703.     fi
  2704. }
  2705. #======================================
  2706. # CDMount
  2707. #--------------------------------------
  2708. function CDMount {
  2709.     # /.../
  2710.     # search all CD/DVD drives and use the one we can find
  2711.     # the CD configuration on
  2712.     # ----
  2713.     local count=0
  2714.     local ecode=0
  2715.     local cdopt
  2716.     mkdir -p /cdrom
  2717.     if [ -f /.profile ];then
  2718.         importFile < /.profile
  2719.     fi
  2720.     #======================================
  2721.     # check for hybrid mbr ID
  2722.     #--------------------------------------
  2723.     if [ -z "$cdinst" ];then
  2724.         searchBIOSBootDevice
  2725.         ecode=$?
  2726.         if [ ! $ecode = 0 ];then
  2727.             if [ $ecode = 2 ];then
  2728.                 systemException "$biosBootDevice" "reboot"
  2729.             fi
  2730.             unset kiwi_hybrid
  2731.         fi
  2732.     else
  2733.         unset kiwi_hybrid
  2734.     fi
  2735.     #======================================
  2736.     # walk through media
  2737.     #--------------------------------------
  2738.     if [ -z "$kiwi_hybrid" ];then
  2739.         #======================================
  2740.         # search for CD/DVD devices
  2741.         #--------------------------------------
  2742.         CDDevice
  2743.         Echo -n "Mounting live boot drive..."
  2744.         while true;do
  2745.             IFS=":"; for i in $cddev;do
  2746.                 cdopt=$(CDMountOption $i)
  2747.                 if [ -x /usr/bin/driveready ];then
  2748.                     driveready $i&& eval mount $cdopt -o ro $i /cdrom >/dev/null
  2749.                 else
  2750.                     eval mount $cdopt -o ro $i /cdrom >/dev/null
  2751.                 fi
  2752.                 if [ -f $LIVECD_CONFIG ];then
  2753.                     cddev=$i; echo
  2754.                     #======================================
  2755.                     # run mediacheck if requested and boot
  2756.                     #--------------------------------------
  2757.                     if [ "$mediacheck" = 1 ]; then
  2758.                         test -e /proc/splash && echo verbose > /proc/splash
  2759.                         checkmedia $cddev
  2760.                         Echo -n "Press ENTER for reboot: "; read nope
  2761.                         /sbin/reboot -f -i >/dev/null
  2762.                     fi
  2763.                     #======================================
  2764.                     # device found go with it
  2765.                     #--------------------------------------
  2766.                     IFS=$IFS_ORIG
  2767.                     return
  2768.                 fi
  2769.                 umount $i &>/dev/null
  2770.             done
  2771.             IFS=$IFS_ORIG
  2772.             if [ $count -eq 12 ]; then
  2773.                 break
  2774.             else
  2775.                 echo -n .
  2776.                 sleep 1
  2777.             fi
  2778.             count=`expr $count + 1`
  2779.         done
  2780.     else
  2781.         #======================================
  2782.         # search for hybrid device
  2783.         #--------------------------------------
  2784.         # /.../
  2785.         # we have to use fdisk here for partition manipulation
  2786.         # because parted doesn't accept the partition table written
  2787.         # by the isohybrid tool :(
  2788.         # ----
  2789.         PARTITIONER=sfdisk
  2790.         if [ "x$kiwi_hybridpersistent" = "xyes" ]; then
  2791.             createHybridPersistent $biosBootDevice
  2792.         fi
  2793.         Echo -n "Mounting hybrid live boot drive..."
  2794.         cddev=$(ddn $biosBootDevice 1)
  2795.         kiwiMount "$cddev" "/cdrom" "-o ro"
  2796.         if [ -f $LIVECD_CONFIG ];then
  2797.             echo
  2798.             #======================================
  2799.             # run mediacheck if requested and boot
  2800.             #--------------------------------------
  2801.             if [ "$mediacheck" = 1 ]; then
  2802.                 test -e /proc/splash && echo verbose > /proc/splash
  2803.                 checkmedia $cddev
  2804.                 Echo -n "Press ENTER for reboot: "; read nope
  2805.                 /sbin/reboot -f -i >/dev/null
  2806.             fi
  2807.             #======================================
  2808.             # search hybrid for a write partition
  2809.             #--------------------------------------
  2810.             for disknr in 2 3 4;do
  2811.                 id=`partitionID $biosBootDevice $disknr`
  2812.                 if [ "$id" = "83" ];then
  2813.                     export HYBRID_RW=$biosBootDevice$disknr
  2814.                     break
  2815.                 fi
  2816.             done
  2817.             #======================================
  2818.             # device found go with it
  2819.             #--------------------------------------
  2820.             PARTITIONER=parted
  2821.             return
  2822.         fi
  2823.         PARTITIONER=parted
  2824.         umount $cddev &>/dev/null
  2825.     fi
  2826.     echo
  2827.     systemException \
  2828.         "Couldn't find Live image configuration file" \
  2829.     "reboot"
  2830. }
  2831. #======================================
  2832. # CDUmount
  2833. #--------------------------------------
  2834. function CDUmount {
  2835.     # /.../
  2836.     # umount the CD device
  2837.     # ----
  2838.     umount $cddev
  2839. }
  2840. #======================================
  2841. # CDEject
  2842. #--------------------------------------
  2843. function CDEject {
  2844.     eject $cddev
  2845. }
  2846. #======================================
  2847. # searchBIOSBootDevice
  2848. #--------------------------------------
  2849. function searchBIOSBootDevice {
  2850.     # /.../
  2851.     # search for the BIOS boot device which is the device
  2852.     # with the BIOS id 0x80. The test may fail if the boot
  2853.     # device is a CD/DVD drive. If the test fails we search
  2854.     # for the MBR disk label and compare it with the kiwi
  2855.     # written mbrid file in /boot/grub/ of the system image
  2856.     # ----
  2857.     IFS=$IFS_ORIG
  2858.     local h=/usr/sbin/hwinfo
  2859.     local c="Device File:|BIOS id"
  2860.     local ddevs=`$h --disk|grep -E "$c"|sed -e"s@(.*)@@"|cut -f2 -d:|tr -d " "`
  2861.     local cmpd=/tmp/mbrids
  2862.     local ifix=0
  2863.     local matched
  2864.     local bios
  2865.     local file
  2866.     local pred
  2867.     #======================================
  2868.     # Store device with BIOS id 0x80
  2869.     #--------------------------------------
  2870.     for curd in $ddevs;do
  2871.         if [ $curd = "0x80" ];then
  2872.             bios=$pred; break
  2873.         fi
  2874.         pred=$curd
  2875.     done
  2876.     #======================================
  2877.     # Check for OEM ISO installation mode
  2878.     #--------------------------------------
  2879.     if [ ! -z "$cdinst" ];then
  2880.         CDMount
  2881.         umount $cddev
  2882.         curd=$cddev
  2883.         export biosBootDevice=$curd
  2884.         return 0
  2885.     fi
  2886.     #======================================
  2887.     # Search and copy all mbrid files
  2888.     #--------------------------------------
  2889.     mkdir -p $cmpd
  2890.     for curd in $ddevs;do
  2891.         if [ ! $(echo $curd | cut -c 1) = "/" ];then
  2892.             continue
  2893.         fi
  2894.         for id in 1 2 3;do
  2895.             dev=$(ddn $curd $id)
  2896.             if ! mount -o ro $dev /mnt;then
  2897.                 continue
  2898.             fi
  2899.             if [ -f /mnt/boot/grub/mbrid ];then
  2900.                 cp -a /mnt/boot/grub/mbrid $cmpd/mbrid$ifix
  2901.                 ifix=$(expr $ifix + 1)
  2902.                 umount /mnt
  2903.                 break
  2904.             fi
  2905.             umount /mnt
  2906.         done
  2907.     done
  2908.     #======================================
  2909.     # Read mbrid from the newest mbrid file
  2910.     #--------------------------------------
  2911.     file=$(ls -1t $cmpd 2>/dev/null | head -n 1)
  2912.     if [ -z "$file" ];then
  2913.         export biosBootDevice="Failed to find MBR identifier !"
  2914.         return 1
  2915.     fi
  2916.     read mbrI < $cmpd/$file
  2917.     #======================================
  2918.     # Compare ID with MBR entry
  2919.     #--------------------------------------
  2920.     ifix=0
  2921.     for curd in $ddevs;do
  2922.         if [ ! -b $curd ];then
  2923.             continue
  2924.         fi
  2925.         mbrML=`dd if=$curd bs=1 count=4 skip=$((0x1b8))|hexdump -n4 -e '"0x%x"'`
  2926.         mbrMB=`echo $mbrML | sed 's/^0x\(..\)\(..\)\(..\)\(..\)$/0x\4\3\2\1/'`
  2927.         if [ "$mbrML" = "$mbrI" ] || [ "$mbrMB" = "$mbrI" ];then
  2928.             ifix=1
  2929.             matched=$curd
  2930.             if [ "$curd" = "$bios" ];then
  2931.                 export biosBootDevice=$curd
  2932.                 return 0
  2933.             fi
  2934.         fi
  2935.     done
  2936.     if [ $ifix -eq 1 ];then
  2937.         export biosBootDevice=$matched
  2938.         return 0
  2939.     fi
  2940.     export biosBootDevice="No devices matches MBR identifier: $mbrI !"
  2941.     return 2
  2942. }
  2943. #======================================
  2944. # searchVolumeGroup
  2945. #--------------------------------------
  2946. function searchVolumeGroup {
  2947.     # /.../
  2948.     # search for a volume group named $VGROUP and if it can be
  2949.     # found activate it while creating appropriate device nodes:
  2950.     # /dev/$VGROUP/LVRoot and/or /dev/$VGROUP/LVComp
  2951.     # return zero on success
  2952.     # ----
  2953.     if vgscan 2>&1 | grep -q "$VGROUP"; then
  2954.         vgchange -a y $VGROUP
  2955.         return $?
  2956.     fi
  2957.     return 1
  2958. }
  2959. #======================================
  2960. # searchSwapSpace
  2961. #--------------------------------------
  2962. function searchSwapSpace {
  2963.     # /.../
  2964.     # search for a type=82 swap partition
  2965.     # ----
  2966.     if [ ! -z $kiwinoswapsearch ];then
  2967.         return
  2968.     fi
  2969.     local hwapp=/usr/sbin/hwinfo
  2970.     local diskdevs=`$hwapp --disk | grep "Device File:" | cut -f2 -d:`
  2971.     diskdevs=`echo $diskdevs | sed -e "s@(.*)@@"`
  2972.     for diskdev in $diskdevs;do
  2973.         for disknr in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15;do
  2974.             id=`partitionID $diskdev $disknr`
  2975.             if [ "$id" = "82" ];then
  2976.                 echo $diskdev$disknr
  2977.                 return
  2978.             fi
  2979.         done
  2980.     done
  2981. }
  2982. #======================================
  2983. # searchDiskSpace
  2984. #--------------------------------------
  2985. function searchDiskSpace {
  2986.     # /.../
  2987.     # search for a free non swap partition
  2988.     # ----
  2989.     if [ ! -z $kiwinoswapsearch ];then
  2990.         return
  2991.     fi
  2992.     local hwapp=/usr/sbin/hwinfo
  2993.     local diskdevs=`$hwapp --disk | grep "Device File:" | cut -f2 -d:`
  2994.     diskdevs=`echo $diskdevs | sed -e "s@(.*)@@"`
  2995.     for diskdev in $diskdevs;do
  2996.         for disknr in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15;do
  2997.             id=`partitionID $diskdev $disknr`
  2998.             if [ -z $id ];then
  2999.                 id=0
  3000.             fi
  3001.             if [ "$id" -ne 82 ] && [ "$id" -ne 0 ];then
  3002.                 echo $diskdev$disknr
  3003.                 return
  3004.             fi
  3005.         done
  3006.     done
  3007. }
  3008. #======================================
  3009. # updateMTAB
  3010. #--------------------------------------
  3011. function updateMTAB {
  3012.     prefix=$1
  3013.     umount=0
  3014.     if [ ! -e /proc/mounts ];then
  3015.         mount -t proc proc /proc
  3016.         umount=1
  3017.     fi
  3018.     cat /proc/mounts > $prefix/etc/mtab
  3019.     if [ $umount -eq 1 ];then
  3020.         umount /proc
  3021.     fi
  3022. }
  3023. #======================================
  3024. # probeNetworkCard
  3025. #--------------------------------------
  3026. function probeNetworkCard {
  3027.     # /.../
  3028.     # use hwinfo to probe for all network devices. The
  3029.     # function will check for the driver which is needed
  3030.     # to support the card and returns the information in
  3031.     # the networkModule variable
  3032.     # ----
  3033.     IFS="%"
  3034.     local module=""
  3035.     local hwicmd="/usr/sbin/hwinfo"
  3036.     for i in \
  3037.         `$hwicmd --netcard | grep "Driver [IA]" |
  3038.         sed -es"@modprobe\(.*\)\"@\1%@" | tr -d "\n"`
  3039.     do
  3040.         if echo $i | grep -q "#0";then
  3041.             module=`echo $i | cut -f2 -d"\"" | tr -d " "`
  3042.             module=`echo $module | sed -es"@modprobe@@g"`
  3043.             IFS=";"
  3044.             for m in $module;do
  3045.                 if ! echo $networkModule | grep -q $m;then
  3046.                     if [ ! -z "$networkModule" ];then
  3047.                         networkModule="$networkModule:$m"
  3048.                     else
  3049.                         networkModule=$m
  3050.                     fi
  3051.                 fi
  3052.             done
  3053.         fi
  3054.     done
  3055.     IFS=$IFS_ORIG
  3056.     networkModule=`echo $networkModule`
  3057. }
  3058. #======================================
  3059. # setupNetwork
  3060. #--------------------------------------
  3061. function setupNetwork {
  3062.     # /.../
  3063.     # probe for the existing network interface names and
  3064.     # hardware addresses. Match the BOOTIF address from PXE
  3065.     # to the correct linux interface name. Setup the network
  3066.     # interface using a dhcp request. On success the dhcp
  3067.     # info file is imported into the current shell environment
  3068.     # and the nameserver information is written to
  3069.     # /etc/resolv.conf
  3070.     # ----
  3071.     IFS="
  3072.     "
  3073.     local MAC=0
  3074.     local DEV=0
  3075.     local mac_list=0
  3076.     local dev_list=0
  3077.     local index=0
  3078.     local hwicmd=/usr/sbin/hwinfo
  3079.     local iface=eth0
  3080.     local opts="--noipv4ll -p"
  3081.     for i in `$hwicmd --netcard`;do
  3082.         IFS=$IFS_ORIG
  3083.         if echo $i | grep -q "HW Address:";then
  3084.             MAC=`echo $i | sed -e s"@HW Address:@@"`
  3085.             MAC=`echo $MAC`
  3086.             mac_list[$index]=$MAC
  3087.             index=`expr $index + 1`
  3088.         fi
  3089.         if echo $i | grep -q "Device File:";then
  3090.             DEV=`echo $i | sed -e s"@Device File:@@"`
  3091.             DEV=`echo $DEV`
  3092.             dev_list[$index]=$DEV
  3093.         fi
  3094.     done
  3095.     if [ -z $BOOTIF ];then
  3096.         # /.../
  3097.         # there is no PXE boot interface information. We will use
  3098.         # the first detected interface as fallback solution
  3099.         # ----
  3100.         iface=${dev_list[0]}
  3101.     else
  3102.         # /.../
  3103.         # evaluate the information from the PXE boot interface
  3104.         # if we found the MAC in the list the appropriate interface
  3105.         # name is assigned. if not eth0 is used as fallback
  3106.         # ----
  3107.         index=0
  3108.         BOOTIF=`echo $BOOTIF | cut -f2- -d - | tr "-" ":"`
  3109.         for i in ${mac_list[*]};do
  3110.             if [ $i = $BOOTIF ];then
  3111.                 iface=${dev_list[$index]}
  3112.             fi
  3113.             index=`expr $index + 1`
  3114.         done
  3115.     fi
  3116.     export PXE_IFACE=$iface
  3117.     if $DHCPCD_HAVE_PERSIST -eq 0;then
  3118.         # /.../
  3119.         # older version of dhcpd which doesn't have the
  3120.         # options we want to pass
  3121.         # ----
  3122.         unset opts
  3123.     fi
  3124.     if ! dhcpcd $opts $PXE_IFACE 1>&2;then
  3125.         systemException \
  3126.             "Failed to setup DHCP network interface !" \
  3127.         "reboot"
  3128.     fi
  3129.     for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20;do
  3130.         if [ -s /var/lib/dhcpcd/dhcpcd-$PXE_IFACE.info ];then
  3131.             break
  3132.         fi
  3133.         sleep 2
  3134.     done
  3135.     if [ ! -s /var/lib/dhcpcd/dhcpcd-$PXE_IFACE.info ];then
  3136.         systemException \
  3137.             "Can't find DHCP info file: dhcpcd-$PXE_IFACE.info !" \
  3138.         "reboot"
  3139.     fi
  3140.     ifconfig lo 127.0.0.1 netmask 255.0.0.0 up
  3141.     importFile < /var/lib/dhcpcd/dhcpcd-$PXE_IFACE.info
  3142.     if [ -z "$DOMAIN" ] && [ -n "$DNSDOMAIN" ];then
  3143.         export DOMAIN=$DNSDOMAIN
  3144.     fi
  3145.     echo "search $DOMAIN" > /etc/resolv.conf
  3146.     if [ -z "$DNS" ] && [ -n "$DNSSERVERS" ];then
  3147.         export DNS=$DNSSERVERS
  3148.     fi
  3149.     IFS="," ; for i in $DNS;do
  3150.         echo "nameserver $i" >> /etc/resolv.conf
  3151.     done
  3152.     DHCPCHADDR=`echo $DHCPCHADDR | tr a-z A-Z`
  3153. }
  3154. #======================================
  3155. # updateNeeded
  3156. #--------------------------------------
  3157. function updateNeeded {
  3158.     # /.../
  3159.     # check the contents of the IMAGE key and compare the
  3160.     # image version file as well as the md5 sum of the installed
  3161.     # and the available image on the tftp server
  3162.     # ----
  3163.     SYSTEM_INTEGRITY=""
  3164.     SYSTEM_MD5STATUS=""
  3165.     local count=0
  3166.     IFS="," ; for i in $IMAGE;do
  3167.         field=0
  3168.         IFS=";" ; for n in $i;do
  3169.         case $field in
  3170.             0) field=1 ;;
  3171.             1) imageName=$n   ; field=2 ;;
  3172.             2) imageVersion=$n; field=3 ;;
  3173.             3) imageServer=$n ; field=4 ;;
  3174.             4) imageBlkSize=$n; field=5 ;;
  3175.             5) imageZipped=$n ;
  3176.         esac
  3177.         done
  3178.         atversion="$imageName-$imageVersion"
  3179.         versionFile="/mnt/etc/ImageVersion-$atversion"
  3180.         IFS=" "
  3181.         if [ -f "$versionFile" ];then
  3182.             read installed sum2 < $versionFile
  3183.         fi
  3184.         imageMD5s="image/$imageName-$imageVersion.md5"
  3185.         [ -z "$imageServer" ]  && imageServer=$SERVER
  3186.         [ -z "$imageBlkSize" ] && imageBlkSize=8192
  3187.         if [ ! -f /etc/image.md5 ];then
  3188.             fetchFile $imageMD5s /etc/image.md5 uncomp $imageServer
  3189.         fi
  3190.         read sum1 blocks blocksize zblocks zblocksize < /etc/image.md5
  3191.         if [ ! -z "$sum1" ];then
  3192.             SYSTEM_MD5STATUS="$SYSTEM_MD5STATUS:$sum1"
  3193.         else
  3194.             SYSTEM_MD5STATUS="$SYSTEM_MD5STATUS:none"
  3195.         fi
  3196.         if [ ! -z "$1" ];then
  3197.             continue
  3198.         fi
  3199.         if test "$count" = 1;then
  3200.         if test "$SYSTEM_INTEGRITY" = ":clean";then
  3201.             Echo "Main OS image update needed"
  3202.             Echo -b "Forcing download for multi image session"
  3203.             RELOAD_IMAGE="yes"
  3204.         fi
  3205.         fi
  3206.         count=$(($count + 1))
  3207.         Echo "Checking update status for image: $imageName"
  3208.         if test ! -z $RELOAD_IMAGE;then
  3209.             Echo -b "Update forced via RELOAD_IMAGE..."
  3210.             Echo -b "Update status: Clean"
  3211.             SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
  3212.             continue
  3213.         fi
  3214.         if test ! -f $versionFile;then
  3215.             Echo -b "Update forced: /etc/ImageVersion-$atversion not found"
  3216.             Echo -b "Update status: Clean"
  3217.             SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
  3218.             RELOAD_IMAGE="yes"
  3219.             continue
  3220.         fi
  3221.         Echo -b "Current: $atversion Installed: $installed"
  3222.         if test "$atversion" = "$installed";then
  3223.             if test "$sum1" = "$sum2";then
  3224.                 Echo -b "Update status: Fine"
  3225.                 SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:fine"
  3226.                 continue
  3227.             fi
  3228.             Echo -b "Image Update for image [ $imageName ] needed"
  3229.             Echo -b "Image version equals but md5 checksum failed"
  3230.             Echo -b "This means the contents of the new image differ"
  3231.             RELOAD_IMAGE="yes"
  3232.         else
  3233.             Echo -b "Image Update for image [ $imageName ] needed"
  3234.             Echo -b "Name and/or image version differ"
  3235.             RELOAD_IMAGE="yes"
  3236.         fi
  3237.         Echo -b "Update status: Clean"
  3238.         SYSTEM_INTEGRITY="$SYSTEM_INTEGRITY:clean"
  3239.     done
  3240.     SYSTEM_INTEGRITY=`echo $SYSTEM_INTEGRITY | cut -f2- -d:`
  3241.     SYSTEM_MD5STATUS=`echo $SYSTEM_MD5STATUS | cut -f2- -d:`
  3242. }
  3243. #======================================
  3244. # cleanSweep
  3245. #--------------------------------------
  3246. function cleanSweep {
  3247.     # /.../
  3248.     # zero out a the given disk device
  3249.     # ----
  3250.     local diskDevice=$1
  3251.     dd if=/dev/zero of=$diskDevice bs=32M >/dev/null
  3252. }
  3253. #======================================
  3254. # sfdiskGetPartitionID
  3255. #--------------------------------------
  3256. function sfdiskGetPartitionID {
  3257.     # /.../
  3258.     # prints the partition ID for the given device and number
  3259.     # ----
  3260.     sfdisk -c $1 $2
  3261. }
  3262. #======================================
  3263. # partedGetPartitionID
  3264. #--------------------------------------
  3265. function partedGetPartitionID {
  3266.     # /.../
  3267.     # prints the partition ID for the given device and number
  3268.     # ----
  3269.     parted -m -s $1 print | grep ^$2: | cut -f2 -d, |\
  3270.         cut -f2 -d= | tr -d ";" | tr -d 0
  3271. }
  3272. #======================================
  3273. # partitionID
  3274. #--------------------------------------
  3275. function partitionID {
  3276.     local diskDevice=$1
  3277.     local diskNumber=$2
  3278.     if [ $PARTITIONER = "sfdisk" ];then
  3279.         sfdiskGetPartitionID $diskDevice $diskNumber
  3280.     else
  3281.         partedGetPartitionID $diskDevice $diskNumber
  3282.     fi
  3283. }
  3284. #======================================
  3285. # partitionSize
  3286. #--------------------------------------
  3287. function partitionSize {
  3288.     local diskDevice=$1
  3289.     if [ -z "$diskDevice" ] || [ ! -e "$diskDevice" ];then
  3290.         return 1
  3291.     fi
  3292.     expr $(blockdev --getsize64 $diskDevice) / 1024
  3293. }
  3294. #======================================
  3295. # linuxPartition
  3296. #--------------------------------------
  3297. function linuxPartition {
  3298.     # /.../
  3299.     # check for a linux partition on partition number 2
  3300.     # using the given disk device. On success return 0
  3301.     # ----
  3302.     local diskDevice=$1
  3303.     local diskPartitionType=`partitionID $diskDevice 2`
  3304.     if test "$diskPartitionType" = "83";then
  3305.         return 0
  3306.     fi
  3307.     return 1
  3308. }
  3309. #======================================
  3310. # kernelList
  3311. #--------------------------------------
  3312. function kernelList {
  3313.     # /.../
  3314.     # check for all installed kernels whether there are valid
  3315.     # links to the initrd and kernel files. The function will
  3316.     # save the valid linknames in the variable KERNEL_LIST
  3317.     # ----
  3318.     local prefix=$1
  3319.     local kcount=1
  3320.     local kname=""
  3321.     local kernel=""
  3322.     local initrd=""
  3323.     local kpair=""
  3324.     local krunning=`uname -r`
  3325.     KERNEL_LIST=""
  3326.     KERNEL_NAME=""
  3327.     KERNEL_PAIR=""
  3328.     #======================================
  3329.     # search running kernel first
  3330.     #--------------------------------------
  3331.     if [ -d $prefix/lib/modules/$krunning ];then
  3332.         for name in vmlinux vmlinuz image;do
  3333.             if [ -f $prefix/boot/$name-$krunning ];then
  3334.                 kernel=$name-$krunning
  3335.                 initrd=initrd-$krunning
  3336.                 break
  3337.             fi
  3338.         done
  3339.         if [ ! -z "$kernel" ];then
  3340.             KERNEL_PAIR=$kernel:$initrd
  3341.             KERNEL_NAME[$kcount]=$krunning
  3342.             KERNEL_LIST=$KERNEL_PAIR
  3343.         fi
  3344.     fi
  3345.     #======================================
  3346.     # search for other kernels
  3347.     #--------------------------------------
  3348.     if [ ! -z "$KERNEL_LIST" ];then
  3349.         for i in $prefix/lib/modules/*;do
  3350.             if [ ! -d $i ];then
  3351.                 continue
  3352.             fi
  3353.             unset kernel
  3354.             unset initrd
  3355.             kname=`basename $i`
  3356.             if [ "$kname" = $krunning ];then
  3357.                 continue
  3358.             fi
  3359.             for name in vmlinux vmlinuz image;do
  3360.             for k in $prefix/boot/$name-${i##*/}; do
  3361.                 if [ -f $k ];then
  3362.                     kernel=${k##*/}
  3363.                     initrd=initrd-${i##*/}
  3364.                     break 2
  3365.                 fi
  3366.             done
  3367.             done
  3368.             if [ -z "$kernel" ];then
  3369.                 continue
  3370.             fi
  3371.             kcount=$((kcount+1))
  3372.             kpair=$kernel:$initrd
  3373.             KERNEL_NAME[$kcount]=$kname
  3374.             KERNEL_LIST=$KERNEL_LIST,$kpair
  3375.         done
  3376.     fi
  3377.     if [ -z "$KERNEL_LIST" ];then
  3378.         # /.../
  3379.         # the system image doesn't provide the kernel and initrd but
  3380.         # if there is a downloaded kernel and initrd from the KIWI_INITRD
  3381.         # setup. the kernelList function won't find initrds that gets
  3382.         # downloaded over tftp so make sure the vmlinu[zx]/initrd combo
  3383.         # gets added
  3384.         # ----
  3385.         if [ -e $prefix/boot/vmlinuz ];then
  3386.             KERNEL_LIST="vmlinuz:initrd"
  3387.             KERNEL_NAME[1]=vmlinuz
  3388.         fi
  3389.         if [ -e $prefix/boot/vmlinux ];then
  3390.             KERNEL_LIST="vmlinux:initrd"
  3391.             KERNEL_NAME[1]=vmlinux
  3392.         fi
  3393.     fi
  3394.     export KERNEL_LIST
  3395.     export KERNEL_NAME
  3396.     export KERNEL_PAIR
  3397. }
  3398. #======================================
  3399. # validateSize
  3400. #--------------------------------------
  3401. function validateSize {
  3402.     # /.../
  3403.     # check if the image fits into the requested partition.
  3404.     # An information about the sizes is printed out
  3405.     # ----
  3406.     haveBytes=`partitionSize $imageDevice`
  3407.     haveBytes=`expr $haveBytes \* 1024`
  3408.     haveMByte=`expr $haveBytes / 1048576`
  3409.     needBytes=`expr $blocks \* $blocksize`
  3410.     needMByte=`expr $needBytes / 1048576`
  3411.     Echo "Have size: $imageDevice -> $haveBytes Bytes [ $haveMByte MB ]"
  3412.     Echo "Need size: $needBytes Bytes [ $needMByte MB ]"
  3413.     if test $haveBytes -gt $needBytes;then
  3414.         return 0
  3415.     fi
  3416.     return 1
  3417. }
  3418. #======================================
  3419. # validateTarSize
  3420. #--------------------------------------
  3421. function validateTarSize {
  3422.     # /.../
  3423.     # this function requires a destination directory which
  3424.     # could be a tmpfs mount and a compressed tar source file.
  3425.     # The function will then check if the tar file could be
  3426.     # unpacked according to the size of the destination
  3427.     # ----
  3428.     local tsrc=$1
  3429.     local haveKByte=0
  3430.     local haveMByte=0
  3431.     local needBytes=0
  3432.     local needMByte=0
  3433.     haveKByte=`cat /proc/meminfo | grep MemFree | cut -f2 -d: | cut -f1 -dk`
  3434.     haveMByte=`expr $haveKByte / 1024`
  3435.     needBytes=`du --bytes $tsrc | cut -f1`
  3436.     needMByte=`expr $needBytes / 1048576`
  3437.     Echo "Have size: proc/meminfo -> $haveMByte MB"
  3438.     Echo "Need size: $tsrc -> $needMByte MB [ uncompressed ]"
  3439.     if [ "$haveMByte" -gt "$needMByte" ];then
  3440.         return 0
  3441.     fi
  3442.     return 1
  3443. }
  3444. #======================================
  3445. # validateBlockSize
  3446. #--------------------------------------
  3447. function validateBlockSize {
  3448.     # /.../
  3449.     # check the block size value. atftp limits to a maximum of
  3450.     # 65535 blocks, so the block size must be checked according
  3451.     # to the size of the image. The block size itself is also
  3452.     # limited to 65464 bytes
  3453.     # ----
  3454.     if [ -z "$zblocks" ] && [ -z "$blocks" ];then
  3455.         # md5 file not yet read in... skip
  3456.         return
  3457.     fi
  3458.     if [ ! -z "$zblocks" ];then
  3459.         isize=`expr $zblocks \* $zblocksize`
  3460.     else
  3461.         isize=`expr $blocks \* $blocksize`
  3462.     fi
  3463.     isize=`expr $isize / 65535`
  3464.     if [ $isize -gt $imageBlkSize ];then
  3465.         imageBlkSize=`expr $isize + 1024`
  3466.     fi
  3467.     if [ $imageBlkSize -gt 65464 ];then
  3468.         systemException \
  3469.             "Maximum blocksize for atftp protocol exceeded" \
  3470.         "reboot"
  3471.     fi
  3472. }
  3473. #======================================
  3474. # loadOK
  3475. #--------------------------------------
  3476. function loadOK {
  3477.     # /.../
  3478.     # check the output of the atftp command, unfortunately
  3479.     # there is no useful return code to check so we have to
  3480.     # check the output of the command
  3481.     # ----
  3482.     for i in "File not found" "aborting" "no option named" "unknown host" ; do
  3483.         if echo "$1" | grep -q  "$i" ; then
  3484.             return 1
  3485.         fi
  3486.     done
  3487.     return 0
  3488. }
  3489. #======================================
  3490. # includeKernelParameters
  3491. #--------------------------------------
  3492. function includeKernelParameters {
  3493.     # /.../
  3494.     # include the parameters from /proc/cmdline into
  3495.     # the current shell environment
  3496.     # ----
  3497.     IFS=$IFS_ORIG
  3498.     for i in `cat /proc/cmdline`;do
  3499.         if ! echo $i | grep -q "=";then
  3500.             continue
  3501.         fi
  3502.         kernelKey=`echo $i | cut -f1 -d=`
  3503.         kernelVal=`echo $i | cut -f2 -d=`
  3504.         eval export $kernelKey=$kernelVal
  3505.     done
  3506.     if [ ! -z "$kiwikernelmodule" ];then
  3507.         kiwikernelmodule=`echo $kiwikernelmodule | tr , " "`
  3508.     fi
  3509.     if [ ! -z "$kiwibrokenmodule" ];then
  3510.         kiwibrokenmodule=`echo $kiwibrokenmodule | tr , " "`
  3511.     fi
  3512.     if [ ! -z "$kiwistderr" ];then
  3513.         export ELOG_CONSOLE=$kiwistderr
  3514.         export ELOG_EXCEPTION=$kiwistderr
  3515.     fi
  3516.     if [ ! -z "$ramdisk_size" ];then
  3517.         local modfile=/etc/modprobe.conf.local
  3518.         if [ -f $modfile ];then
  3519.             sed -i -e s"@rd_size=.*@rd_size=$ramdisk_size@" $modfile
  3520.         fi
  3521.     fi
  3522.     if [ ! -z "$lang" ];then
  3523.         export DIALOG_LANG=$lang
  3524.     fi
  3525. }
  3526. #======================================
  3527. # checkServer
  3528. #--------------------------------------
  3529. function checkServer {
  3530.     # /.../
  3531.     # check the kernel commandline parameter kiwiserver.
  3532.     # If it exists its contents will be used as
  3533.     # server address stored in the SERVER variabe
  3534.     # ----
  3535.     if [ ! -z $kiwiserver ];then
  3536.         Echo "Found server in kernel cmdline"
  3537.         SERVER=$kiwiserver
  3538.     fi
  3539.     if [ ! -z $kiwiservertype ]; then
  3540.         Echo "Found server type in kernel cmdline"
  3541.         SERVERTYPE=$kiwiservertype
  3542.     else
  3543.         SERVERTYPE=tftp
  3544.     fi
  3545. }
  3546. #======================================
  3547. # umountSystem
  3548. #--------------------------------------
  3549. function umountSystem {
  3550.     local retval=0
  3551.     local OLDIFS=$IFS
  3552.     local mountList="/mnt /read-only /read-write"
  3553.     IFS=$IFS_ORIG
  3554.     #======================================
  3555.     # umount boot device
  3556.     #--------------------------------------
  3557.     if [ ! -z "$imageBootDevice" ];then
  3558.         umount $imageBootDevice 1>&2
  3559.     fi
  3560.     #======================================
  3561.     # umount mounted mountList paths
  3562.     #--------------------------------------
  3563.     for mpath in $(cat /proc/mounts | cut -f2 -d " ");do
  3564.         for umount in $mountList;do
  3565.             if [ "$mpath" = "$umount" ];then
  3566.                 if ! umount $mpath >/dev/null;then
  3567.                 if ! umount -l $mpath >/dev/null;then
  3568.                     retval=1
  3569.                 fi
  3570.                 fi
  3571.             fi
  3572.         done
  3573.     done
  3574.     #======================================
  3575.     # remove mount points
  3576.     #--------------------------------------
  3577.     for dir in "/read-only" "/read-write" "/xino";do
  3578.         test -d $dir && rmdir $dir 1>&2
  3579.     done
  3580.     IFS=$OLDIFS
  3581.     return $retval
  3582. }
  3583. #======================================
  3584. # isFSTypeReadOnly
  3585. #--------------------------------------
  3586. function isFSTypeReadOnly {
  3587.     if [ "$FSTYPE" = "squashfs" ];then
  3588.         export unionFST=aufs
  3589.         return 0
  3590.     fi
  3591.     if [ "$FSTYPE" = "clicfs" ];then
  3592.         export unionFST=clicfs
  3593.         return 0
  3594.     fi
  3595.     return 1
  3596. }
  3597. #======================================
  3598. # kiwiMount
  3599. #--------------------------------------
  3600. function kiwiMount {
  3601.     local src=$1
  3602.     local dst=$2
  3603.     local opt=$3
  3604.     local lop=$4
  3605.     #======================================
  3606.     # load not autoloadable fs modules
  3607.     #--------------------------------------
  3608.     modprobe squashfs &>/dev/null
  3609.     #======================================
  3610.     # store old FSTYPE value
  3611.     #--------------------------------------
  3612.     if [ ! -z "$FSTYPE" ];then
  3613.         FSTYPE_SAVE=$FSTYPE
  3614.     fi
  3615.     #======================================
  3616.     # probe filesystem
  3617.     #--------------------------------------
  3618.     if [ ! "$FSTYPE" = "nfs" ];then
  3619.         if [ ! -z "$lop" ];then
  3620.             probeFileSystem $lop
  3621.         else
  3622.             probeFileSystem $src
  3623.         fi
  3624.     fi
  3625.     if [ -z "$FSTYPE" ] || [ "$FSTYPE" = "unknown" ];then
  3626.         FSTYPE="auto"
  3627.     fi
  3628.     #======================================
  3629.     # decide for a mount method
  3630.     #--------------------------------------
  3631.     if [ ! -z "$lop" ];then
  3632.         # /.../
  3633.         # if loop mount is requested a fixed loop1 device
  3634.         # was set as src parameter. Because this fixed loop
  3635.         # name is used later too we stick to this device name
  3636.         # ----
  3637.         losetup /dev/loop1 $lop
  3638.     fi
  3639.     if ! mount -t $FSTYPE $opt $src $dst >/dev/null;then
  3640.         return 1
  3641.     fi
  3642.     if [ ! -z "$FSTYPE_SAVE" ];then
  3643.         FSTYPE=$FSTYPE_SAVE
  3644.     fi
  3645.     return 0
  3646. }
  3647. #======================================
  3648. # setupReadWrite
  3649. #--------------------------------------
  3650. function setupReadWrite {
  3651.     # /.../
  3652.     # check/create read-write filesystem used for
  3653.     # overlay data
  3654.     # ----
  3655.     local rwDir=/read-write
  3656.     local rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
  3657.     mkdir -p $rwDir
  3658.     if [ $LOCAL_BOOT = "no" ] && [ $systemIntegrity = "clean" ];then
  3659.         if [ "$RELOAD_IMAGE" = "yes" ] || \
  3660.             ! mount -o ro $rwDevice $rwDir >/dev/null
  3661.         then
  3662.             #======================================
  3663.             # store old FSTYPE value
  3664.             #--------------------------------------
  3665.             if [ ! -z "$FSTYPE" ];then
  3666.                 FSTYPE_SAVE=$FSTYPE
  3667.             fi
  3668.             #======================================
  3669.             # probe filesystem
  3670.             #--------------------------------------
  3671.             probeFileSystem $rwDevice
  3672.             if [ ! "$FSTYPE" = "unknown" ];then
  3673.                 Echo "Checking filesystem for RW data on $rwDevice..."
  3674.                 e2fsck -p $rwDevice
  3675.             fi
  3676.             #======================================
  3677.             # restore FSTYPE
  3678.             #--------------------------------------
  3679.             if [ ! -z "$FSTYPE_SAVE" ];then
  3680.                 FSTYPE=$FSTYPE_SAVE
  3681.             fi
  3682.             if [ "$RELOAD_IMAGE" = "yes" ] || \
  3683.                 ! mount -o ro $rwDevice $rwDir >/dev/null
  3684.             then
  3685.                 Echo "Creating filesystem for RW data on $rwDevice..."
  3686.                 if ! mke2fs -F -T ext3 -j $rwDevice >/dev/null;then
  3687.                     Echo "Failed to create ext3 filesystem"
  3688.                     return 1
  3689.                 fi
  3690.                 e2fsck -p $rwDevice >/dev/null
  3691.             fi
  3692.         else
  3693.             umount $rwDevice
  3694.         fi
  3695.     fi
  3696.     return 0
  3697. }
  3698. #======================================
  3699. # mountSystemUnified
  3700. #--------------------------------------
  3701. function mountSystemUnified {
  3702.     local loopf=$1
  3703.     local roDir=/read-only
  3704.     local roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
  3705.     #======================================
  3706.     # create read only mount point
  3707.     #--------------------------------------
  3708.     mkdir -p $roDir
  3709.     #======================================
  3710.     # check read/only device location
  3711.     #--------------------------------------
  3712.     if [ ! -z "$NFSROOT" ];then
  3713.         roDevice="$imageRootDevice"
  3714.     fi
  3715.     #======================================
  3716.     # mount read only device
  3717.     #--------------------------------------
  3718.     if ! kiwiMount "$roDevice" "$roDir" "" $loopf;then
  3719.         Echo "Failed to mount read only filesystem"
  3720.         return 1
  3721.     fi
  3722.     #======================================
  3723.     # check union mount method
  3724.     #--------------------------------------
  3725.     mountSystemOverlay
  3726. }
  3727. #======================================
  3728. # mountSystemClicFS
  3729. #--------------------------------------
  3730. function mountSystemClicFS {
  3731.     local loopf=$1
  3732.     local roDir=/read-only
  3733.     local rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
  3734.     local roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
  3735.     local clic_cmd=clicfs
  3736.     local resetReadWrite=0
  3737.     local haveBytes
  3738.     local haveKByte
  3739.     local haveMByte
  3740.     local wantCowFS
  3741.     local size
  3742.     #======================================
  3743.     # load fuse module
  3744.     #--------------------------------------
  3745.     modprobe fuse &>/dev/null
  3746.     #======================================
  3747.     # create read only mount point
  3748.     #--------------------------------------
  3749.     mkdir -p $roDir
  3750.     #======================================
  3751.     # check read/only device location
  3752.     #--------------------------------------
  3753.     if [ ! -z "$NFSROOT" ];then
  3754.         roDevice="$imageRootDevice"
  3755.     fi
  3756.     #======================================  
  3757.     # check kernel command line for log file  
  3758.     #--------------------------------------  
  3759.     if [ -n "$cliclog" ]; then  
  3760.         clic_cmd="$clic_cmd -l $cliclog"  
  3761.     fi  
  3762.     #======================================
  3763.     # check read/write device location
  3764.     #--------------------------------------
  3765.     getDiskDevice $rwDevice | grep -q ram
  3766.     if [ $? = 0 ];then
  3767.         haveKByte=`cat /proc/meminfo | grep MemFree | cut -f2 -d:| cut -f1 -dk`
  3768.         haveMByte=`expr $haveKByte / 1024`
  3769.         haveMByte=`expr $haveMByte \* 7 / 10`
  3770.         clic_cmd="$clic_cmd -m $haveMByte"
  3771.     else
  3772.         haveBytes=`blockdev --getsize64 $rwDevice`
  3773.         haveMByte=`expr $haveBytes / 1024 / 1024`
  3774.         wantCowFS=0
  3775.         if \
  3776.             [ "x$kiwi_hybrid" = "xyes" ] &&
  3777.             [ "x$kiwi_hybridpersistent" = "xyes" ]
  3778.         then
  3779.             # write into a cow file on a filesystem, for hybrid iso's
  3780.             wantCowFS=1
  3781.         fi
  3782.         if [ $wantCowFS = 1 ];then
  3783.             # write into a cow file on a filesystem
  3784.             mkdir -p $HYBRID_PERSISTENT_DIR
  3785.             if [ $LOCAL_BOOT = "no" ] && [ $systemIntegrity = "clean" ];then
  3786.                 resetReadWrite=1
  3787.             elif ! mount $rwDevice $HYBRID_PERSISTENT_DIR;then
  3788.                 resetReadWrite=1
  3789.             fi
  3790.             if [ $resetReadWrite = 1 ];then
  3791.                 if ! setupReadWrite; then
  3792.                     Echo "Failed to setup read-write filesystem"
  3793.                     return 1
  3794.                 fi
  3795.                 if ! mount $rwDevice $HYBRID_PERSISTENT_DIR;then
  3796.                     Echo "Failed to mount read/write filesystem"
  3797.                     return 1
  3798.                 fi
  3799.             fi
  3800.             clic_cmd="$clic_cmd -m $haveMByte"
  3801.             clic_cmd="$clic_cmd -c $HYBRID_PERSISTENT_DIR/.clicfs_COW"
  3802.             clic_cmd="$clic_cmd --ignore-cow-errors"
  3803.         else
  3804.             # write into a device directly
  3805.             clic_cmd="$clic_cmd -m $haveMByte -c $rwDevice --ignore-cow-errors"
  3806.         fi
  3807.     fi
  3808.     #======================================
  3809.     # mount/check clic file
  3810.     #--------------------------------------
  3811.     if [ ! -z "$NFSROOT" ];then
  3812.         #======================================
  3813.         # clic exported via NFS
  3814.         #--------------------------------------
  3815.         if ! kiwiMount "$roDevice" "$roDir" "" $loopf;then
  3816.             Echo "Failed to mount NFS filesystem"
  3817.             return 1
  3818.         fi
  3819.         if [ ! -e "$roDir/fsdata.ext3" ];then
  3820.             Echo "Can't find clic fsdata.ext3 in NFS export"
  3821.             return 1
  3822.         fi
  3823.     else
  3824.         #======================================
  3825.         # mount clic container
  3826.         #--------------------------------------
  3827.         if [ -z "$loopf" ];then
  3828.             loopf=$roDevice
  3829.         fi
  3830.         if ! $clic_cmd $loopf $roDir; then  
  3831.             Echo "Failed to mount clic filesystem"
  3832.             return 1
  3833.         fi
  3834.     fi
  3835.     #======================================
  3836.     # mount root over clic
  3837.     #--------------------------------------
  3838.     size=`stat -c %s $roDir/fsdata.ext3`
  3839.     size=$((size/512))
  3840.     # we don't want reserved blocks...
  3841.     tune2fs -m 0 $roDir/fsdata.ext3 >/dev/null
  3842.     # we don't want automatic filesystem check...
  3843.     tune2fs -i 0 $roDir/fsdata.ext3 >/dev/null
  3844.     if [ ! $LOCAL_BOOT = "no" ];then
  3845.         e2fsck -p $roDir/fsdata.ext3
  3846.     fi
  3847.     if [ $LOCAL_BOOT = "no" ];then
  3848.         resize2fs $roDir/fsdata.ext3 $size"s"
  3849.     fi
  3850.     mount -o loop,noatime,nodiratime,errors=remount-ro,barrier=0 \
  3851.         $roDir/fsdata.ext3 /mnt
  3852.     if [ ! $? = 0 ];then
  3853.         Echo "Failed to mount ext3 clic container"
  3854.         return 1
  3855.     fi
  3856.     export haveClicFS=yes
  3857.     return 0
  3858. }
  3859. #======================================
  3860. # mountSystemOverlay
  3861. #--------------------------------------
  3862. function mountSystemOverlay {
  3863.     local roDir=/read-only
  3864.     local rwDir=/read-write
  3865.     local xiDir=/xino
  3866.     local rwDevice=`echo $UNIONFS_CONFIG | cut -d , -f 1`
  3867.     local roDevice=`echo $UNIONFS_CONFIG | cut -d , -f 2`
  3868.     local unionFST=`echo $UNIONFS_CONFIG | cut -d , -f 3`
  3869.     #======================================
  3870.     # create read write mount points
  3871.     #--------------------------------------
  3872.     for dir in $rwDir $xiDir;do
  3873.         mkdir -p $dir
  3874.     done
  3875.     #======================================
  3876.     # check read/write device location
  3877.     #--------------------------------------
  3878.     getDiskDevice $rwDevice | grep -q ram
  3879.     if [ $? = 0 ];then
  3880.         # /.../
  3881.         # write part is a ram location, use tmpfs for ram
  3882.         # disk data storage
  3883.         # ----
  3884.         if ! mount -t tmpfs tmpfs $rwDir >/dev/null;then
  3885.             return 1
  3886.         fi
  3887.     else
  3888.         # /.../
  3889.         # write part is not a ram disk, create/check ext3 filesystem
  3890.         # on it if not remote. Mount the filesystem
  3891.         # ----
  3892.         if [ "$roDevice" = "nfs" ];then
  3893.             rwDevice="-o nolock,rw $nfsRootServer:$rwDevice"
  3894.         fi
  3895.         if [ ! "$roDevice" = "nfs" ] && ! setupReadWrite; then
  3896.             return 1
  3897.         fi
  3898.         if ! mount $rwDevice $rwDir >/dev/null;then
  3899.             Echo "Failed to mount read/write filesystem"
  3900.             return 1
  3901.         fi
  3902.     fi
  3903.     #======================================
  3904.     # setup overlay mount
  3905.     #--------------------------------------
  3906.     if [ $unionFST = "aufs" ];then
  3907.         mount -t tmpfs tmpfs $xiDir >/dev/null || retval=1
  3908.         mount -t aufs \
  3909.             -o dirs=$rwDir=rw:$roDir=ro,xino=$xiDir/.aufs.xino aufs /mnt \
  3910.         >/dev/null || return 1
  3911.     else
  3912.         mount -t unionfs \
  3913.             -o dirs=$rwDir=rw:$roDir=ro unionfs /mnt
  3914.         >/dev/null || return 1
  3915.     fi
  3916.     usleep 500000
  3917.     return 0
  3918. }
  3919. #======================================
  3920. # mountSystemCombined
  3921. #--------------------------------------
  3922. function mountSystemCombined {
  3923.     local mountDevice=$1
  3924.     local loopf=$2
  3925.     local roDevice=$mountDevice
  3926.     if [ "$haveLVM" = "yes" ]; then
  3927.         local rwDevice="/dev/$VGROUP/LVRoot"
  3928.     elif [ "$haveLuks" = "yes" ]; then
  3929.         local rwDevice="/dev/mapper/luksReadWrite"
  3930.     else
  3931.         local rwDevice=`getNextPartition $mountDevice`
  3932.     fi
  3933.     mkdir /read-only >/dev/null
  3934.     # /.../
  3935.     # mount the read-only partition to /read-only and use
  3936.     # mount option -o ro for this filesystem
  3937.     # ----
  3938.     if ! kiwiMount "$roDevice" "/read-only" "" $loopf;then
  3939.         Echo "Failed to mount read only filesystem"
  3940.         return 1
  3941.     fi
  3942.     # /.../
  3943.     # mount a tmpfs as /mnt which will become the root fs (/) later on
  3944.     # and extract the rootfs tarball with the RAM data and the read-only
  3945.     # and read-write links into the tmpfs.
  3946.     # ----
  3947.     local rootfs=/read-only/rootfs.tar
  3948.     if [ ! -f $rootfs ];then
  3949.         Echo "Can't find rootfs tarball"
  3950.         umount "$roDevice" &>/dev/null
  3951.         return 1
  3952.     fi
  3953.     # /.../
  3954.     # count inode numbers for files in rootfs tarball
  3955.     # ----
  3956.     local inr=`tar -tf $rootfs | wc -l`
  3957.     inr=`expr $inr \* 11 / 10 / 1024`
  3958.     inr=$inr"k"
  3959.     # /.../
  3960.     # mount tmpfs, reserve max 512MB for the rootfs data
  3961.     # ----
  3962.     if ! mount -t tmpfs tmpfs -o size=512M,nr_inodes=$inr /mnt;then
  3963.         systemException \
  3964.             "Failed to mount root tmpfs" \
  3965.         "reboot"
  3966.     fi
  3967.     if ! validateTarSize $rootfs;then
  3968.         systemException \
  3969.             "Not enough RAM space available for temporary data" \
  3970.         "reboot"
  3971.     fi
  3972.     cd /mnt && tar xf $rootfs >/dev/null && cd /
  3973.     # /.../
  3974.     # create a /mnt/read-only mount point and move the /read-only
  3975.     # mount into the /mnt root tree. After that remove the /read-only
  3976.     # directory and create a link to /mnt/read-only instead
  3977.     # /read-only -> /mnt/read-only
  3978.     # ----
  3979.     mkdir /mnt/read-only >/dev/null
  3980.     mount --move /read-only /mnt/read-only >/dev/null
  3981.     rm -rf /read-only >/dev/null
  3982.     ln -s /mnt/read-only /read-only >/dev/null || return 1
  3983.     if ! echo $rwDevice | grep -q loop;then
  3984.         if partitionSize $rwDevice &>/dev/null;then
  3985.             # /.../
  3986.             # mount the read-write partition to /mnt/read-write and create
  3987.             # a link to it: /read-write -> /mnt/read-write
  3988.             # ----
  3989.             mkdir /mnt/read-write >/dev/null
  3990.             mount $rwDevice /mnt/read-write >/dev/null
  3991.             rm -rf /read-write >/dev/null
  3992.             ln -s /mnt/read-write /read-write >/dev/null
  3993.         fi
  3994.     fi
  3995. }
  3996. #======================================
  3997. # mountSystemStandard
  3998. #--------------------------------------
  3999. function mountSystemStandard {
  4000.     local mountDevice=$1
  4001.     if [ "$FSTYPE" = "btrfs" ];then
  4002.         export haveBtrFS=yes
  4003.     fi
  4004.     if [ ! -z $FSTYPE ]          &&
  4005.        [ ! $FSTYPE = "unknown" ] &&
  4006.        [ ! $FSTYPE = "auto" ]
  4007.     then
  4008.         kiwiMount "$mountDevice" "/mnt"
  4009.     else
  4010.         mount $mountDevice /mnt >/dev/null
  4011.     fi
  4012.     if [ "$haveLVM" = "yes" ];then
  4013.         for i in /dev/$VGROUP/LV*;do
  4014.             local volume=$(echo $i | cut -f4 -d/ | cut -c3-)
  4015.             local mpoint=$(echo $volume | tr _ /)
  4016.             if \
  4017.                 [ ! $volume = "Root" ] && \
  4018.                 [ ! $volume = "Comp" ] && \
  4019.                 [ ! $volume = "Swap" ]
  4020.             then
  4021.                 mkdir -p /mnt/$mpoint
  4022.                 mount /dev/$VGROUP/LV$volume /mnt/$mpoint
  4023.             fi
  4024.         done
  4025.     fi
  4026.     return $?
  4027. }
  4028. #======================================
  4029. # mountSystem
  4030. #--------------------------------------
  4031. function mountSystem {
  4032.     local retval=0
  4033.     local OLDIFS=$IFS
  4034.     IFS=$IFS_ORIG
  4035.     #======================================
  4036.     # set primary mount device
  4037.     #--------------------------------------
  4038.     local mountDevice="$imageRootDevice"
  4039.     if [ ! -z "$1" ];then
  4040.         mountDevice="$1"
  4041.     fi
  4042.     #======================================
  4043.     # wait for storage device to appear
  4044.     #--------------------------------------
  4045.     if ! echo $mountDevice | grep -qE "loop|nolock";then
  4046.         waitForStorageDevice $mountDevice
  4047.     fi
  4048.     #======================================
  4049.     # check root tree type
  4050.     #--------------------------------------
  4051.     if [ ! -z "$COMBINED_IMAGE" ];then
  4052.         mountSystemCombined "$mountDevice" $2
  4053.         retval=$?
  4054.     elif [ ! -z "$UNIONFS_CONFIG" ];then
  4055.         local unionFST=`echo $UNIONFS_CONFIG | cut -d , -f 3`
  4056.         if [ "$unionFST" = "clicfs" ];then
  4057.             mountSystemClicFS $2
  4058.         else
  4059.             mountSystemUnified $2
  4060.         fi
  4061.         retval=$?
  4062.     else
  4063.         mountSystemStandard "$mountDevice"
  4064.         retval=$?
  4065.     fi
  4066.     #======================================
  4067.     # setup boot partition
  4068.     #--------------------------------------
  4069.     if [ $retval = 0 ] && [ -z "$RESTORE" ];then
  4070.         setupBootPartition
  4071.     fi
  4072.     #======================================
  4073.     # reset mount counter
  4074.     #--------------------------------------
  4075.     resetMountCounter
  4076.     IFS=$OLDIFS
  4077.     return $retval
  4078. }
  4079. #======================================
  4080. # cleanDirectory
  4081. #--------------------------------------
  4082. function cleanDirectory {
  4083.     local directory=$1
  4084.     shift 1
  4085.     local save=$@
  4086.     local tmpdir=`mktemp -d`
  4087.     for saveItem in $save;do
  4088.         mv $directory/$saveItem $tmpdir >/dev/null
  4089.     done
  4090.     rm -rf $directory/*
  4091.     mv $tmpdir/* $directory
  4092.     rm -rf $tmpdir
  4093. }
  4094. #======================================
  4095. # cleanInitrd
  4096. #--------------------------------------
  4097. function cleanInitrd {
  4098.     cp /usr/bin/chroot /bin
  4099.     cp /sbin/halt /bin/reboot
  4100.     if [ -x /usr/sbin/klogconsole ];then
  4101.         cp /usr/sbin/klogconsole /bin
  4102.     fi
  4103.     for dir in /*;do
  4104.         case "$dir" in
  4105.             "/lib")   continue ;;
  4106.             "/lib64") continue ;;
  4107.             "/bin")   continue ;;
  4108.             "/mnt")   continue ;;
  4109.             "/read-only") continue ;;
  4110.             "/read-write") continue ;;
  4111.             "/xino")  continue ;;
  4112.             "/dev")   continue ;;
  4113.         esac
  4114.         rm -rf $dir/* &>/dev/null
  4115.     done
  4116.     if test -L /read-only;then
  4117.         rm -f /read-only
  4118.     fi
  4119.     if test -L /read-write;then
  4120.         rm -f /read-write
  4121.     fi
  4122.     # mount opens fstab so we give them one
  4123.     touch /etc/fstab
  4124.     hash -r
  4125. }
  4126. #======================================
  4127. # searchAlternativeConfig
  4128. #--------------------------------------
  4129. function searchAlternativeConfig {
  4130.     # Check config.IP in Hex (pxelinux style)
  4131.     localip=$IPADDR
  4132.     hexip1=`echo $localip | cut -f1 -d'.'`
  4133.     hexip2=`echo $localip | cut -f2 -d'.'`
  4134.     hexip3=`echo $localip | cut -f3 -d'.'`
  4135.     hexip4=`echo $localip | cut -f4 -d'.'`
  4136.     hexip=`printf "%02X" $hexip1 $hexip2 $hexip3 $hexip4`
  4137.     STEP=8
  4138.     while [ $STEP -gt 0 ]; do
  4139.         hexippart=`echo $hexip | cut -b -$STEP`
  4140.         Echo "Checking for config file: config.$hexippart"
  4141.         fetchFile KIWI/config.$hexippart $CONFIG
  4142.         if test -s $CONFIG;then
  4143.             break
  4144.         fi
  4145.         let STEP=STEP-1
  4146.     done
  4147.     # Check config.default if no hex config was found
  4148.     if test ! -s $CONFIG;then
  4149.         Echo "Checking for config file: config.default"
  4150.         fetchFile KIWI/config.default $CONFIG
  4151.     fi
  4152. }
  4153. #======================================
  4154. # runHook
  4155. #--------------------------------------
  4156. function runHook {
  4157.     HOOK="/hooks/$1.sh"
  4158.     if [ -e $HOOK ]; then
  4159.         . $HOOK
  4160.     fi
  4161. }
  4162. #======================================
  4163. # getNextPartition
  4164. #--------------------------------------
  4165. function getNextPartition {
  4166.     part=$1
  4167.     nextPart=`echo $part | sed -e "s/\(.*\)[0-9]/\1/"`
  4168.     nextPartNum=`echo $part | sed -e "s/.*\([0-9]\)/\1/"`
  4169.     nextPartNum=`expr $nextPartNum + 1`
  4170.     nextPart="${nextPart}${nextPartNum}"
  4171.     echo $nextPart
  4172. }
  4173. #======================================
  4174. # startShell
  4175. #--------------------------------------
  4176. function startShell {
  4177.     # /.../
  4178.     # start a debugging shell on ELOG_BOOTSHELL
  4179.     # ----
  4180.     if [ -z "$kiwistderr" ] && [ ! -z $kiwidebug ];then
  4181.         Echo "Starting boot shell on $ELOG_BOOTSHELL"
  4182.         setctsid -f $ELOG_BOOTSHELL /bin/bash -i
  4183.     fi
  4184. }
  4185. #======================================
  4186. # killShell
  4187. #--------------------------------------
  4188. function killShell {
  4189.     # /.../
  4190.     # kill debugging shell on ELOG_BOOTSHELL
  4191.     # ----
  4192.     local umountProc=0
  4193.     if [ ! -e /proc/mounts ];then
  4194.         mount -t proc proc /proc
  4195.         umountProc=1
  4196.     fi
  4197.     if [ -z "$kiwistderr" ];then
  4198.         Echo "Stopping boot shell"
  4199.         fuser -k $ELOG_BOOTSHELL >/dev/null
  4200.     fi
  4201.     if [ $umountProc -eq 1 ];then
  4202.         umount /proc
  4203.     fi
  4204. }
  4205. #======================================
  4206. # waitForStorageDevice
  4207. #--------------------------------------
  4208. function waitForStorageDevice {
  4209.     # /.../
  4210.     # function to check access on a storage device
  4211.     # which could be a whole disk or a partition.
  4212.     # the function will wait until the size of the
  4213.     # storage device could be obtained or the check
  4214.     # counter equals 4
  4215.     # ----
  4216.     local device=$1
  4217.     local check=0
  4218.     udevPending
  4219.     while true;do
  4220.         partitionSize $device &>/dev/null
  4221.         if [ $? = 0 ];then
  4222.             sleep 1; return 0
  4223.         fi
  4224.         if [ $check -eq 4 ];then
  4225.             return 1
  4226.         fi
  4227.         Echo "Waiting for device $device to settle..."
  4228.         check=`expr $check + 1`
  4229.         sleep 2
  4230.     done
  4231. }
  4232.  
  4233. #======================================
  4234. # waitForBlockDevice
  4235. #--------------------------------------
  4236. function waitForBlockDevice {
  4237.     # /.../
  4238.     # function to check if the given block device
  4239.     # exists. If not the function will wait until the
  4240.     # device appears or the check counter equals 4
  4241.     # ----
  4242.     local device=$1
  4243.     local check=0
  4244.     udevPending
  4245.     while true;do
  4246.         if [ -b $device ] || [ $check -eq 4 ];then
  4247.             break
  4248.         fi
  4249.         Echo "Waiting for device $device to settle..."
  4250.         check=`expr $check + 1`
  4251.         sleep 2
  4252.     done
  4253. }
  4254.  
  4255. #======================================
  4256. # fetchFile
  4257. #--------------------------------------
  4258. function fetchFile {
  4259.     # /.../
  4260.     # the generic fetcher which is able to use different protocols
  4261.     # tftp,ftp, http, https. fetchFile is used in the netboot linuxrc
  4262.     # and uses curl and atftp to download files from the network
  4263.     # ----
  4264.     local path=$1
  4265.     local dest=$2
  4266.     local izip=$3
  4267.     local host=$4
  4268.     local type=$5
  4269.     if test -z "$path"; then
  4270.         systemException "No path specified" "reboot"
  4271.     fi
  4272.     if test -z "$host"; then
  4273.         if test -z "$SERVER"; then
  4274.             systemException "No server specified" "reboot"
  4275.         fi
  4276.         host=$SERVER
  4277.     fi
  4278.     if test -z "$type"; then
  4279.         if test -z "$SERVERTYPE"; then
  4280.             type="tftp"
  4281.         else
  4282.             type="$SERVERTYPE"
  4283.         fi
  4284.     fi
  4285.     if test "$izip" = "compressed"; then
  4286.         path=$(echo $path | sed -e s@\\.gz@@)
  4287.         path="$path.gz"
  4288.     fi
  4289.     case "$type" in
  4290.         "http")
  4291.             if test "$izip" = "compressed"; then
  4292.                 curl -f http://$host/$path 2>$TRANSFER_ERRORS_FILE |\
  4293.                     gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE
  4294.             else
  4295.                 curl -f http://$host/$path > $dest 2> $TRANSFER_ERRORS_FILE
  4296.             fi
  4297.             loadCode=$?
  4298.             ;;
  4299.         "https")
  4300.             if test "$izip" = "compressed"; then
  4301.                 curl -f -k https://$host/$path 2>$TRANSFER_ERRORS_FILE |\
  4302.                     gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE
  4303.             else
  4304.                 curl -f -k https://$host/$path > $dest 2> $TRANSFER_ERRORS_FILE
  4305.             fi
  4306.             loadCode=$?
  4307.             ;;
  4308.         "ftp")
  4309.             if test "$izip" = "compressed"; then
  4310.                 curl ftp://$host/$path 2>$TRANSFER_ERRORS_FILE |\
  4311.                     gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE
  4312.             else
  4313.                 curl ftp://$host/$path > $dest 2> $TRANSFER_ERRORS_FILE
  4314.             fi
  4315.             loadCode=$?
  4316.             ;;
  4317.         "tftp")
  4318.             validateBlockSize
  4319.             # /.../
  4320.             # atftp activates multicast by '--option "multicast"'
  4321.             # and deactivates it again  by '--option "disable multicast"'
  4322.             # ----
  4323.             multicast_atftp="multicast"
  4324.             if test "$multicast" != "enable"; then
  4325.                 multicast_atftp="disable multicast"
  4326.             fi
  4327.             if test "$izip" = "compressed"; then
  4328.                 # mutlicast is disabled because you can't seek in a pipe
  4329.                 # atftp is disabled because it doesn't work with pipes
  4330.                 busybox tftp \
  4331.                     -b $imageBlkSize -g -r $path \
  4332.                     -l >(gzip -d > $dest 2>>$TRANSFER_ERRORS_FILE) \
  4333.                     $host 2>>$TRANSFER_ERRORS_FILE
  4334.             else
  4335.                 atftp \
  4336.                     --option "$multicast_atftp"  \
  4337.                     --option "blksize $imageBlkSize" \
  4338.                     -g -r $path -l $dest $host &> $TRANSFER_ERRORS_FILE
  4339.             fi
  4340.             loadCode=$?
  4341.             ;;
  4342.         *)
  4343.             systemException "Unknown download type: $type" "reboot"
  4344.             ;;
  4345.     esac
  4346.     loadStatus=`cat $TRANSFER_ERRORS_FILE`
  4347.     return $loadCode
  4348. }
  4349.  
  4350. #======================================
  4351. # putFile
  4352. #--------------------------------------
  4353. function putFile {
  4354.     # /.../
  4355.     # the generic putFile function is used to upload boot data on
  4356.     # a server. Supported protocols are tftp, ftp, http, https
  4357.     # ----
  4358.     local path=$1
  4359.     local dest=$2
  4360.     local host=$3
  4361.     local type=$4
  4362.     if test -z "$path"; then
  4363.         systemException "No path specified" "reboot"
  4364.     fi
  4365.     if test -z "$host"; then
  4366.         if test -z "$SERVER"; then
  4367.             systemException "No server specified" "reboot"
  4368.         fi
  4369.         host=$SERVER
  4370.     fi
  4371.     if test -z "$type"; then
  4372.         if test -z "$SERVERTYPE"; then
  4373.             type="tftp"
  4374.         else
  4375.             type="$SERVERTYPE"
  4376.         fi
  4377.     fi
  4378.     case "$type" in
  4379.         "http")
  4380.             curl -f -T $path http://$host/$dest > $TRANSFER_ERRORS_FILE 2>&1
  4381.             return $?
  4382.             ;;
  4383.         "https")
  4384.             curl -f -T $path https://$host/$dest > $TRANSFER_ERRORS_FILE 2>&1
  4385.             return $?
  4386.             ;;
  4387.         "ftp")
  4388.             curl -T $path ftp://$host/$dest  > $TRANSFER_ERRORS_FILE 2>&1
  4389.             return $?
  4390.             ;;
  4391.         "tftp")
  4392.             atftp -p -l $path -r $dest $host >/dev/null 2>&1
  4393.             return $?
  4394.             ;;
  4395.         *)
  4396.             systemException "Unknown download type: $type" "reboot"
  4397.             ;;
  4398.     esac
  4399. }
  4400.  
  4401. #======================================
  4402. # importBranding
  4403. #--------------------------------------
  4404. function importBranding {
  4405.     # /.../
  4406.     # include possible custom boot loader and bootsplash files
  4407.     # to the system to allow to use them persistently
  4408.     # ----
  4409.     if [ -f /image/loader/message ];then
  4410.         if ! canWrite /mnt/boot;then
  4411.             Echo "Can't write to boot, import of boot message skipped"
  4412.         else
  4413.             mv /image/loader/message /mnt/boot
  4414.         fi
  4415.     fi
  4416.     if [ -f /image/loader/branding/logo.mng ];then
  4417.     if [ -d /mnt/etc/bootsplash/themes ];then
  4418.         for theme in /mnt/etc/bootsplash/themes/*;do
  4419.             if [ -d $theme/images ];then
  4420.                 if ! canWrite $theme;then
  4421.                     Echo "Can't write to $theme, import of boot theme skipped"
  4422.                     continue
  4423.                 fi
  4424.                 cp /image/loader/branding/logo.mng  $theme/images
  4425.                 cp /image/loader/branding/logov.mng $theme/images
  4426.                 cp /image/loader/branding/*.jpg $theme/images
  4427.                 cp /image/loader/branding/*.cfg $theme/config
  4428.             fi
  4429.         done   
  4430.     fi
  4431.     fi
  4432. }
  4433.  
  4434. #======================================
  4435. # validateRootTree
  4436. #--------------------------------------
  4437. function validateRootTree {
  4438.     # /.../
  4439.     # after the root of the system image has been mounted we should
  4440.     # check whether that mount is a valid system tree or not. Therefore
  4441.     # some sanity checks are made here
  4442.     # ----
  4443.     if [ ! -x /mnt/sbin/init ];then
  4444.         systemException "/sbin/init no such file or not executable" "reboot"
  4445.     fi
  4446. }
  4447.  
  4448. #======================================
  4449. # getDiskID
  4450. #--------------------------------------
  4451. function getDiskID {
  4452.     # /.../
  4453.     # this function is able to turn a given standard device
  4454.     # name into the udev ID based representation
  4455.     # ----
  4456.     local device=$1
  4457.     if [ -z "$device" ];then
  4458.         return
  4459.     fi
  4460.     if echo $device | grep -q "$VGROUP"; then
  4461.         echo $device
  4462.         return
  4463.     fi
  4464.     for i in /dev/disk/by-id/*;do
  4465.         if echo $i | grep -q edd-;then
  4466.             continue
  4467.         fi
  4468.         local dev=`readlink $i`
  4469.         dev=/dev/`basename $dev`
  4470.         if [ $dev = $device ];then
  4471.             echo $i
  4472.             return
  4473.         fi
  4474.     done
  4475.     echo $device
  4476. }
  4477. #======================================
  4478. # getDiskDevice
  4479. #--------------------------------------
  4480. function getDiskDevice {
  4481.     # /.../
  4482.     # this function is able to turn the given udev disk
  4483.     # ID label into the /dev/ device name
  4484.     # ----
  4485.     local device=`readlink $1`
  4486.     if [ -z "$device" ];then
  4487.         echo $1
  4488.         return
  4489.     fi
  4490.     device=`basename $device`
  4491.     device=/dev/$device
  4492.     echo $device
  4493. }
  4494. #======================================
  4495. # getDiskModel
  4496. #--------------------------------------
  4497. function getDiskModels {
  4498.     # /.../
  4499.     # this function returns the disk identifier as
  4500.     # registered in the sysfs layer
  4501.     # ----
  4502.     local models=`cat /sys/block/*/device/model 2>/dev/null`
  4503.     if [ ! -z "$models" ];then
  4504.         echo $models; return
  4505.     fi
  4506.     echo "unknown"
  4507. }
  4508. #======================================
  4509. # setupInittab
  4510. #--------------------------------------
  4511. function setupInittab {
  4512.     # /.../
  4513.     # setup default runlevel according to /proc/cmdline
  4514.     # information. If textmode is set to 1 we will boot into
  4515.     # runlevel 3
  4516.     # ----
  4517.     local prefix=$1
  4518.     if cat /proc/cmdline | grep -qi "textmode=1";then
  4519.         sed -i -e s"@id:.*:initdefault:@id:3:initdefault:@" $prefix/etc/inittab
  4520.     fi
  4521. }
  4522. #======================================
  4523. # setupConfigFiles
  4524. #--------------------------------------
  4525. function setupConfigFiles {
  4526.     # /.../
  4527.     # all files created below /config inside the initrd are
  4528.     # now copied into the system image
  4529.     # ----
  4530.     local file
  4531.     local dir
  4532.     cd /config
  4533.     find . -type f | while read file;do
  4534.         dir=$(dirname $file)
  4535.         if ! canWrite /mnt/$dir;then
  4536.             Echo "Can't write to $dir, read-only filesystem... skipped"
  4537.             continue
  4538.         fi
  4539.         if [ ! -d /mnt/$dir ];then
  4540.             mkdir -p /mnt/$dir
  4541.         fi
  4542.         cp $file /mnt/$file
  4543.     done
  4544.     cd /
  4545.     rm -rf /config
  4546. }
  4547. #======================================
  4548. # activateImage
  4549. #--------------------------------------
  4550. function activateImage {
  4551.     # /.../
  4552.     # move the udev created nodes from the initrd into
  4553.     # the system root tree call the pre-init phase which
  4554.     # already runs in the new tree and finaly switch the
  4555.     # new tree to be the new root (/)
  4556.     # ----
  4557.     #======================================
  4558.     # setup image name
  4559.     #--------------------------------------
  4560.     local name
  4561.     if [ ! -z "$stickSerial" ];then
  4562.         name="$stickSerial on -> $stickDevice"
  4563.     elif [ ! -z "$imageName" ];then
  4564.         name=$imageName
  4565.     elif [ ! -z "$imageRootName" ];then
  4566.         name=$imageRootName
  4567.     elif [ ! -z "$imageRootDevice" ];then
  4568.         name=$imageRootDevice
  4569.     elif [ ! -z "$imageDiskDevice" ];then
  4570.         name=$imageDiskDevice
  4571.     else
  4572.         name="unknown"
  4573.     fi
  4574.     #======================================
  4575.     # move union mount points to system
  4576.     #--------------------------------------
  4577.     local roDir=read-only
  4578.     local rwDir=read-write
  4579.     local xiDir=xino
  4580.     if [ -z "$NFSROOT" ];then
  4581.         if [ -d $roDir ];then
  4582.             mkdir -p /mnt/$roDir && mount --move /$roDir /mnt/$roDir
  4583.         fi
  4584.         if [ -d $rwDir ];then
  4585.             mkdir -p /mnt/$rwDir && mount --move /$rwDir /mnt/$rwDir
  4586.         fi
  4587.         if [ -d $xiDir ];then
  4588.             mkdir -p /mnt/$xiDir && mount --move /$xiDir /mnt/$xiDir
  4589.         fi
  4590.     fi
  4591.     #======================================
  4592.     # move live CD mount points to system
  4593.     #--------------------------------------
  4594.     local cdDir=/livecd
  4595.     if [ -d $cdDir ];then
  4596.         mkdir -p /mnt/$cdDir && mount --move /$cdDir /mnt/$cdDir
  4597.     fi
  4598.     #======================================
  4599.     # move device nodes
  4600.     #--------------------------------------
  4601.     Echo "Activating Image: [$name]"
  4602.     reopenKernelConsole
  4603.     udevPending
  4604.     mount --move /dev /mnt/dev
  4605.     udevKill
  4606.     #======================================
  4607.     # run preinit stage
  4608.     #--------------------------------------
  4609.     Echo "Preparing preinit phase..."
  4610.     if ! cp /iprocs /mnt;then
  4611.         systemException "Failed to copy iprocs code" "reboot"
  4612.     fi
  4613.     if ! cp /preinit /mnt;then
  4614.         systemException "Failed to copy preinit code" "reboot"
  4615.     fi
  4616.     if ! cp /include /mnt;then
  4617.         systemException "Failed to copy include code" "reboot"
  4618.     fi
  4619.     if [ ! -x /lib/mkinitrd/bin/run-init ];then
  4620.         systemException "Can't find run-init program" "reboot"
  4621.     fi
  4622. }
  4623. #======================================
  4624. # cleanImage
  4625. #--------------------------------------
  4626. function cleanImage {
  4627.     # /.../
  4628.     # remove preinit code from system image before real init
  4629.     # is called
  4630.     # ----
  4631.     #======================================
  4632.     # kill second utimer and tail
  4633.     #--------------------------------------
  4634.     . /iprocs
  4635.     kill $UTIMER_PID &>/dev/null
  4636.     kill $TAIL_PID   &>/dev/null
  4637.     #======================================
  4638.     # remove preinit code from system image
  4639.     #--------------------------------------
  4640.     rm -f /tmp/utimer
  4641.     rm -f /dev/utimer
  4642.     rm -f /utimer
  4643.     rm -f /iprocs
  4644.     rm -f /preinit
  4645.     rm -f /include
  4646.     rm -f /.kconfig
  4647.     rm -f /.profile
  4648.     rm -rf /image
  4649.     #======================================
  4650.     # return early for special types
  4651.     #--------------------------------------
  4652.     if \
  4653.         [ "$haveClicFS" = "yes" ] || \
  4654.         [ "$haveBtrFS"  = "yes" ] || \
  4655.         [ ! -z "$NFSROOT" ]       || \
  4656.         [ ! -z "$NBDROOT" ]       || \
  4657.         [ ! -z "$AOEROOT" ]       || \
  4658.         [ ! -z "$COMBINED_IMAGE" ]
  4659.     then
  4660.         export ROOTFS_FSCK="0"
  4661.         return
  4662.     fi
  4663.     #======================================
  4664.     # umount non busy fstab listed entries
  4665.     #--------------------------------------
  4666.     umount -a &>/dev/null
  4667.     #======================================
  4668.     # umount LVM root parts lazy
  4669.     #--------------------------------------
  4670.     if [ "$haveLVM" = "yes" ]; then
  4671.         for i in /dev/$VGROUP/LV*;do
  4672.             if [ ! -e $i ];then
  4673.                 continue
  4674.             fi
  4675.             if \
  4676.                 [ ! $i = "/dev/$VGROUP/LVRoot" ] && \
  4677.                 [ ! $i = "/dev/$VGROUP/LVComp" ] && \
  4678.                 [ ! $i = "/dev/$VGROUP/LVSwap" ]
  4679.             then
  4680.                 umount -l $i &>/dev/null
  4681.             fi
  4682.         done
  4683.     fi
  4684. }
  4685. #======================================
  4686. # bootImage
  4687. #--------------------------------------
  4688. function bootImage {
  4689.     # /.../
  4690.     # call the system image init process and therefore
  4691.     # boot into the operating system
  4692.     # ----
  4693.     local reboot=no
  4694.     local option=$@
  4695.     #======================================
  4696.     # turn runlevel 4 to 5 if found
  4697.     #--------------------------------------
  4698.     option=$(echo $@ | sed -e s@4@5@)
  4699.     echo && Echo "Booting System: $option"
  4700.     export IFS=$IFS_ORIG
  4701.     #======================================
  4702.     # check for reboot request
  4703.     #--------------------------------------
  4704.     if [ "$LOCAL_BOOT" = "no" ];then
  4705.         if [ -z "$KIWI_RECOVERY" ];then
  4706.             if [ ! -z "$OEM_REBOOT" ] || [ ! -z "$REBOOT_IMAGE" ];then
  4707.                 reboot=yes
  4708.             fi
  4709.         fi
  4710.     fi
  4711.     #======================================
  4712.     # kill initial tail and utimer
  4713.     #--------------------------------------
  4714.     . /iprocs
  4715.     kill $UTIMER_PID &>/dev/null
  4716.     kill $TAIL_PID   &>/dev/null
  4717.     #======================================
  4718.     # copy boot log file into system image
  4719.     #--------------------------------------
  4720.     mkdir -p /mnt/var/log
  4721.     rm -f /mnt/boot/grub/mbrid
  4722.     if [ -e /mnt/dev/shm/initrd.msg ];then
  4723.         cp -f /mnt/dev/shm/initrd.msg /mnt/var/log/boot.msg
  4724.     fi
  4725.     if [ -e /var/log/boot.kiwi ];then
  4726.         cp -f /var/log/boot.kiwi /mnt/var/log/boot.kiwi
  4727.     fi
  4728.     #======================================
  4729.     # directly boot/reboot
  4730.     #--------------------------------------
  4731.     umount proc &>/dev/null && \
  4732.     umount proc &>/dev/null
  4733.     if [ $reboot = "yes" ];then
  4734.         Echo "Reboot requested... rebooting after preinit"
  4735.         exec /lib/mkinitrd/bin/run-init -c /dev/console /mnt /bin/bash -c \
  4736.             "/preinit ; . /include ; cleanImage ; exec /sbin/reboot -f -i"
  4737.     else
  4738.         # FIXME: clicfs doesn't like run-init
  4739.         if [ ! "$haveClicFS" = "yes" ];then
  4740.             exec /lib/mkinitrd/bin/run-init -c /dev/console /mnt /bin/bash -c \
  4741.                 "/preinit ; . /include ; cleanImage ; exec /sbin/init $option"
  4742.         else
  4743.             cd /mnt && exec chroot . /bin/bash -c \
  4744.                 "/preinit ; . /include ; cleanImage ; exec /sbin/init $option"
  4745.         fi
  4746.     fi
  4747. }
  4748. #======================================
  4749. # setupUnionFS
  4750. #--------------------------------------
  4751. function setupUnionFS {
  4752.     # /.../
  4753.     # export the UNIONFS_CONFIG environment variable
  4754.     # which contains a three part coma separated list of the
  4755.     # following style: rwDevice,roDevice,unionType. The
  4756.     # devices are stores by disk ID if possible
  4757.     # ----
  4758.     local rwDevice=`getDiskID $1`
  4759.     local roDevice=`getDiskID $2`
  4760.     local unionFST=$3
  4761.     rwDeviceLuks=$(luksOpen $rwDevice luksReadWrite)
  4762.     roDeviceLuks=$(luksOpen $roDevice luksReadOnly)
  4763.     if [ ! $rwDeviceLuks = $rwDevice ];then
  4764.         rwDevice=$rwDeviceLuks
  4765.         export haveLuks="yes"
  4766.     fi
  4767.     if [ ! $roDeviceLuks = $roDevice ];then
  4768.         roDevice=$roDeviceLuks
  4769.         export haveLuks="yes"
  4770.     fi
  4771.     export UNIONFS_CONFIG="$rwDevice,$roDevice,$unionFST"
  4772. }
  4773. #======================================
  4774. # canWrite
  4775. #--------------------------------------
  4776. function canWrite {
  4777.     # /.../
  4778.     # check if we can write to the given location
  4779.     # If no location is given the function test
  4780.     # for write permissions in /mnt.
  4781.     # returns zero on success.
  4782.     # ---
  4783.     local dest=$1
  4784.     if [ -z "$dest" ];then
  4785.         dest=/mnt
  4786.     fi
  4787.     if [ ! -d $dest ];then
  4788.         return 1
  4789.     fi
  4790.     if touch $dest/can-write &>/dev/null;then
  4791.         rm $dest/can-write
  4792.         return 0
  4793.     fi
  4794.     return 1
  4795. }
  4796. #======================================
  4797. # xenServer
  4798. #--------------------------------------
  4799. function xenServer {
  4800.     # /.../
  4801.     # check if the given kernel is a xen kernel and if so
  4802.     # check if a dom0 or a domU setup was requested
  4803.     # ----
  4804.     local kname=$1
  4805.     local mountPrefix=$2
  4806.     local sysmap="$mountPrefix/boot/System.map-$kname"
  4807.     local isxen
  4808.     if [ ! -e $sysmap ]; then
  4809.         sysmap="$mountPrefix/boot/System.map"
  4810.     fi
  4811.     if [ ! -e $sysmap ]; then
  4812.         Echo "No system map for kernel $kname found"
  4813.         return 1
  4814.     fi
  4815.     isxen=$(grep -c "xen_base" $sysmap)
  4816.     if [ $isxen -eq 0 ]; then
  4817.         # not a xen kernel
  4818.         return 1
  4819.     fi
  4820.     if [ -z "$kiwi_xendomain" ];then
  4821.         # no xen domain set, assume domU
  4822.         return 1
  4823.     fi
  4824.     if [ $kiwi_xendomain = "dom0" ];then
  4825.         # xen dom0 requested
  4826.         return 0
  4827.     fi
  4828.     return 1
  4829. }
  4830. #======================================
  4831. # makeLabel
  4832. #--------------------------------------
  4833. function makeLabel {
  4834.     # /.../
  4835.     # create boot label and replace all spaces with
  4836.     # underscores. current bootloaders show the
  4837.     # underscore sign as as space in the boot menu
  4838.     # ---
  4839.     if [ -z "$loader" ];then
  4840.         loader="grub"
  4841.     fi
  4842.     if [ ! $loader = "syslinux" ] && [ ! $loader = "extlinux" ];then
  4843.         echo $1 | tr " " "_"
  4844.     else
  4845.         echo $1
  4846.     fi
  4847. }
  4848. #======================================
  4849. # waitForX
  4850. #--------------------------------------
  4851. function waitForX {
  4852.     # /.../
  4853.     # wait for the X-Server with PID $xserver_pid to
  4854.     # become read for client calls
  4855.     # ----
  4856.     local xserver_pid=$1
  4857.     local testx=/usr/sbin/testX
  4858.     local err=1
  4859.     while kill -0 $xserver_pid 2>/dev/null ; do
  4860.         sleep 1
  4861.         if test -e /tmp/.X11-unix/X0 && test -x $testx ; then
  4862.             $testx 16 2>/dev/null
  4863.             err=$?
  4864.             # exit code 1 -> XOpenDisplay failed...
  4865.             if test $err = 1;then
  4866.                 Echo "TestX: XOpenDisplay failed"
  4867.                 return 1
  4868.             fi
  4869.             # exit code 2 -> color or dimensions doesn't fit...
  4870.             if test $err = 2;then
  4871.                 Echo "TestX: color or dimensions doesn't fit"
  4872.                 kill $xserver_pid
  4873.                 return 1
  4874.             fi
  4875.             # server is running, detach oom-killer from it
  4876.             echo -n '-17' > /proc/$xserver_pid/oom_adj
  4877.             return 0
  4878.         fi
  4879.     done
  4880.     return 1
  4881. }
  4882. #======================================
  4883. # startX
  4884. #--------------------------------------
  4885. function startX {
  4886.     # /.../
  4887.     # start X-Server and wait for it to become ready
  4888.     # ----
  4889.     export DISPLAY=:0
  4890.     local XServer=/usr/bin/Xorg
  4891.     if [ -x /usr/X11R6/bin/Xorg ];then
  4892.         XServer=/usr/X11R6/bin/Xorg
  4893.     fi
  4894.     $XServer -deferglyphs 16 vt07 &
  4895.     export XServerPID=$!
  4896.     if ! waitForX $XServerPID;then
  4897.         Echo "Failed to start X-Server"
  4898.         return 1
  4899.     fi
  4900.     return 0
  4901. }
  4902. #======================================
  4903. # stoppX
  4904. #--------------------------------------
  4905. function stoppX {
  4906.     if [ -z "$XServerPID" ];then
  4907.         return
  4908.     fi
  4909.     if kill -0 $XServerPID 2>/dev/null; then
  4910.         sleep 1 && kill $XServerPID
  4911.         while kill -0 $XServerPID 2>/dev/null; do
  4912.             sleep 1
  4913.         done
  4914.     fi
  4915. }
  4916. #======================================
  4917. # luksOpen
  4918. #--------------------------------------
  4919. function luksOpen {
  4920.     # /.../
  4921.     # check given device if it uses the LUKS extension
  4922.     # if yes open the device and return the new
  4923.     # /dev/mapper/ device name
  4924.     # ----
  4925.     local ldev=$1
  4926.     local name=$2
  4927.     local info
  4928.     if [ -z $name ];then
  4929.         name=luksroot
  4930.     fi
  4931.     if [ -e /dev/mapper/$name ];then
  4932.         echo /dev/mapper/$name; return
  4933.     fi
  4934.     if ! cryptsetup isLuks $ldev &>/dev/null;then
  4935.         echo $ldev; return
  4936.     fi
  4937.     while true;do
  4938.         if [ ! -e /tmp/luks ];then
  4939.             LUKS_OPEN=$(runInteractive \
  4940.                 "--stdout --insecure --passwordbox "\"$TEXT_LUKS\"" 10 60"
  4941.             )
  4942.             echo $LUKS_OPEN > /tmp/luks
  4943.         fi
  4944.         info=$(cat /tmp/luks | cryptsetup luksOpen $ldev $name 2>&1)
  4945.         if [ $? = 0 ];then
  4946.             break
  4947.         fi
  4948.         rm -f /tmp/luks
  4949.         Dialog --stdout --timeout 10 --msgbox "\"Error: $info\"" 8 60
  4950.     done
  4951.     echo /dev/mapper/$name
  4952. }
  4953. #======================================
  4954. # luksResize
  4955. #--------------------------------------
  4956. function luksResize {
  4957.     # /.../
  4958.     # check given device if it is a mapped device name
  4959.     # and run cryptsetup resize on the mapper name
  4960.     # ----
  4961.     local ldev=$1
  4962.     if [ ! "$haveLuks" = "yes" ] || [ ! -e $ldev ];then
  4963.         return
  4964.     fi
  4965.     local name=$(basename $ldev)
  4966.     local dmap=$(dirname  $ldev)
  4967.     if [ ! "$dmap" = "/dev/mapper" ];then
  4968.         return
  4969.     fi
  4970.     cryptsetup resize $name
  4971. }
  4972. #======================================
  4973. # luksClose
  4974. #--------------------------------------
  4975. function luksClose {
  4976.     # /.../
  4977.     # close all open LUKS mappings
  4978.     # ----
  4979.     local name
  4980.     for i in /dev/mapper/luks*;do
  4981.         name=$(basename $i)
  4982.         cryptsetup luksClose $name
  4983.     done
  4984. }
  4985. #======================================
  4986. # selectLanguage
  4987. #--------------------------------------
  4988. function selectLanguage {
  4989.     # /.../
  4990.     # select language if not yet done. The value is
  4991.     # used for all dialog windows with i18n support
  4992.     # ----
  4993.     local title="\"Select Language\""
  4994.     local list="en_US \"[ English ]\" on"
  4995.     local list_orig=$list
  4996.     local zh_CN=Chinese
  4997.     local zh_TW=Taiwanese
  4998.     local ru_RU=Russian
  4999.     local de_DE=German
  5000.     local ar_AR=Arabic
  5001.     local cs_CZ=Czech
  5002.     local el_GR=Greek
  5003.     local es_ES=Spanish
  5004.     local fi_FI=Finnish
  5005.     local fr_FR=French
  5006.     local hu_HU=Hungarian
  5007.     local it_IT=Italian
  5008.     local ja_JP=Japanese
  5009.     local ko_KR=Korean
  5010.     local nl_NL=Dutch
  5011.     local pl_PL=Polish
  5012.     local pt_BR=Portuguese
  5013.     local sv_SE=Swedish
  5014.     local tr_TR=Turkish
  5015.     local code
  5016.     local lang
  5017.     if [ -f /.profile ];then
  5018.         importFile < /.profile
  5019.     fi
  5020.     if [ ! -z "$kiwi_oemunattended" ] && [ "$DIALOG_LANG" = "ask" ];then
  5021.         DIALOG_LANG=en_US
  5022.     fi
  5023.     if [ "$DIALOG_LANG" = "ask" ];then
  5024.         for code in $(echo $kiwi_language | tr "," " ");do
  5025.             if [ $code = "en_US" ];then
  5026.                 continue
  5027.             fi
  5028.             eval lang=\$$code
  5029.             list="$list $code \"[ $lang ]\" off"
  5030.         done
  5031.         if [ "$list" = "$list_orig" ];then
  5032.             DIALOG_LANG=en_US
  5033.         else
  5034.             DIALOG_LANG=$(runInteractive \
  5035.                 "--stdout --no-cancel --radiolist $title 20 40 10 $list"
  5036.             )
  5037.         fi
  5038.     fi
  5039.     #======================================
  5040.     # Exports (Texts)
  5041.     #--------------------------------------
  5042.     export TEXT_OK=$(
  5043.         getText "OK")
  5044.     export TEXT_CANCEL=$(
  5045.         getText "Cancel")
  5046.     export TEXT_YES=$(
  5047.         getText "Yes")
  5048.     export TEXT_NO=$(
  5049.         getText "No")
  5050.     export TEXT_EXIT=$(
  5051.         getText "Exit")
  5052.     export TEXT_LUKS=$(
  5053.         getText "Enter LUKS passphrase")
  5054.     export TEXT_LICENSE=$(
  5055.         getText "Do you accept the license agreement ?")
  5056.     export TEXT_RESTORE=$(
  5057.         getText "Do you want to start the System-Restore ?")
  5058.     export TEXT_REPAIR=$(
  5059.         getText "Do you want to start the System-Recovery ?")
  5060.     export TEXT_RECOVERYTITLE=$(
  5061.         getText "Restoring base operating system...")
  5062.     export TEXT_INSTALLTITLE=$(
  5063.         getText "Installation...")
  5064.     export TEXT_CDPULL=$(
  5065.         getText "Please remove the CD/DVD before reboot")
  5066.     export TEXT_USBPULL=$(
  5067.         getText "Please unplug the USB stick before reboot")
  5068.     export TEXT_SELECT=$(
  5069.         getText "Select disk for installation:")
  5070. }
  5071. #======================================
  5072. # getText
  5073. #--------------------------------------
  5074. function getText {
  5075.     # /.../
  5076.     # return translated text
  5077.     # ----
  5078.     export LANG=$DIALOG_LANG.utf8
  5079.     local text=$(gettext kiwi "$1")
  5080.     if [ ! -z "$2" ];then
  5081.         text=$(echo $text | sed -e s"@%1@$2@")
  5082.     fi
  5083.     if [ ! -z "$3" ];then
  5084.         text=$(echo $text | sed -e s"@%2@$3@")
  5085.     fi
  5086.     echo "$text"
  5087. }
  5088. #======================================
  5089. # displayEULA
  5090. #--------------------------------------
  5091. function displayEULA {
  5092.     # /.../
  5093.     # display in a dialog window the text part of the
  5094.     # selected language file or the default file
  5095.     # /license.txt or /EULA.txt
  5096.     # ----
  5097.     local code=$(echo $DIALOG_LANG | cut -f1 -d_)
  5098.     #======================================
  5099.     # check license files
  5100.     #--------------------------------------
  5101.     local files=$(find /license.*txt 2>/dev/null)
  5102.     if [ -z "$files" ];then
  5103.         return
  5104.     fi
  5105.     #======================================
  5106.     # use selected file or default
  5107.     #--------------------------------------
  5108.     code=/license.$code.txt
  5109.     if [ ! -f $code ];then
  5110.         code=/license.txt
  5111.         if [ ! -f $code ];then
  5112.             code=/EULA.txt
  5113.         fi
  5114.     fi
  5115.     #======================================
  5116.     # check selected file and show it
  5117.     #--------------------------------------
  5118.     if [ ! -f $code ];then
  5119.         Echo "License file $code not found... skipped"
  5120.         return
  5121.     fi
  5122.     while true;do
  5123.         Dialog --textbox $code 20 70 \
  5124.             --and-widget --extra-button \
  5125.             --extra-label "$TEXT_NO" \
  5126.             --ok-label "$TEXT_YES" \
  5127.             --cancel-label "$TEXT_CANCEL" \
  5128.             --yesno "\"$TEXT_LICENSE\"" \
  5129.             5 45
  5130.         case $? in
  5131.             0 ) break
  5132.                 ;;
  5133.             1 ) continue
  5134.                 ;;
  5135.             * ) systemException \
  5136.                     "License not accepted... reboot" \
  5137.                 "reboot"
  5138.                 ;;
  5139.         esac
  5140.     done
  5141. }
  5142. #======================================
  5143. # ddn
  5144. #--------------------------------------
  5145. function ddn {
  5146.     # /.../
  5147.     # print disk device name (node name) according to the
  5148.     # linux device node specs: If the last character of the
  5149.     # device is a letter, attach the partition number. If the
  5150.     # last character is a number, attach a 'p' and then the
  5151.     # partition number.
  5152.     # ----
  5153.     local lastc=$(echo $1 | sed -e 's@\(^.*\)\(.$\)@\2@')
  5154.     if echo $lastc | grep -qP "^\d+$";then
  5155.         echo $1"p"$2
  5156.         return
  5157.     fi
  5158.     echo $1$2
  5159. }
  5160. #======================================
  5161. # dn
  5162. #--------------------------------------
  5163. function dn {
  5164.     # /.../
  5165.     # print disk name (device name) according to the
  5166.     # linux device node specs: If the device matches "p"
  5167.     # followed by a number remove pX else remove
  5168.     # the last number
  5169.     # ----
  5170.     local part=$(getDiskDevice $1)
  5171.     local part_new=$(echo $part | sed -e 's@\(^.*\)\(p.*$\)@\1@')
  5172.     if [ $part = $part_new ];then
  5173.         part_new=$(echo $part | sed -e 's@\(^.*\)\([0-9].*$\)@\1@')
  5174.     fi
  5175.     echo $part_new
  5176. }
  5177. #======================================
  5178. # ndd
  5179. #--------------------------------------
  5180. function nd {
  5181.     # /.../
  5182.     # print the number of the disk device according to the
  5183.     # device node name.
  5184.     # ----
  5185.     local part=$(getDiskDevice $1)
  5186.     local part_new=$(echo $part | sed -e 's@\(^.*\)p\(.*$\)@\2@')
  5187.     if [ $part = $part_new ];then
  5188.         part_new=$(echo $part | sed -e 's@\(^.*\)\([0-9].*$\)@\2@')
  5189.     fi
  5190.     echo $part_new
  5191. }
  5192. #======================================
  5193. # runInteractive
  5194. #--------------------------------------
  5195. function runInteractive {
  5196.     # /.../
  5197.     # run dialog in a bash inside an fbiterm or directly
  5198.     # on the running terminal. Make the terminal the controlling
  5199.     # tty first. The output of the dialog call is stored in
  5200.     # a file and printed as result to this function
  5201.     # ----
  5202.     local r=/tmp/rid
  5203.     local code
  5204.     echo "dialog $@ > /tmp/out" > $r
  5205.     echo "echo -n \$? > /tmp/out.exit" >> $r
  5206.     if [ -e /dev/fb0 ];then
  5207.         setctsid $ELOG_EXCEPTION fbiterm -m $UFONT -- bash -i $r
  5208.     else
  5209.         setctsid $ELOG_EXCEPTION bash -i $r
  5210.     fi
  5211.     code=$(cat /tmp/out.exit)
  5212.     if [ ! $code = 0 ];then
  5213.         return $code
  5214.     fi
  5215.     cat /tmp/out && rm -f /tmp/out* $r
  5216.     return 0
  5217. }
  5218. #======================================
  5219. # createHybridPersistent
  5220. #--------------------------------------
  5221. function createHybridPersistent {
  5222.     local dev=$1;
  5223.     local relativeDevName=`basename $dev`
  5224.     local input=/part.input
  5225.     local id=0
  5226.     for disknr in 2 3 4; do
  5227.         id=`partitionID $dev $disknr`
  5228.         if [ $id = $HYBRID_PERSISTENT_ID ]; then
  5229.             Echo "Existing persistent hybrid partition found $dev$disknr"
  5230.             return
  5231.         else
  5232.             Echo "Creating hybrid persistent partition for COW data: "
  5233.             Echo "$dev$disknr id=$HYBRID_PERSISTENT_ID fs=$HYBRID_PERSISTENT_FS"
  5234.             if [ $disknr -lt 4 ];then
  5235.                 createPartitionerInput \
  5236.                     n p $disknr . . t $disknr $HYBRID_PERSISTENT_ID w
  5237.             else
  5238.                 createPartitionerInput \
  5239.                     n p . . t 4 $HYBRID_PERSISTENT_ID w
  5240.             fi
  5241.             imageDiskDevice=$dev
  5242.             callPartitioner $input
  5243.             if ! waitForStorageDevice $dev$disknr;then
  5244.                 Echo "Partition $dev$disknr doesn't appear... fatal !"
  5245.                 Echo "Persistent writing deactivated"
  5246.                 unset kiwi_hybridpersistent
  5247.             elif ! mkfs.$HYBRID_PERSISTENT_FS $dev$disknr;then
  5248.                 Echo "Failed to create hybrid persistent filesystem"
  5249.                 Echo "Persistent writing deactivated"
  5250.                 unset kiwi_hybridpersistent
  5251.             fi
  5252.             return
  5253.         fi
  5254.     done
  5255. }
  5256. #======================================
  5257. # callPartitioner
  5258. #--------------------------------------
  5259. function callPartitioner {
  5260.     local input=$1
  5261.     if [ $PARTITIONER = "sfdisk" ];then
  5262.         Echo "Repartition the disk according to real geometry [ fdisk ]"
  5263.         echo "w" >> $input
  5264.         echo "q" >> $input
  5265.         fdisk $imageDiskDevice < $input 1>&2
  5266.         if test $? != 0; then
  5267.             systemException "Failed to create partition table" "reboot"
  5268.         fi
  5269.     else
  5270.         # /.../
  5271.         # nothing to do for parted here as we write
  5272.         # imediately with parted and don't create a
  5273.         # command input file as for fdisk but we re-read
  5274.         # the disk so that the new table will be used
  5275.         # ----
  5276.         udevPending
  5277.         blockdev --rereadpt $imageDiskDevice
  5278.     fi
  5279. }
  5280. #======================================
  5281. # createPartitionerInput
  5282. #--------------------------------------
  5283. function createPartitionerInput {
  5284.     if [ $PARTITIONER = "sfdisk" ];then
  5285.         createFDiskInput $@
  5286.     else
  5287.         Echo "Repartition the disk according to real geometry [ parted ]"
  5288.         partedInit $imageDiskDevice
  5289.         partedSectorInit $imageDiskDevice
  5290.         createPartedInput $imageDiskDevice $@
  5291.     fi
  5292. }
  5293. #======================================
  5294. # createFDiskInput
  5295. #--------------------------------------
  5296. function createFDiskInput {
  5297.     local input=/part.input
  5298.     for cmd in $*;do
  5299.         if [ $cmd = "." ];then
  5300.             echo >> $input
  5301.             continue
  5302.         fi
  5303.         echo $cmd >> $input
  5304.     done
  5305. }
  5306. #======================================
  5307. # partedInit
  5308. #--------------------------------------
  5309. function partedInit {
  5310.     # /.../
  5311.     # initialize current partition table output
  5312.     # as well as the number of cylinders and the
  5313.     # cyliner size in kB for this disk
  5314.     # ----
  5315.     local device=$1
  5316.     local IFS=""
  5317.     local parted=$(parted -m $device unit cyl print)
  5318.     local header=$(echo $parted | head -n 3 | tail -n 1)
  5319.     local ccount=$(echo $header | cut -f1 -d:)
  5320.     local cksize=$(echo $header | cut -f4 -d: | cut -f1 -dk)
  5321.     export partedOutput=$parted
  5322.     export partedCylCount=$ccount
  5323.     export partedCylKSize=$cksize
  5324. }
  5325. #======================================
  5326. # partedWrite
  5327. #--------------------------------------
  5328. function partedWrite {
  5329.     # /.../
  5330.     # call parted with current command queue.
  5331.     # This will immediately change the partition table
  5332.     # ----
  5333.     local device=$1
  5334.     local cmds=$2
  5335.     local opts
  5336.     if [ $PARTED_HAVE_ALIGN -eq 1 ];then
  5337.         opts="-a cyl"
  5338.     fi
  5339.     if ! parted $opts -m $device unit cyl $cmds;then
  5340.         systemException "Failed to create partition table" "reboot"
  5341.     fi
  5342.     partedInit $device
  5343. }
  5344. #======================================
  5345. # partedSectorInit
  5346. #--------------------------------------
  5347. function partedSectorInit {
  5348.     # /.../
  5349.     # return start/end sectors of current partitions.
  5350.     # ----
  5351.     IFS=$IFS_ORIG
  5352.     local disk=$1
  5353.     local s_start
  5354.     local s_stopp
  5355.     unset startSectors
  5356.     unset endSectors
  5357.     for i in $(
  5358.         parted -m $disk unit s print | grep ^[1-4]: | cut -f2-3 -d: | tr -d s
  5359.     );do
  5360.         s_start=$(echo $i | cut -f1 -d:)
  5361.         s_stopp=$(echo $i | cut -f2 -d:)
  5362.         if [ -z "$startSectors" ];then
  5363.             startSectors=${s_start}s
  5364.         else
  5365.             startSectors=${startSectors}:${s_start}s
  5366.         fi
  5367.         if [ -z "$endSectors" ];then
  5368.             endSectors=$((s_stopp + 1))s
  5369.         else
  5370.             endSectors=$endSectors:$((s_stopp + 1))s
  5371.         fi
  5372.     done
  5373.     if [ -z "$startSectors" ];then
  5374.         startSectors=1
  5375.     fi
  5376. }
  5377. #======================================
  5378. # partedEndCylinder
  5379. #--------------------------------------
  5380. function partedEndCylinder {
  5381.     # /.../
  5382.     # return end cylinder of given partition, next
  5383.     # partition must start at return value plus 1
  5384.     # ----
  5385.     local part=$(($1 + 3))
  5386.     local IFS=""
  5387.     local header=$(echo $partedOutput | head -n $part | tail -n 1)
  5388.     local ccount=$(echo $header | cut -f3 -d: | tr -d cyl)
  5389.     echo $ccount
  5390. }
  5391. #======================================
  5392. # partedMBToCylinder
  5393. #--------------------------------------
  5394. function partedMBToCylinder {
  5395.     # /.../
  5396.     # convert size given in MB to cylinder count
  5397.     # ----
  5398.     local sizeKB=$(($1 * 1048576))
  5399.     local cylreq=$(($sizeKB / ($partedCylKSize * 1000)))
  5400.     echo $cylreq
  5401. }
  5402. #======================================
  5403. # createPartedInput
  5404. #--------------------------------------
  5405. function createPartedInput {
  5406.     # /.../
  5407.     # evaluate partition instructions and turn them
  5408.     # into a parted command line queue. As soon as the
  5409.     # geometry data would be changed according to the
  5410.     # last partedInit() call the command queue is processed
  5411.     # and the partedInit() will be called afterwards
  5412.     # ----
  5413.     local disk=$1
  5414.     shift
  5415.     local index=0
  5416.     local pcmds
  5417.     local pcmds_fix
  5418.     local partid
  5419.     local pstart
  5420.     local pstopp
  5421.     local value
  5422.     local cmdq
  5423.     #======================================
  5424.     # create list of commands
  5425.     #--------------------------------------
  5426.     for cmd in $*;do
  5427.         pcmds[$index]=$cmd
  5428.         index=$(($index + 1))
  5429.     done
  5430.     index=0
  5431.     index_fix=0
  5432.     #======================================
  5433.     # fix list of commands
  5434.     #--------------------------------------
  5435.     while [ ! -z "${pcmds[$index]}" ];do
  5436.         cmd=${pcmds[$index]}
  5437.         pcmds_fix[$index_fix]=$cmd
  5438.         case $cmd in
  5439.             "d")
  5440.                 partid=${pcmds[$index + 1]}
  5441.                 if ! echo $partid | grep -q "^[0-4]$";then
  5442.                     # make sure there is a ID set for the deletion
  5443.                     index_fix=$(($index_fix + 1))
  5444.                     pcmds_fix[$index_fix]=1
  5445.                 fi
  5446.             ;;
  5447.             "n")
  5448.                 partid=${pcmds[$index + 2]}
  5449.                 if ! echo $partid | grep -q "^[0-4]$";then
  5450.                     # make sure there is a ID set for the creation
  5451.                     index_fix=$(($index_fix + 1))
  5452.                     pcmds_fix[$index_fix]=${pcmds[$index + 1]}
  5453.                     index_fix=$(($index_fix + 1))
  5454.                     pcmds_fix[$index_fix]=4
  5455.                     index=$(($index + 1))
  5456.                 fi
  5457.             ;;
  5458.         esac
  5459.         index=$(($index + 1))
  5460.         index_fix=$(($index_fix + 1))
  5461.     done
  5462.     #======================================
  5463.     # use fixed list and print log info
  5464.     #--------------------------------------
  5465.     unset pcmds
  5466.     pcmds=(${pcmds_fix[*]})
  5467.     unset pcmds_fix
  5468.     index=0
  5469.     #======================================
  5470.     # process commands
  5471.     #--------------------------------------
  5472.     echo "createPartedInput: fixed input: ${pcmds[*]}" 1>&2
  5473.     for cmd in ${pcmds[*]};do
  5474.         case $cmd in
  5475.             #======================================
  5476.             # delete partition
  5477.             #--------------------------------------
  5478.             "d")
  5479.                 partid=${pcmds[$index + 1]}
  5480.                 partid=$(($partid / 1))
  5481.                 cmdq="$cmdq rm $partid"
  5482.                 partedWrite "$disk" "$cmdq"
  5483.                 cmdq=""
  5484.                 ;;
  5485.             #======================================
  5486.             # create new partition
  5487.             #--------------------------------------
  5488.             "n")
  5489.                 partid=${pcmds[$index + 2]}
  5490.                 partid=$(($partid / 1))
  5491.                 pstart=${pcmds[$index + 3]}
  5492.                 if [ "$pstart" = "1" ];then
  5493.                     pstart=$(echo $startSectors | cut -f $partid -d:)
  5494.                 fi
  5495.                 if [ $pstart = "." ];then
  5496.                     # start is next sector according to previous partition
  5497.                     pstart=$(($partid - 1))
  5498.                     if [ $pstart -gt 0 ];then
  5499.                         pstart=$(echo $endSectors | cut -f $pstart -d:)
  5500.                     else
  5501.                         pstart=$(echo $startSectors | cut -f $partid -d:)
  5502.                     fi
  5503.                 fi
  5504.                 pstopp=${pcmds[$index + 4]}
  5505.                 if [ $pstopp = "." ];then
  5506.                     # use rest of the disk for partition end
  5507.                     pstopp=$partedCylCount
  5508.                 elif echo $pstopp | grep -qi M;then
  5509.                     # calculate stopp cylinder from size
  5510.                     pstopp=$(($partid - 1))
  5511.                     if [ $pstopp -gt 0 ];then
  5512.                         pstopp=$(partedEndCylinder $pstopp)
  5513.                     fi
  5514.                     value=$(echo ${pcmds[$index + 4]} | cut -f1 -dM | tr -d +)
  5515.                     value=$(partedMBToCylinder $value)
  5516.                     pstopp=$((1 + $pstopp + $value))
  5517.                 fi
  5518.                 cmdq="$cmdq mkpart primary $pstart $pstopp"
  5519.                 partedWrite "$disk" "$cmdq"
  5520.                 partedSectorInit $imageDiskDevice
  5521.                 cmdq=""
  5522.                 ;;
  5523.             #======================================
  5524.             # change partition ID
  5525.             #--------------------------------------
  5526.             "t")
  5527.                 ptypex=${pcmds[$index + 2]}
  5528.                 partid=${pcmds[$index + 1]}
  5529.                 cmdq="$cmdq set $partid type 0x$ptypex"
  5530.                 partedWrite "$disk" "$cmdq"
  5531.                 cmdq=""
  5532.                 ;;
  5533.         esac
  5534.         index=$(($index + 1))
  5535.     done
  5536. }
  5537. #======================================
  5538. # reloadKernel
  5539. #--------------------------------------
  5540. function reloadKernel {
  5541.     # /.../
  5542.     # reload the given kernel and initrd. This function
  5543.     # checks USB stick devices for a kernel and initrd
  5544.     # and shows them in a dialog window. The selected kernel
  5545.     # and initrd is loaded via kexec.
  5546.     # ----
  5547.     #======================================
  5548.     # check proc/cmdline
  5549.     #--------------------------------------
  5550.     ldconfig
  5551.     mountSystemFilesystems &>/dev/null
  5552.     if ! cat /proc/cmdline | grep -qi "hotfix=1";then
  5553.         umountSystemFilesystems
  5554.         return
  5555.     fi
  5556.     #======================================
  5557.     # check for kexec
  5558.     #--------------------------------------
  5559.     if [ ! -x /sbin/kexec ];then
  5560.         systemException "Can't find kexec" "reboot"
  5561.     fi
  5562.     #======================================
  5563.     # start udev
  5564.     #--------------------------------------
  5565.     touch /etc/modules.conf
  5566.     touch /lib/modules/*/modules.dep
  5567.     udevStart
  5568.     errorLogStart
  5569.     probeDevices
  5570.     #======================================
  5571.     # search hotfix stick
  5572.     #--------------------------------------
  5573.     USBStickDevice kexec
  5574.     if [ $stickFound = 0 ];then
  5575.         systemException "No hotfix USB stick found" "reboot"
  5576.     fi
  5577.     #======================================
  5578.     # mount stick
  5579.     #--------------------------------------
  5580.     if ! mount -o ro $stickDevice /mnt;then
  5581.         systemException "Failed to mount hotfix stick" "reboot"
  5582.     fi
  5583.     #======================================
  5584.     # load kernel
  5585.     #--------------------------------------
  5586.     kexec -l /mnt/linux.kexec --initrd=/mnt/initrd.kexec \
  5587.         --append="$(cat /proc/cmdline | sed -e s"@hotfix=1@@")"
  5588.     if [ ! $? = 0 ];then
  5589.         systemException "Failed to load hotfix kernel" "reboot"
  5590.     fi
  5591.     #======================================
  5592.     # go for gold
  5593.     #--------------------------------------
  5594.     exec kexec -e
  5595. }
  5596. #======================================
  5597. # resizeFilesystem
  5598. #--------------------------------------
  5599. function resizeFilesystem {
  5600.     local deviceResize=$1
  5601.     local callme=$2
  5602.     local ramdisk=0
  5603.     local resize_fs
  5604.     local resize_lucks
  5605.     local check
  5606.     if echo $deviceResize | grep -qi "/dev/ram";then
  5607.         ramdisk=1
  5608.     fi
  5609.     if [ -z "$FSTYPE" ];then
  5610.         probeFileSystem $deviceResize
  5611.     fi
  5612.     resize_lucks="luksResize $deviceResize"
  5613.     if [ "$FSTYPE" = "reiserfs" ];then
  5614.         Echo "Resize Reiser filesystem to full partition space..."
  5615.         resize_fs="resize_reiserfs -q $deviceResize"
  5616.         check="reiserfsck -y $deviceResize"
  5617.     elif [ "$FSTYPE" = "ext2" ];then
  5618.         Echo "Resize EXT2 filesystem to full partition space..."
  5619.         resize_fs="resize2fs -f -F -p $deviceResize"
  5620.         check="e2fsck -p $deviceResize"
  5621.         if [ $ramdisk -eq 1 ];then
  5622.             resize_fs="resize2fs -f $deviceResize"
  5623.         fi
  5624.     elif [ "$FSTYPE" = "ext3" ];then
  5625.         Echo "Resize EXT3 filesystem to full partition space..."
  5626.         resize_fs="resize2fs -f -F -p $deviceResize"
  5627.         check="e2fsck -p $deviceResize"
  5628.         if [ $ramdisk -eq 1 ];then
  5629.             resize_fs="resize2fs -f $deviceResize"
  5630.         fi
  5631.     elif [ "$FSTYPE" = "ext4" ];then
  5632.         Echo "Resize EXT4 filesystem to full partition space..."
  5633.         resize_fs="resize2fs -f -F -p $deviceResize"
  5634.         check="e2fsck -p $deviceResize"
  5635.         if [ $ramdisk -eq 1 ];then
  5636.             resize_fs="resize2fs -f $deviceResize"
  5637.         fi
  5638.     elif [ "$FSTYPE" = "btrfs" ];then
  5639.         Echo "Resize BTRFS filesystem to full partition space..."
  5640.         resize_fs="mount $deviceResize /mnt &&"
  5641.         resize_fs="$resize_fs btrfsctl -r max /mnt;umount /mnt"
  5642.         check="btrfsck $deviceResize"
  5643.     else
  5644.         # don't know how to resize this filesystem
  5645.         return
  5646.     fi
  5647.     if [ -z "$callme" ];then
  5648.         if [ $ramdisk -eq 0 ];then
  5649.             eval $resize_lucks
  5650.             eval $resize_fs
  5651.         else
  5652.             eval $resize_lucks
  5653.             eval $resize_fs
  5654.         fi
  5655.         if [ ! $? = 0 ];then
  5656.             systemException \
  5657.                 "Failed to resize/check filesystem" \
  5658.             "reboot"
  5659.         fi
  5660.         if [ $ramdisk -eq 0 ];then
  5661.             $check
  5662.         fi
  5663.         INITRD_MODULES="$INITRD_MODULES $FSTYPE"
  5664.     else
  5665.         echo $resize_fs
  5666.     fi
  5667. }
  5668. #======================================
  5669. # resetMountCounter
  5670. #--------------------------------------
  5671. function resetMountCounter {
  5672.     local curtype=$FSTYPE
  5673.     local command
  5674.     for device in \
  5675.         $imageRootDevice $imageBootDevice \
  5676.         $imageRecoveryDevice $imageHomeDevice
  5677.     do
  5678.         if [ ! -e $device ];then
  5679.             continue
  5680.         fi
  5681.         probeFileSystem $device
  5682.         if [ "$FSTYPE" = "ext2" ];then
  5683.             command="tune2fs -c -1 -i 0"
  5684.         elif [ "$FSTYPE" = "ext3" ];then
  5685.             command="tune2fs -c -1 -i 0"
  5686.         elif [ "$FSTYPE" = "ext4" ];then
  5687.             command="tune2fs -c -1 -i 0"
  5688.         else
  5689.             # nothing to do here...
  5690.             continue
  5691.         fi
  5692.         eval $command $device 1>&2
  5693.     done
  5694.     FSTYPE=$curtype
  5695. }
  5696. #======================================
  5697. # createFilesystem
  5698. #--------------------------------------
  5699. function createFilesystem {
  5700.     local deviceCreate=$1
  5701.     local blocks=$2
  5702.     if [ "$FSTYPE" = "reiserfs" ];then
  5703.         mkreiserfs -f $deviceCreate $blocks 1>&2
  5704.     elif [ "$FSTYPE" = "ext2" ];then
  5705.         mke2fs -T ext2 -F $deviceCreate $blocks 1>&2
  5706.     elif [ "$FSTYPE" = "ext3" ];then
  5707.         mke2fs -T ext3 -j -F $deviceCreate $blocks 1>&2
  5708.     elif [ "$FSTYPE" = "ext4" ];then
  5709.         mke2fs -T ext4 -j -F $deviceCreate $blocks 1>&2
  5710.     elif [ "$FSTYPE" = "btrfs" ];then
  5711.         if [ ! -z "$blocks" ];then
  5712.             local bytes=$((blocks * 4096))
  5713.             mkfs.btrfs -b $bytes $deviceCreate
  5714.         else
  5715.             mkfs.btrfs $deviceCreate
  5716.         fi
  5717.     else
  5718.         # use ext3 by default
  5719.         mke2fs -T ext3 -j -F $deviceCreate $blocks 1>&2
  5720.     fi
  5721.     if [ ! $? = 0 ];then
  5722.         systemException \
  5723.             "Failed to create filesystem" \
  5724.         "reboot"
  5725.     fi
  5726. }
  5727. #======================================
  5728. # restoreLVMMetadata
  5729. #--------------------------------------
  5730. function restoreLVMPhysicalVolumes {
  5731.     # /.../
  5732.     # restore the pysical volumes by the given restore file
  5733.     # created from vgcfgbackup. It's important to create them
  5734.     # with the same uuid's compared to the restore file
  5735.     # ----
  5736.     local restorefile=$1
  5737.     cat $restorefile | grep -A2 -E 'pv[0-9] {' | while read line;do
  5738.         if [ -z "$uuid" ];then
  5739.             uuid=$(echo $line | grep 'id =' |\
  5740.                 cut -f2 -d= | tr -d \")
  5741.         fi
  5742.         if [ -z "$pdev" ];then
  5743.             pdev=$(echo $line|grep 'device =' |\
  5744.                 cut -f2 -d\" | cut -f1 -d\")
  5745.         fi
  5746.         if [ ! -z "$pdev" ];then
  5747.             pvcreate -u $uuid $pdev
  5748.             unset uuid
  5749.             unset pdev
  5750.         fi
  5751.     done
  5752. }
  5753. #======================================
  5754. # pxeSizeToMB
  5755. #--------------------------------------
  5756. function pxeSizeToMB {
  5757.     local size=$1
  5758.     if [ "$size" = "x" ];then
  5759.         echo . ; return
  5760.     fi
  5761.     local lastc=$(echo $size | sed -e 's@\(^.*\)\(.$\)@\2@')
  5762.     local value=$(echo $size | sed -e 's@\(^.*\)\(.$\)@\1@')
  5763.     if [ "$lastc" = "m" ] || [ "$lastc" = "M" ];then
  5764.         size=$value
  5765.     elif [ "$lastc" = "g" ] || [ "$lastc" = "G" ];then
  5766.         size=$(($value * 1024))
  5767.     fi
  5768.     echo +"$size"M
  5769. }
  5770. #======================================
  5771. # pxePartitionInput
  5772. #--------------------------------------
  5773. function pxePartitionInput {
  5774.     local field=0
  5775.     local count=0
  5776.     local IFS=","
  5777.     for i in $PART;do
  5778.         field=0
  5779.         count=$((count + 1))
  5780.         IFS=";" ; for n in $i;do
  5781.         case $field in
  5782.             0) partSize=$n   ; field=1 ;;
  5783.             1) partID=$n     ; field=2 ;;
  5784.             2) partMount=$n;
  5785.         esac
  5786.         done
  5787.         partSize=$(pxeSizeToMB $partSize)
  5788.         if [ $partID = "S" ];then
  5789.             partID=82
  5790.         fi
  5791.         if [ $partID = "L" ];then
  5792.             partID=83
  5793.         fi
  5794.         if [ $partID = "V" ];then
  5795.             partID=8e
  5796.         fi
  5797.         if [ $count -eq 1 ];then
  5798.             echo -n "n p $count 1 $partSize "
  5799.         else
  5800.             echo -n "n p $count . $partSize "
  5801.         fi
  5802.         if [ $partID = "82" ] || [ $partID = "8e" ];then
  5803.             echo -n "t $count $partID "
  5804.         fi
  5805.     done
  5806.     echo "w q"
  5807. }
  5808. #======================================
  5809. # pxeSwapDevice
  5810. #--------------------------------------
  5811. function pxeSwapDevice {
  5812.     local field=0
  5813.     local count=0
  5814.     local device
  5815.     local IFS=","
  5816.     for i in $PART;do
  5817.         field=0
  5818.         count=$((count + 1))
  5819.         IFS=";" ; for n in $i;do
  5820.         case $field in
  5821.             0) partSize=$n   ; field=1 ;;
  5822.             1) partID=$n     ; field=2 ;;
  5823.             2) partMount=$n;
  5824.         esac
  5825.         done
  5826.         if test $partID = "82" -o $partID = "S";then
  5827.             device=$(ddn $DISK $count)
  5828.             waitForStorageDevice $device
  5829.             echo $device
  5830.             return
  5831.         fi
  5832.     done
  5833. }
  5834. #======================================
  5835. # startUtimer
  5836. #--------------------------------------
  5837. function startUtimer {
  5838.     local utimer=/usr/bin/utimer
  5839.     if [ ! -x $utimer ];then
  5840.         utimer=/utimer
  5841.     fi
  5842.     if [ -x $utimer ];then
  5843.         if [ ! -e /tmp/utimer ];then
  5844.             ln -s $UTIMER_INFO /tmp/utimer
  5845.         fi
  5846.         $utimer
  5847.         export UTIMER=$(cat /var/run/utimer.pid)
  5848.         if [ -f /iprocs ];then
  5849.             cat /iprocs | grep -v UTIMER_PID > /iprocs
  5850.         fi
  5851.         echo UTIMER_PID=$UTIMER >> /iprocs
  5852.     fi
  5853. }
  5854. #======================================
  5855. # setupBootPartition
  5856. #--------------------------------------
  5857. function setupBootPartition {
  5858.     local pSearch=83
  5859.     local mpoint
  5860.     if [ "$haveLVM" = "yes" ];then
  5861.         #======================================
  5862.         # lvmboot
  5863.         #--------------------------------------
  5864.         test -z "$bootid" && export bootid=2
  5865.         mpoint=lvmboot
  5866.     elif [ "$haveBtrFS" = "yes" ];then
  5867.         #======================================
  5868.         # btrboot
  5869.         #--------------------------------------
  5870.         test -z "$bootid" && export bootid=2
  5871.         mpoint=btrboot
  5872.     elif [ "$haveClicFS" = "yes" ];then
  5873.         #======================================
  5874.         # clicboot
  5875.         #--------------------------------------
  5876.         test -z "$bootid" && export bootid=3
  5877.         mpoint=clicboot
  5878.     elif \
  5879.         [ "$loader" = "syslinux" ] || \
  5880.         [ "$loader" = "extlinux" ] || \
  5881.         [ "$haveLuks" = "yes" ]
  5882.     then
  5883.         #======================================
  5884.         # syslboot / luksboot
  5885.         #--------------------------------------
  5886.         if [ -z "$bootid" ];then
  5887.             test "$loader" = "syslinux" && pSearch=6
  5888.             for i in 4 3 2;do
  5889.                 pType=$(partitionID $imageDiskDevice $i)
  5890.                 if [ "$pType" = $pSearch ];then
  5891.                     export bootid=$i
  5892.                     break
  5893.                 fi
  5894.             done
  5895.         fi
  5896.         if [ "$haveLuks" = "yes" ];then
  5897.             mpoint=luksboot
  5898.         else
  5899.             mpoint=syslboot
  5900.         fi
  5901.     else
  5902.         #======================================
  5903.         # no separate boot partition
  5904.         #--------------------------------------
  5905.         if [ -z "$bootid" ];then
  5906.             export bootid=1
  5907.         fi
  5908.         return
  5909.     fi
  5910.     if [ -z "$imageBootDevice" ];then
  5911.         export imageBootDevice=$(ddn $imageDiskDevice $bootid)
  5912.     fi
  5913.     mkdir -p /mnt/$mpoint
  5914.     mount $imageBootDevice /mnt/$mpoint
  5915.     cp -a /mnt/boot /mnt/$mpoint
  5916.     if [ -e /boot.tgz ];then
  5917.         tar -xf /boot.tgz -C /mnt/$mpoint
  5918.     fi
  5919.     rm -rf /mnt/boot
  5920.     mkdir  /mnt/boot
  5921.     mount --bind \
  5922.         /mnt/$mpoint/boot /mnt/boot
  5923. }
  5924. #======================================
  5925. # initialize
  5926. #--------------------------------------
  5927. function initialize {
  5928.     #======================================
  5929.     # Check for hotfix kernel
  5930.     #--------------------------------------
  5931.     reloadKernel
  5932.     #======================================
  5933.     # Prevent blank screen
  5934.     #--------------------------------------
  5935.     if [ -x /usr/bin/setterm ];then
  5936.         setterm -powersave off -blank 0
  5937.     fi
  5938.     #======================================
  5939.     # Start boot timer (first stage)
  5940.     #--------------------------------------
  5941.     startUtimer
  5942. }
  5943.  
  5944. # vim: set noexpandtab:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement