install.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. printf "Create partition and filesystem\n"
  120. if [ $rb532 -ne 0 ];then
  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. $sfdisk $1 << EOF
  140. ,,L
  141. ;
  142. ;
  143. ;
  144. y
  145. EOF
  146. $mke2fs ${rootpart}
  147. else
  148. $parted -s $1 mklabel msdos
  149. sleep 2
  150. maxsize=$(env LC_ALL=C $parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
  151. rootsize=$(($maxsize-2))
  152. $parted -s $1 unit cyl mkpartfs primary ext2 0 $rootsize
  153. $parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
  154. $parted -s $1 set 1 boot on
  155. $sfdisk --change-id $1 2 88
  156. fi
  157. fi
  158. if [ $? -eq 0 ];then
  159. printf "Successfully created partition ${rootpart}\n"
  160. else
  161. printf "Partition creation failed, Exiting.\n"
  162. exit 1
  163. fi
  164. sleep 2
  165. $tune2fs -c 0 -i 0 ${rootpart} >/dev/null
  166. if [ $? -eq 0 ];then
  167. printf "Successfully disabled filesystem checks on ${rootpart}\n"
  168. else
  169. printf "Disabling filesystem checks failed, Exiting.\n"
  170. exit 1
  171. fi
  172. tmp=$(mktemp -d)
  173. mount -t ext2 ${rootpart} $tmp
  174. printf "Extracting install archive\n"
  175. tar -C $tmp -xzpf $2
  176. printf "Fixing permissions\n"
  177. chmod 1777 $tmp/tmp
  178. chmod 4755 $tmp/bin/busybox
  179. if [ $rb532 -eq 0 ];then
  180. mkdir -p $tmp/boot/grub
  181. mount -o bind /dev $tmp/dev
  182. chroot $tmp mount -t proc /proc /proc
  183. chroot $tmp mount -t sysfs /sys /sys
  184. cat << EOF > $tmp/boot/grub/grub.cfg
  185. set default=0
  186. set timeout=5
  187. serial --unit=0 --speed=115200
  188. terminal_output serial
  189. terminal_input serial
  190. menuentry "GNU/Linux (OpenADK)" {
  191. insmod ext2
  192. set root=(hd0,1)
  193. linux /boot/vmlinuz-adk root=/dev/sda1 ro init=/init console=ttyS0,115200 console=tty0 panic=10
  194. }
  195. EOF
  196. chroot $tmp grub-install $1
  197. umount $tmp/proc
  198. umount $tmp/sys
  199. umount $tmp/dev
  200. fi
  201. printf "Creating device nodes\n"
  202. mknod -m 666 $tmp/dev/null c 1 3
  203. mknod -m 622 $tmp/dev/console c 5 1
  204. mknod -m 666 $tmp/dev/tty c 5 0
  205. umount $tmp
  206. printf "Successfully installed.\n"
  207. exit 0