install-chroot.sh 4.7 KB

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