install-rpi.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env bash
  2. # This file is part of the OpenADK project. OpenADK is copyrighted
  3. # material, please see the LICENCE file in the top-level directory.
  4. if [ $(id -u) -ne 0 ];then
  5. echo "Installation is only possible as root"
  6. exit 1
  7. fi
  8. f=0
  9. for tool in parted sfdisk mkfs.vfat mkfs.ext4;do
  10. if ! which $tool >/dev/null; then
  11. echo "Checking if $tool is installed... failed"
  12. f=1
  13. fi
  14. done
  15. if [ $f -eq 1 ];then exit 1;fi
  16. datadir=0
  17. keep=0
  18. while getopts "dk" ch; do
  19. case $ch in
  20. d)
  21. datadir=1
  22. ;;
  23. k)
  24. keep=1
  25. ;;
  26. esac
  27. done
  28. shift $((OPTIND - 1))
  29. if [ -z $1 ];then
  30. echo "Please give your SD card device as first parameter"
  31. exit 1
  32. else
  33. if [ -z $2 ];then
  34. echo "Please give your install tar archive as second parameter"
  35. exit 1
  36. fi
  37. if [ -f $2 ];then
  38. echo "Installing $2 on $1"
  39. else
  40. echo "$2 is not a file, exiting"
  41. exit 1
  42. fi
  43. if [ -b $1 ];then
  44. echo "Using $1 as SD card disk for installation"
  45. echo "WARNING: This will destroy all data on $1 - type Yes to continue!"
  46. read y
  47. if [ "$y" = "Yes" ];then
  48. env LC_ALL=C sfdisk -l $1 2>&1 |grep 'No medium'
  49. if [ $? -eq 0 ];then
  50. echo "No medium found"
  51. exit 1
  52. else
  53. echo "Starting with installation"
  54. fi
  55. else
  56. echo "Exiting."
  57. exit 1
  58. fi
  59. else
  60. echo "Sorry $1 is not a block device"
  61. exit 1
  62. fi
  63. fi
  64. if [ $(mount | grep $1| wc -l) -ne 0 ];then
  65. echo "Block device $1 is in use, please umount first"
  66. exit 1
  67. fi
  68. echo "Wiping existing partitions"
  69. dd if=/dev/zero of=$1 bs=512 count=1 >/dev/null 2>&1
  70. sync
  71. echo "Create partition and filesystem for raspberry pi"
  72. rootpart=${1}2
  73. parted -s $1 mklabel msdos
  74. sleep 2
  75. maxsize=$(env LC_ALL=C parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
  76. if [ $datadir -eq 0 ];then
  77. rootsize=$(($maxsize-2))
  78. else
  79. rootsize=$(($maxsize-34))
  80. datasize=$(($maxsize-2))
  81. fi
  82. parted -s $1 unit cyl mkpart primary fat32 -- 0 16
  83. if [ $datadir -eq 0 ];then
  84. parted -s $1 unit cyl mkpart primary ext2 -- 16 $rootsize
  85. parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
  86. sfdisk --change-id $1 3 88 >/dev/null 2>&1
  87. else
  88. parted -s $1 unit cyl mkpart primary ext2 -- 16 $rootsize
  89. parted -s $1 unit cyl mkpart primary ext2 $rootsize $datasize
  90. parted -s $1 unit cyl mkpart primary fat32 $datasize $maxsize
  91. parted -s $1 set 1 boot on
  92. sfdisk --change-id $1 4 88 >/dev/null 2>&1
  93. fi
  94. sleep 2
  95. mkfs.vfat ${1}1 >/dev/null
  96. mkfs.ext4 -F -q -O ^huge_file ${1}2
  97. if [ $datadir -eq 1 ];then
  98. if [ $keep -eq 0 ];then
  99. mkfs.ext4 -F -q -O ^huge_file ${1}3
  100. fi
  101. fi
  102. sync
  103. sleep 2
  104. tmp=$(mktemp -d)
  105. mount -t ext4 ${rootpart} $tmp
  106. mkdir $tmp/boot
  107. if [ $datadir -eq 1 ];then
  108. if [ $keep -eq 0 ];then
  109. mkdir $tmp/data
  110. mount -t ext4 ${1}3 $tmp/data
  111. mkdir $tmp/data/mpd $tmp/data/xbmc
  112. umount $tmp/data
  113. fi
  114. fi
  115. mount -t vfat ${1}1 $tmp/boot
  116. sleep 1
  117. echo "Extracting install archive"
  118. tar -C $tmp -xzpf $2
  119. echo "Fixing permissions"
  120. chmod 1777 $tmp/tmp
  121. chmod 4755 $tmp/bin/busybox
  122. if [ $datadir -eq 1 ];then
  123. echo "/dev/mmcblk0p3 /data ext4 rw 0 0" >>$tmp/etc/fstab
  124. fi
  125. umount $tmp/boot
  126. umount $tmp
  127. echo "Successfully installed."
  128. exit 0