Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.18 KB | None | 0 0
  1. #!/bin/sh
  2. set -e
  3.  
  4. # Required Syslinux version
  5. sysver=4.05
  6. sysbin=syslinux
  7. extbin=extlinux
  8. sysmbr=/usr/share/syslinux/mbr_c.bin
  9. sysmbr2=/usr/lib/syslinux/mbr_c.bin
  10. mattrbin=mattrib
  11.  
  12. # Directories for ldlinux.sys and bundled syslinux binary
  13. sysdir=/liberte/boot/syslinux
  14. systmpdir=
  15. mntexec=
  16.  
  17. # Filesystem types (for auto mode)
  18. fs_fat=4d44
  19. fs_ext=ef53
  20.  
  21.  
  22. if [ ! \( $# = 1 -o \( $# = 2 -a "$2" = nombr \) -o "$1" = automagic \) ]; then
  23.     cat <<EOF
  24. This script installs SYSLINUX or EXTLINUX on a device with
  25. Liberté Linux.
  26.  
  27. You need the following installed.
  28.  
  29.     Syslinux ${sysver} (Gentoo: sys-boot/syslinux)
  30.         [if unavailable, bundled 32-bit version will be used]
  31.     udev + sysfs  (Gentoo: sys-fs/udev)
  32.         [available on most modern Linux distributions]
  33.  
  34. Run setup.sh as:
  35.  
  36.     setup.sh auto     [nombr]
  37.     setup.sh /dev/XXX [nombr]
  38.  
  39. If the optional <nombr> parameter is specified, and /dev/XXX
  40. is a partition, then the block device's master boot record
  41. will be unaffected. Use this parameter when a custom bootloader
  42. is installed in the MBR.
  43. EOF
  44.     exit 1
  45. fi
  46.  
  47.  
  48. # Arguments
  49. dev="$1"
  50. nombr="$2"
  51.  
  52.  
  53. # Error handling
  54. error() {
  55.     echo "$@"
  56.     exit 1
  57. }
  58.  
  59.  
  60. # Handle auto mode
  61. if [ "${dev}" = auto ]; then
  62.     echo "*** Determining device name ***"
  63.     shift
  64.  
  65.     mpoint=`readlink -f "$0"`
  66.     if [ "${mpoint}" = "${mpoint%/liberte/setup.sh}" ]; then
  67.         error "Bad script path"
  68.     fi
  69.     mpoint="${mpoint%/liberte/setup.sh}"
  70.  
  71.     if ! mountpoint -q "${mpoint}"; then
  72.         error "${mpoint} is not a mount point"
  73.     fi
  74.  
  75.     fstype=`stat -f -c "%t" "${mpoint}"`
  76.     fsdev=/dev/block/`mountpoint -d "${mpoint}"`
  77.     fsdev=`readlink -f "${fsdev}"`
  78.  
  79.     case "${fstype}" in
  80.         ${fs_fat})
  81.             echo "${fsdev} is mounted as FAT(32) on ${mpoint}"
  82.             setup=`mktemp`
  83.             cp $0 "${setup}"
  84.  
  85.             exec /bin/sh "${setup}" automagic "${mpoint}" "${fsdev}" "$@"
  86.             ;;
  87.  
  88.         ${fs_ext})
  89.             echo "${fsdev} is mounted as ext[234] on ${mpoint}"
  90.             exec /bin/sh "$0" "${fsdev}" "$@"
  91.             ;;
  92.  
  93.         *)
  94.             error "Unknown filesystem type"
  95.             ;;
  96.     esac
  97.  
  98.     error "Internal error"
  99. fi
  100.  
  101. if [ "${dev}" = automagic ]; then
  102.     mpoint="$2"
  103.     shift 2
  104.  
  105.     echo "Unmounting ${mpoint}"
  106.     umount "${mpoint}"
  107.  
  108.     /bin/sh "$0" "$@"
  109.     exec rm "$0"
  110.  
  111.     error "Internal error"
  112. fi
  113.  
  114.  
  115. # Check that the argument is a block device
  116. if [ ! -b "${dev}" ]; then
  117.     error "${dev} is not a block device."
  118. fi
  119.  
  120.  
  121. # Check for pre-4.x Syslinux (without the -v switch)
  122. sysok=0
  123. extok=0
  124. if ! ${sysbin} -v 1>/dev/null 2>&1; then
  125.     echo "Syslinux v4+ not found, will use bundled binary"
  126. elif [ ! -e ${sysmbr}  -a  ! -e ${sysmbr2} ]; then
  127.     echo "${sysmbr} or ${sysmbr2} not found, will use bundled Syslinux"
  128. else
  129.     # Check for wrong Syslinux version (exact match required)
  130.     havesysver=`${sysbin} -v 2>&1 | cut -d' ' -f2`
  131.     if [ "${havesysver}" != ${sysver} ]; then
  132.         echo "Syslinux v${havesysver} detected, need v${sysver}"
  133.     else
  134.         sysok=1
  135.         if [ -e ${sysmbr2} ]; then
  136.             sysmbr=${sysmbr2}
  137.         fi
  138.  
  139.         if ! ${extbin} -v 1>/dev/null 2>&1; then
  140.             echo "EXTLINUX not found, will use bundled binary if installing to ext[234]"
  141.         else
  142.             extok=1
  143.         fi
  144.     fi
  145. fi
  146.  
  147.  
  148. # udev properties accessor
  149. udevprop() {
  150.     local devpath="$1" prop="$2"
  151.     udevadm info -q property -p "${devpath}" | sed -n "s/^${prop}=//p"
  152. }
  153.  
  154.  
  155. # 32-bit executability checker
  156. checkexec() {
  157.     local binary="$1"
  158.  
  159.     # 32-bit: /lib/ld-linux.so.2, 64-bit: /lib/ld-linux-x86-64.so.2
  160.     if [ ! -e /lib/ld-linux.so.2 ]; then
  161.         echo "WARNING: it looks like 32-bit ELF binaries are unsupported on this system"
  162.     # Use test(1) from coreutils (it calls access(2), honoring MS_NOEXEC and MS_RDONLY)
  163.     elif [ ! -x "${binary}" ] || { [ -e /usr/bin/test ] && ! /usr/bin/test -x "${binary}"; }; then
  164.         echo "WARNING: executing bundled binaries will fail"
  165.  
  166.         binary=`dirname "$(readlink -f "${binary}")"`
  167.         while ! mountpoint -q ${binary}; do
  168.             binary=`dirname "${binary}"`
  169.         done
  170.  
  171.         if grep -q " ${binary} .*\<noexec\>" /proc/mounts; then
  172.             mntexec="${binary}"
  173.             echo "WARNING: ${mntexec} is mounted with noexec option"
  174.             echo "Remounting ${mntexec} with exec permissions"
  175.             mount -o remount,exec "${mntexec}"
  176.         fi
  177.     fi
  178. }
  179.  
  180.  
  181. # Check for wrong block device type (highly unlikely)
  182. devpath=`udevadm info -q path -n "${dev}"`
  183. devtype=`udevprop ${devpath} DEVTYPE`
  184. if [ "${devtype}" != partition -a "${devtype}" != disk ]; then
  185.     error "${dev} is neither a disk nor a disk partition"
  186. fi
  187.  
  188.  
  189. # Check and normalize filesystem type
  190. devfs=`udevprop "${devpath}" ID_FS_TYPE`
  191. case "${devfs}" in
  192.     '')
  193.         error "${dev} is not formatted, format it as FAT/ext2 or specify a partition instead"
  194.         ;;
  195.     vfat|msdos)
  196.         echo "Detected FAT(32) filesystem, will install using SYSLINUX"
  197.         devfs=fat
  198.         ;;
  199.     ext[234])
  200.         echo "Detected ext[234] filesystem, will install using EXTLINUX"
  201.         devfs=ext2
  202.         ;;
  203.     *)
  204.         error "${dev} has a [${devfs}] filesystem type, need FAT/ext2"
  205.         ;;
  206. esac
  207.  
  208.  
  209. if [ ${devfs} = fat ]; then
  210.  
  211.     # Check for mounted filesystem (be a bit paranoid, so no '$' after ${dev})
  212.     if cut -d' ' -f1 /proc/mounts | grep -q "^${dev}"; then
  213.         error "${dev} is mounted, unmount it or wait for auto-unmount"
  214.     fi
  215.  
  216.  
  217.     # Check for installation directory
  218.     mntdir=`mktemp -d`
  219.     mount -r -t vfat -o noatime,nosuid,nodev,noexec "${dev}" ${mntdir}
  220.     if [ -e ${mntdir}${sysdir}/syslinux-x86.tbz ]; then
  221.         hassysdir=1
  222.     else
  223.         hassysdir=0
  224.     fi
  225.  
  226.     # Copy bundled syslinux binary if system versions are wrong
  227.     if [ ${hassysdir} = 1  -a  ${sysok} = 0 ]; then
  228.         echo "Using bundled 32-bit SYSLINUX/Mtools binaries and MBR"
  229.  
  230.         systmpdir=`mktemp -d`
  231.         tar -xpjf ${mntdir}${sysdir}/syslinux-x86.tbz -C ${systmpdir}
  232.  
  233.         sysbin=${systmpdir}/syslinux
  234.         sysmbr=${systmpdir}/mbr_c.bin
  235.  
  236.         mattrbin=${systmpdir}/mattrib
  237.         export PATH=${systmpdir}:"${PATH}"
  238.  
  239.         checkexec ${sysbin}
  240.     fi
  241.  
  242.     # Create OTFE directory so that it can be hidden, too
  243.     if [ ! -e ${mntdir}/otfe ]; then
  244.         mount -o remount,rw ${mntdir}
  245.         mkdir ${mntdir}/otfe
  246.     fi
  247.  
  248.     umount -l ${mntdir}
  249.     rmdir     ${mntdir}
  250.  
  251.     if [ ${hassysdir} = 0 ]; then
  252.         error "Directory ${sysdir} not found or incorrect on ${dev}"
  253.     fi
  254.  
  255.  
  256.     # Install SYSLINUX
  257.     echo "*** Installing SYSLINUX on ${dev} ***"
  258.     ${sysbin} -i -d ${sysdir} "${dev}"
  259.  
  260.     # Hide directories
  261.     echo "*** Hiding top-level directories ***"
  262.  
  263.     unset  MTOOLSRC
  264.     export MTOOLS_SKIP_CHECK=1
  265.     export MTOOLS_FAT_COMPATIBILITY=1
  266.  
  267.     ${mattrbin} -i "${dev}" +h ::/liberte ::/otfe
  268.     ${mattrbin} -i "${dev}" +h ::/EFI 2>/dev/null || :
  269.  
  270.     if [ -n "${mntexec}" ]; then
  271.         echo "Remounting ${mntexec} with noexec option"
  272.         mount -o remount,noexec "${mntexec}"
  273.     fi
  274.  
  275. elif [ ${devfs} = ext2 ]; then
  276.  
  277.     devdir=`grep "^${dev} " /proc/mounts | head -n 1 | cut -d' ' -f2`
  278.     if [ -n "${devdir}" ]  &&  mountpoint -q "${devdir}"; then
  279.         echo "Detected ${dev} mount on ${devdir}"
  280.     else
  281.         error "${dev} is not mounted, please manually mount or auto-mount it"
  282.     fi
  283.  
  284.     if [ ! -e "${devdir}"${sysdir}/syslinux-x86.tbz ]; then
  285.         error "Directory ${sysdir} not found or incorrect in ${devdir}"
  286.     elif [ ${extok} = 0 ]; then
  287.         echo "Using bundled 32-bit EXTLINUX binary and MBR"
  288.  
  289.         systmpdir=`mktemp -d`
  290.         tar -xpjf "${devdir}"${sysdir}/syslinux-x86.tbz -C ${systmpdir}
  291.  
  292.         extbin=${systmpdir}/extlinux
  293.         sysmbr=${systmpdir}/mbr_c.bin
  294.  
  295.         checkexec ${extbin}
  296.     fi
  297.  
  298.     # Install EXTLINUX
  299.     echo "*** Installing EXTLINUX in ${devdir} ***"
  300.     ${extbin} -i "${devdir}"${sysdir}
  301.  
  302.     if [ -n "${mntexec}" ]; then
  303.         echo "Remounting ${mntexec} with noexec option"
  304.         mount -o remount,noexec "${mntexec}"
  305.     fi
  306.  
  307. else
  308.     error "Internal error"
  309. fi
  310.  
  311.  
  312. # If necessary, install Syslinux-supplied MBR
  313. if [ -z "${nombr}" -a ${devtype} = partition ]; then
  314.     # Get the parent device
  315.     rdevpath=`dirname "${devpath}"`
  316.     rdev=`udevprop "${rdevpath}" DEVNAME`
  317.  
  318.     echo "*** Installing bootloader to the MBR of ${rdev} ***"
  319.  
  320.     # Check that the parent device is a block device
  321.     if [ ! -b "${rdev}" ]; then
  322.         error "${rdev} is not a block device."
  323.     fi
  324.  
  325.  
  326.     # Check that the parent device is indeed a disk
  327.     rdevtype=`udevprop "${rdevpath}" DEVTYPE`
  328.     if [ "${rdevtype}" != disk ]; then
  329.         error "${rdev} is not a disk, but ${rdevtype}, aborting"
  330.     fi
  331.  
  332.  
  333.     # Check that the disk is a removable device
  334.     if [ -e /sys"${rdevpath}"/removable ]; then
  335.         if [ "`cat /sys"${rdevpath}"/removable`" = 0 ]; then
  336.             echo "WARNING: ${rdev} is not a removable device"'!'
  337.             echo "Press Ctrl-C now to abort (waiting 10 seconds)..."
  338.             sleep 10
  339.         fi
  340.     fi
  341.  
  342.  
  343.     # Check that the partition table is MSDOS
  344.     ptable=`udevprop "${rdevpath}" ID_PART_TABLE_TYPE`
  345.     if [ "${ptable}" != dos ]; then
  346.         error "Partition table is of type [${ptable}], need MS-DOS"
  347.     fi
  348.  
  349.  
  350.     # Determine partition number
  351.     if [ ! -e /sys"${devpath}"/partition ]; then
  352.         error "Unable to reliably determine partition number of ${dev}"
  353.     fi
  354.     devpart=`cat /sys"${devpath}"/partition`
  355.  
  356.  
  357.     # Install Syslinux's MBR (less than 512B, doesn't overwrite the partition table)
  358.     if ! type sfdisk 1>/dev/null 2>&1; then
  359.         error "sfdisk not found, cannot mark partition ${devpart} active"
  360.     fi
  361.  
  362.     echo running sfdisk -q -A"${devpart}" "${rdev}"
  363.     sfdisk -q -A"${devpart}" "${rdev}"
  364.     echo ran sfdisk -q -A"${devpart}" "${rdev}"
  365.     dd bs=440 count=1 iflag=fullblock conv=notrunc if=${sysmbr} of="${rdev}"
  366. fi
  367.  
  368.  
  369. # Remove temporary directories
  370. if [ -n "${systmpdir}" ]; then
  371.     rm -r ${systmpdir}
  372. fi
  373.  
  374.  
  375. # Synchronize
  376. echo "*** Synchronizing ***"
  377. sync
  378.  
  379. echo "*** Done ***"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement