YoungJules

piprepare

Nov 14th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. die( )
  4. {
  5.     echo $1 >&2 ; exit 1
  6. }
  7.  
  8. doit( )
  9. {
  10.     echo "+ $*" >&2 ; $* || die "command failed"
  11. }
  12.  
  13. unmount( )
  14. {
  15.     for dev in `mount | grep ^$1 | cut -f 1 -d ' '` ; do doit umount $dev ; done
  16. }
  17.  
  18. last( )
  19. {
  20.     ls -d $* 2>/dev/null | tail -1
  21. }
  22.  
  23. check( )
  24. {
  25.     result=1
  26.     partitions=( `fdisk -l | grep ^$1 | cut -d " " -f 1` )
  27.     if [ ${#partitions[@]} -eq 2 ]
  28.     then
  29.         file -sL ${partitions[0]} | grep \"System > /dev/null 2>&1 &&
  30.         file -sL ${partitions[1]} | grep \"Storage\" > /dev/null 2>&1 &&
  31.         result=0
  32.     elif [ ${#partitions[@]} -eq 0 ]
  33.     then
  34.         die "Error: $1 is invalid"
  35.     fi
  36.    
  37.     return $result
  38. }
  39.  
  40. [ $# -ne 1 ] && die "Usage: sudo `basename $0` /dev/sdb"
  41.  
  42. device=$1
  43. device1=${device}1
  44. device2=${device}2
  45.  
  46. bootloader=`last build.OpenELEC-RPi.arm-devel/bcm2835-bootloader-*`
  47. system=`last target/OpenELEC-RPi.arm-devel-*.system`
  48. kernel=`last target/OpenELEC-RPi.arm-devel-*.kernel`
  49.  
  50. mount="/tmp/media/System"
  51. storage="/tmp/media/Storage"
  52.  
  53. [ -e "$device" ] || die "Error: $device is not found"
  54. [ "$bootloader" == "" ] && die "Error: Unable to find bootloader - you need to run this script in your openelectv build directory"
  55. [ "$system" == "" ] && die "Error: Unable to find system - you need to run this script in your openelectv build directory"
  56. [ "$kernel" == "" ] && die "Error: Unable to find kernel - you need to run this script in your openelectv build directory"
  57.  
  58. echo "Updating $device with:"
  59. echo
  60. echo "bootloader = $bootloader"
  61. echo "system     = $system"
  62. echo "kernel     = $kernel"
  63. echo
  64. echo -n "Do you want to continue (y/n)? "
  65.  
  66. read answer
  67.  
  68. [ "$answer" != "y" ] && exit 0
  69.  
  70. echo
  71.  
  72. unmount $device
  73.  
  74. if ! check $device
  75. then
  76.     echo
  77.     echo "This sd card has not be paritioned for openelec - continuing will destroy"
  78.     echo "the contents of the disk."
  79.     echo
  80.     echo -n "Are you sure you want to continue (y/n)? "
  81.  
  82.     read answer
  83.  
  84.     [ "$answer" != "y" ] && exit 0
  85.  
  86.     echo
  87.  
  88.     doit parted -s $device mklabel msdos
  89.     doit parted -s $device unit cyl mkpart primary fat32 -- 0 16
  90.     doit parted -s $device set 1 boot on
  91.     doit parted -s $device unit cyl mkpart primary ext2 -- 16 -2
  92.  
  93.     doit mkfs.vfat -n System $device1
  94.     doit mkfs.ext4 -L Storage $device2
  95.     doit partprobe
  96. fi
  97.  
  98. doit mkdir -p $mount
  99. doit mount $device1 $mount
  100.  
  101. doit cp $bootloader/start.elf $mount
  102. doit cp $bootloader/bootcode.bin $mount
  103. doit cp $bootloader/fixup.dat $mount
  104.  
  105. doit cp $system $mount/SYSTEM
  106. doit cp $kernel $mount/kernel.img
  107.  
  108. echo "boot=/dev/mmcblk0p1 disk=/dev/mmcblk0p2 ssh quiet" > cmdline.txt
  109. doit mv cmdline.txt $mount
  110.  
  111. doit mkdir -p $storage
  112. doit mount $device2 $storage
  113.  
  114. if [ -f "autostart.sh" ]
  115. then
  116.     doit mkdir -p $storage/.config
  117.     doit cp autostart.sh $storage/.config
  118.     doit chmod +x $storage/.config/autostart.sh
  119. fi
  120.  
  121. doit sync
  122.  
  123. doit umount $device1
  124. doit umount $device2
Advertisement
Add Comment
Please, Sign In to add comment