install.sh 4.8 KB

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