install.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/bin/sh
  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. while getopts "n" option
  48. do
  49. case $option in
  50. n)
  51. cfgfs=0
  52. ;;
  53. *)
  54. printf "Option not recognized\n"
  55. exit 1
  56. ;;
  57. esac
  58. done
  59. shift $(($OPTIND - 1))
  60. if [ -z $1 ];then
  61. printf "Please give your compact flash or USB device as first parameter\n"
  62. exit 1
  63. else
  64. if [ -z $2 ];then
  65. printf "Please give your install tar as second parameter\n"
  66. exit 2
  67. fi
  68. if [ -f $2 ];then
  69. printf "Installing $2 on $1\n"
  70. else
  71. printf "$2 is not a file, Exiting\n"
  72. exit 1
  73. fi
  74. if [ -b $1 ];then
  75. printf "Using $1 as CF/USB disk for installation\n"
  76. printf "This will destroy all data on $1, are you sure?\n"
  77. printf "Type "y" to continue\n"
  78. read y
  79. if [ $y = "y" ];then
  80. $sfdisk -l $1 2>&1 |grep 'No medium'
  81. if [ $? -eq 0 ];then
  82. exit 1
  83. else
  84. printf "Starting with installation\n"
  85. fi
  86. else
  87. printf "Exiting.\n"
  88. exit 1
  89. fi
  90. else
  91. printf "Sorry $1 is not a block device\n"
  92. exit 1
  93. fi
  94. fi
  95. if [ $(mount | grep $1| wc -l) -ne 0 ];then
  96. printf "Block device $1 is in use, please umount first.\n"
  97. exit 1
  98. fi
  99. if [ $($sfdisk -l $1 2>/dev/null|grep Empty|wc -l) -ne 4 ];then
  100. printf "Partitions already exist, should I wipe them?\n"
  101. printf "Type y to continue\n"
  102. read y
  103. if [ $y = "y" ];then
  104. printf "Wiping existing partitions\n"
  105. dd if=/dev/zero of=$1 bs=512 count=1
  106. else
  107. printf "Exiting.\n"
  108. exit 1
  109. fi
  110. fi
  111. printf "Create partition and filesystem\n"
  112. if [ $cfgfs -eq 0 ];then
  113. $sfdisk $1 << EOF
  114. ,,L
  115. ;
  116. ;
  117. ;
  118. y
  119. EOF
  120. $mke2fs ${1}1
  121. else
  122. $parted -s $1 mklabel msdos
  123. sleep 2
  124. declare -i maxsize
  125. maxsize=$(parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
  126. let rootsize=$maxsize-1
  127. $parted -s $1 unit cyl mkpartfs primary ext2 0 $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 2 88
  131. fi
  132. if [ $? -eq 0 ];then
  133. printf "Successfully created partition ${1}1\n"
  134. else
  135. printf "Partition creation failed, Exiting.\n"
  136. exit 1
  137. fi
  138. sleep 4
  139. $tune2fs -c 0 -i 0 ${1}1 >/dev/null
  140. if [ $? -eq 0 ];then
  141. printf "Successfully disabled filesystem checks on ${1}1\n"
  142. else
  143. printf "Disabling filesystem checks failed, Exiting.\n"
  144. fi
  145. tmp=$(mktemp -d)
  146. mount -t ext2 ${1}1 $tmp
  147. printf "Extracting install archive\n"
  148. tar -C $tmp -xzpf $2
  149. printf "Fixing permissions\n"
  150. chmod 1777 $tmp/tmp
  151. chmod 4755 $tmp/bin/busybox
  152. printf "Copying grub files\n"
  153. mkdir $tmp/boot/grub
  154. cp /boot/grub/stage1 $tmp/boot/grub
  155. cp /boot/grub/stage2 $tmp/boot/grub
  156. cp /boot/grub/e2fs_stage1_5 $tmp/boot/grub
  157. cat << EOF > $tmp/boot/grub/menu.lst
  158. serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
  159. terminal --timeout=2 serial console
  160. timeout 2
  161. default 0
  162. hiddenmenu
  163. title linux
  164. root (hd0,0)
  165. kernel /boot/kernel root=/dev/sda1 init=/init console=ttyS0,115200 console=tty0 panic=10 rw
  166. EOF
  167. printf "Installing Grub bootloader\n"
  168. $grub --batch --no-curses --no-floppy --device-map=/dev/null >/dev/null << EOF
  169. device (hd0) $1
  170. root (hd0,0)
  171. setup (hd0)
  172. quit
  173. EOF
  174. printf "Creating device nodes\n"
  175. mknod -m 666 $tmp/dev/null c 1 3
  176. mknod -m 622 $tmp/dev/console c 5 1
  177. mknod -m 666 $tmp/dev/tty c 5 0
  178. umount $tmp
  179. printf "Successfully installed.\n"
  180. exit 0