install.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/usr/bin/env bash
  2. if [ $(id -u) -ne 0 ];then
  3. printf "Installation is only possible as root\n"
  4. exit 1
  5. fi
  6. printf "Checking if sfdisk is installed"
  7. sfdisk=$(which sfdisk)
  8. if [ ! -z $sfdisk -a -x $sfdisk ];then
  9. printf "...okay\n"
  10. else
  11. printf "...failed\n"
  12. exit 1
  13. fi
  14. printf "Checking if parted is installed"
  15. parted=$(which parted)
  16. if [ ! -z $parted -a -x $parted ];then
  17. printf "...okay\n"
  18. else
  19. printf "...failed\n"
  20. exit 1
  21. fi
  22. printf "Checking if mke2fs is installed"
  23. mke2fs=$(which mke2fs)
  24. if [ ! -z $mke2fs -a -x $mke2fs ];then
  25. printf "...okay\n"
  26. else
  27. printf "...failed\n"
  28. exit 1
  29. fi
  30. printf "Checking if tune2fs is installed"
  31. tune2fs=$(which tune2fs)
  32. if [ ! -z $tune2fs -a -x $tune2fs ];then
  33. printf "...okay\n"
  34. else
  35. printf "...failed\n"
  36. exit 1
  37. fi
  38. cfgfs=1
  39. rb532=0
  40. while getopts "nr" option
  41. do
  42. case $option in
  43. n)
  44. cfgfs=0
  45. ;;
  46. r)
  47. rb532=1
  48. ;;
  49. *)
  50. printf "Option not recognized\n"
  51. exit 1
  52. ;;
  53. esac
  54. done
  55. shift $(($OPTIND - 1))
  56. if [ -z $1 ];then
  57. printf "Please give your compact flash or USB device as first parameter\n"
  58. exit 1
  59. else
  60. if [ -z $2 ];then
  61. printf "Please give your install tar archive as second parameter\n"
  62. exit 2
  63. fi
  64. if [ -f $2 ];then
  65. printf "Installing $2 on $1\n"
  66. else
  67. printf "$2 is not a file, Exiting\n"
  68. exit 1
  69. fi
  70. if [ $rb532 -eq 1 ];then
  71. if [ -z $3 ];then
  72. printf "Please give the kernel as third parameter\n"
  73. exit 2
  74. fi
  75. if [ -f $3 ];then
  76. printf "Installing $3 on $1\n"
  77. else
  78. printf "$3 is not a file, Exiting\n"
  79. exit 1
  80. fi
  81. fi
  82. if [ -b $1 ];then
  83. printf "Using $1 as CF/USB disk for installation\n"
  84. printf "This will destroy all data on $1, are you sure?\n"
  85. printf "Type "y" to continue\n"
  86. read y
  87. if [ "$y" = "y" ];then
  88. $sfdisk -l $1 2>&1 |grep 'No medium'
  89. if [ $? -eq 0 ];then
  90. exit 1
  91. else
  92. printf "Starting with installation\n"
  93. fi
  94. else
  95. printf "Exiting.\n"
  96. exit 1
  97. fi
  98. else
  99. printf "Sorry $1 is not a block device\n"
  100. exit 1
  101. fi
  102. fi
  103. if [ $(mount | grep $1| wc -l) -ne 0 ];then
  104. printf "Block device $1 is in use, please umount first.\n"
  105. exit 1
  106. fi
  107. if [ $($sfdisk -l $1 2>/dev/null|grep Empty|wc -l) -ne 4 ];then
  108. printf "Partitions already exist, should I wipe them?\n"
  109. printf "Type y to continue\n"
  110. read y
  111. if [ $y = "y" ];then
  112. printf "Wiping existing partitions\n"
  113. dd if=/dev/zero of=$1 bs=512 count=1 >/dev/null 2>&1
  114. else
  115. printf "Exiting.\n"
  116. exit 1
  117. fi
  118. fi
  119. if [ $rb532 -ne 0 ];then
  120. printf "Create partition and filesystem for rb532\n"
  121. rootpart=${1}2
  122. $parted -s $1 mklabel msdos
  123. sleep 2
  124. maxsize=$(env LC_ALL=C $parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
  125. rootsize=$(($maxsize-2))
  126. $parted -s $1 unit cyl mkpart primary ext2 0 1
  127. $parted -s $1 unit cyl mkpartfs primary ext2 1 $rootsize
  128. $parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
  129. $parted -s $1 set 1 boot on
  130. $sfdisk --change-id $1 1 27
  131. $sfdisk --change-id $1 3 88
  132. sleep 2
  133. sync
  134. dd if=$3 of=${1}1 bs=2048
  135. sync
  136. else
  137. rootpart=${1}1
  138. if [ $cfgfs -eq 0 ];then
  139. printf "Create partition and filesystem without cfgfs\n"
  140. $sfdisk $1 << EOF
  141. ,,L
  142. ;
  143. ;
  144. ;
  145. y
  146. EOF
  147. $mke2fs ${rootpart}
  148. else
  149. printf "Create partition and filesystem with cfgfs\n"
  150. $parted -s $1 mklabel msdos
  151. sleep 2
  152. maxsize=$(env LC_ALL=C $parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
  153. rootsize=$(($maxsize-2))
  154. $parted -s $1 unit cyl mkpartfs primary ext2 0 $rootsize
  155. $parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
  156. $parted -s $1 set 1 boot on
  157. $sfdisk --change-id $1 2 88
  158. fi
  159. fi
  160. if [ $? -eq 0 ];then
  161. printf "Successfully created partition ${rootpart}\n"
  162. else
  163. printf "Partition creation failed, Exiting.\n"
  164. exit 1
  165. fi
  166. sleep 2
  167. $tune2fs -c 0 -i 0 -m 1 ${rootpart} >/dev/null
  168. if [ $? -eq 0 ];then
  169. printf "Successfully disabled filesystem checks on ${rootpart}\n"
  170. else
  171. printf "Disabling filesystem checks failed, Exiting.\n"
  172. exit 1
  173. fi
  174. tmp=$(mktemp -d)
  175. mount -t ext2 ${rootpart} $tmp
  176. printf "Extracting install archive\n"
  177. tar -C $tmp -xzpf $2
  178. printf "Fixing permissions\n"
  179. chmod 1777 $tmp/tmp
  180. chmod 4755 $tmp/bin/busybox
  181. if [ $rb532 -eq 0 ];then
  182. printf "Installing GRUB bootloader\n"
  183. mkdir -p $tmp/boot/grub
  184. mount -o bind /dev $tmp/dev
  185. chroot $tmp mount -t proc /proc /proc
  186. chroot $tmp mount -t sysfs /sys /sys
  187. cat << EOF > $tmp/boot/grub/grub.cfg
  188. set default=0
  189. set timeout=1
  190. serial --unit=0 --speed=115200
  191. terminal_output serial
  192. terminal_input serial
  193. menuentry "GNU/Linux (OpenADK)" {
  194. insmod ext2
  195. set root=(hd0,1)
  196. linux /boot/vmlinuz-adk root=/dev/sda1 ro init=/init console=ttyS0,115200 console=tty0 panic=10
  197. }
  198. EOF
  199. chroot $tmp grub-install $1
  200. umount $tmp/proc
  201. umount $tmp/sys
  202. umount $tmp/dev
  203. fi
  204. printf "Creating device nodes\n"
  205. mknod -m 666 $tmp/dev/null c 1 3
  206. mknod -m 622 $tmp/dev/console c 5 1
  207. mknod -m 666 $tmp/dev/tty c 5 0
  208. umount $tmp
  209. printf "Successfully installed.\n"
  210. exit 0