Advertisement
Guest User

OpenELEC create_installstick_osx.sh

a guest
May 22nd, 2012
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.78 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. read_input(){
  4.     local MESSAGE="${1}"
  5.     if [ -n "${MESSAGE}" ];then
  6.         echo "${MESSAGE}"
  7.     fi
  8.     read INPUT
  9. }
  10.  
  11. write_warning(){
  12.     local MESSAGE=$1
  13.     echo "WARNING: ${MESSAGE}"
  14. }
  15.  
  16. write_error(){
  17.     local MESSAGE=$1
  18.     echo "ERROR: ${MESSAGE}"
  19. }
  20.  
  21. plist_value() {
  22.     echo "$1" | xpath "//key[.='$2']/following-sibling::*[1]/text()" 2> /dev/null
  23. }
  24.  
  25. choose_install_device(){
  26.     echo ""
  27.     echo "#########################################################"
  28.     echo "#                                                       #"
  29.     echo "#            OpenELEC Installer for Mac OS X            #"
  30.     echo "#                                                       #"
  31.     echo "#########################################################"
  32.     echo "#                                                       #"
  33.     echo "#     This will wipe ALL data off your chosen drive     #"
  34.     echo "#    Please read the instructions and use carefully..   #"
  35.     echo "#                                                       #"
  36.     echo "#########################################################"
  37.     echo ""
  38.     echo "The following devices were found:"
  39.     echo ""
  40.     local devices=$(diskutil list -plist | xpath "//key[.='WholeDisks']/following-sibling::*[1]//string" 2>/dev/null | sed 's/\<string\>//g' | sed 's/\<\/string\>/ /g')
  41.     echo "[Device]  [Media name]  [Total size]"
  42.     for device in $devices; do
  43.         local device_info=$(diskutil info -plist $device)
  44.         local size=$(plist_value "$device_info" TotalSize)
  45.         local mediaName=$(plist_value "$device_info" MediaName)
  46.         echo "$device  $mediaName  $size bytes"
  47.     done
  48.     echo ""
  49.     INPUT=""
  50.     while [ -z "${INPUT}" ];do
  51.         read_input "Please enter the install device (e.g. disk1): "
  52.         echo ""
  53.     done
  54.     INSTALL_DEVICE="${INPUT}"
  55. }
  56.  
  57. check_install_device(){
  58.     diskutil list ${INSTALL_DEVICE} > /dev/null
  59.     if [ $? == 0 ]; then
  60.         diskutil list /dev/"${INSTALL_DEVICE}"
  61.     else
  62.         write_error "Device /dev/${INSTALL_DEVICE} appears to be invalid"
  63.         echo ""
  64.         exit 1
  65.     fi
  66.     echo ""
  67.     write_warning "*** ALL DATA WILL BE WIPED FROM /dev/${INSTALL_DEVICE} ***"
  68.     echo ""
  69.     read -p "Are you sure you want to continue? " -n 1
  70.     if [[ ! $REPLY =~ ^[Yy]$ ]];then
  71.         echo ""
  72.         echo ""
  73.         echo "Goodbye!"
  74.         echo ""
  75.         exit 1
  76.     fi
  77. }
  78.  
  79. unmount_install_device_partitions(){
  80.     diskutil unmountDisk /dev/${INSTALL_DEVICE} &> /dev/null
  81. }
  82.  
  83. prepare_install_device(){
  84.     echo ""
  85.     echo ""
  86.     echo "Creating OpenELEC Install Stick"
  87.     echo ""
  88.     diskutil eraseDisk MS-DOS INSTALL MBR /dev/${INSTALL_DEVICE} &> /dev/null
  89.     diskutil unmountDisk /dev/${INSTALL_DEVICE} &> /dev/null
  90.     curl -O http://chewitt.openelec.tv/osx_installstick_files.tar.gz &> /dev/null
  91.     tar -xzvf osx_installstick_files.tar.gz &> /dev/null
  92.     chmod 755 osx_installstick_files/*
  93.     cat osx_installstick_files/mbr.bin > /dev/${INSTALL_DEVICE}
  94.     osx_installstick_files/syslinux -f /dev/${INSTALL_DEVICE}s1 &> /dev/null
  95. }
  96.  
  97. create_partitions(){
  98.     diskutil unmountDisk /dev/${INSTALL_DEVICE} &> /dev/null
  99.     fdisk -e /dev/${INSTALL_DEVICE} < osx_installstick_files/fdisk.input &> /dev/null
  100. }
  101.  
  102. install_files(){
  103.     diskutil mountDisk /dev/${INSTALL_DEVICE} &> /dev/null
  104.     cp -a target/KERNEL /Volumes/INSTALL/
  105.     cp -a target/SYSTEM /Volumes/INSTALL/  
  106.     cp -a sample.conf/syslinux_installer.cfg /Volumes/INSTALL/syslinux.cfg
  107. }
  108.  
  109. final_cleanup(){
  110.     diskutil unmountDisk /dev/${INSTALL_DEVICE} &> /dev/null
  111.     fsck_msdos -y /dev/${INSTALL_DEVICE}s1 &> /dev/null
  112.     echo "ALL DONE! ..it's now safe to remove your USB stick"
  113.     echo ""
  114. }
  115.  
  116. main(){
  117.     if [ $UID != 0 ];then
  118.         echo "This script *must* be run as root"
  119.         exit 1
  120.     fi
  121.  
  122.     choose_install_device
  123.     check_install_device
  124.     unmount_install_device_partitions
  125.     prepare_install_device
  126.     create_partitions
  127.     install_files  
  128.     final_cleanup
  129.     exit 0
  130. }
  131.  
  132. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement