install-chroot.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. console=0
  40. serial=0
  41. while getopts "nrcs" option
  42. do
  43. case $option in
  44. n)
  45. cfgfs=0
  46. ;;
  47. s)
  48. serial=1
  49. ;;
  50. c)
  51. console=1
  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 archive 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 >/dev/null 2>&1
  106. else
  107. printf "Exiting.\n"
  108. exit 1
  109. fi
  110. fi
  111. case $2 in
  112. wrap*)
  113. speed=38400
  114. ;;
  115. *)
  116. speed=115200
  117. ;;
  118. esac
  119. rootpart=${1}1
  120. if [ $cfgfs -eq 0 ];then
  121. printf "Create partition and filesystem without cfgfs\n"
  122. $sfdisk $1 << EOF
  123. ,,L
  124. ;
  125. ;
  126. ;
  127. y
  128. EOF
  129. $mke2fs ${rootpart}
  130. else
  131. printf "Create partition and filesystem with cfgfs\n"
  132. $parted -s $1 mklabel msdos
  133. sleep 2
  134. maxsize=$(env LC_ALL=C $parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
  135. rootsize=$(($maxsize-2))
  136. $parted -s $1 unit cyl mkpart primary ext2 0 $rootsize
  137. $parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
  138. $parted -s $1 set 1 boot on
  139. $sfdisk --change-id $1 2 88
  140. $mke2fs ${1}1
  141. fi
  142. if [ $? -eq 0 ];then
  143. printf "Successfully created partition ${rootpart}\n"
  144. else
  145. printf "Partition creation failed, Exiting.\n"
  146. exit 1
  147. fi
  148. sleep 2
  149. $tune2fs -c 0 -i 0 -m 1 ${rootpart} >/dev/null
  150. if [ $? -eq 0 ];then
  151. printf "Successfully disabled filesystem checks on ${rootpart}\n"
  152. else
  153. printf "Disabling filesystem checks failed, Exiting.\n"
  154. exit 1
  155. fi
  156. tmp=$(mktemp -d)
  157. mount -t ext2 ${rootpart} $tmp
  158. printf "Extracting install archive\n"
  159. tar -C $tmp -xzpf $2
  160. printf "Fixing permissions\n"
  161. chmod 1777 $tmp/tmp
  162. chmod 4755 $tmp/bin/busybox
  163. printf "Installing GRUB bootloader\n"
  164. mkdir -p $tmp/boot/grub
  165. mount -o bind /dev $tmp/dev
  166. chroot $tmp mount -t proc /proc /proc
  167. chroot $tmp mount -t sysfs /sys /sys
  168. if [ $serial -eq 1 ];then
  169. cat << EOF > $tmp/boot/grub/grub.cfg
  170. set default=0
  171. set timeout=1
  172. serial --unit=0 --speed=$speed
  173. terminal_output serial
  174. terminal_input serial
  175. menuentry "GNU/Linux (OpenADK)" {
  176. insmod ext2
  177. set root=(hd0,1)
  178. linux /boot/vmlinuz-adk ro init=/init console=ttyS0,$speed console=tty0 panic=10
  179. }
  180. EOF
  181. fi
  182. if [ $console -eq 1 ];then
  183. cat << EOF > $tmp/boot/grub/grub.cfg
  184. set default=0
  185. set timeout=1
  186. terminal_output console
  187. terminal_input console
  188. menuentry "GNU/Linux (OpenADK)" {
  189. insmod ext2
  190. set root=(hd0,1)
  191. linux /boot/vmlinuz-adk ro init=/init console=tty0 panic=10
  192. }
  193. EOF
  194. fi
  195. chroot $tmp grub-install $1
  196. umount $tmp/proc
  197. umount $tmp/sys
  198. umount $tmp/dev
  199. umount $tmp
  200. printf "Successfully installed.\n"
  201. exit 0