adkinstall 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/mksh
  2. # This file is part of the OpenADK project.
  3. # install to ADK to a block/flash device
  4. if [ $(id -u) -ne 0 ];then
  5. print Installation is only possible as root
  6. exit 1
  7. fi
  8. # get adk target system
  9. target=$(cat /etc/.adktarget)
  10. if [ -z $target ];then
  11. print autodetection of target failed
  12. exit 1
  13. fi
  14. function mikrotik-rb532-help {
  15. cat >&2 <<EOF
  16. Syntax: adkinstall [-c|-n] -a <archive>
  17. -a: archive
  18. -c: compact flash install
  19. -n: nand install
  20. -f: filesystem for compact flash
  21. -h: help text
  22. EOF
  23. exit 1
  24. }
  25. case $target {
  26. (mikrotik-rb532)
  27. cfgfssize=32768
  28. nand=0
  29. cf=0
  30. fs=ext4
  31. while getopts "a:cnhf:" ch; do
  32. case $ch in
  33. a)
  34. archive=$OPTARG
  35. ;;
  36. c)
  37. cf=1
  38. fs=ext4
  39. ;;
  40. n)
  41. nand=1
  42. fs=yaffs2
  43. ;;
  44. f)
  45. fs=$OPTARG
  46. ;;
  47. h)
  48. mikrotik-rb532-help
  49. exit 1
  50. ;;
  51. *)
  52. mikrotik-rb532-help
  53. exit 1
  54. ;;
  55. esac
  56. done
  57. shift $((OPTIND - 1))
  58. ;;
  59. (*)
  60. print target $target not supported
  61. exit 1
  62. ;;
  63. }
  64. if [ $cf -eq 0 -a $nand -eq 0 ];then
  65. print "You either install on cf (-c) or nand (-n)"
  66. mikrotik-rb532-help
  67. exit 1
  68. fi
  69. tools="parted sfdisk"
  70. if [ $cf -eq 1 ];then
  71. tools="$tools mkfs.$fs"
  72. fi
  73. f=0
  74. for tool in $tools;do
  75. if ! which $tool >/dev/null; then
  76. echo "Checking if $tool is installed... failed"
  77. f=1
  78. fi
  79. done
  80. if [ $f -eq 1 ];then exit 1;fi
  81. # create empty partition table
  82. function create_label {
  83. print "Creating empty partition table"
  84. parted -s $1 mklabel msdos
  85. }
  86. # get max size of disk in sectors
  87. function get_max_size {
  88. maxsize=$(env LC_ALL=C parted $1 -s unit s print |awk '/^Disk/ { print $3 }'|sed -e 's/s//')
  89. rootsize=$(($maxsize-$cfgfssize))
  90. print device has $maxsize sectors. Using $rootsize for root.
  91. }
  92. # create partition, with fstype start and end in sectors
  93. function create_partition {
  94. print creating partition on $1
  95. parted -s $1 unit s mkpart primary $2 $3 $4 > /dev/null 2>&1
  96. }
  97. function set_boot_flag {
  98. print setting bootflag on $1 partition $2 > /dev/null 2>&1
  99. parted -s $1 set $2 boot on
  100. }
  101. function change_part_type {
  102. print setting partition type on $1 partition $2 to $3
  103. sfdisk --change-id $1 $2 $3 >/dev/null 2>&1
  104. }
  105. function create_filesystem {
  106. print creating filesystem $2 on $1 partition $3
  107. mkfs.$2 -q ${1}${3}
  108. }
  109. function mount_fs {
  110. print mounting ${1}${2} to $4 with filesystem $3
  111. mount -t $3 ${1}${2} $4
  112. }
  113. function extract_archive {
  114. print extracting archive $1 onto $2
  115. tar -C $2 -xpf $1
  116. }
  117. function fix_perm {
  118. print fixing permissions
  119. chmod 1777 ${1}/tmp
  120. chmod 4755 ${1}/bin/busybox
  121. }
  122. case $target {
  123. (mikrotik-rb532)
  124. if (( cf )); then
  125. get_max_size /dev/sda
  126. create_label /dev/sda
  127. create_partition /dev/sda ext2 1 8192
  128. create_partition /dev/sda ext2 8193 $rootsize
  129. create_partition /dev/sda ext2 $(($rootsize+1)) $(($maxsize-1))
  130. set_boot_flag /dev/sda 1
  131. change_part_type /dev/sda 1 27
  132. change_part_type /dev/sda 3 88
  133. create_filesystem /dev/sda $fs 2
  134. mdev -s
  135. mount_fs /dev/sda 2 $fs /mnt
  136. extract_archive $archive /mnt
  137. print installing kernel to cf disk /dev/sda1
  138. dd if=/mnt/boot/kernel of=/dev/sda1 bs=2048 >/dev/null 2>&1
  139. fix_perm /mnt
  140. umount /mnt
  141. fi
  142. if (( nand )); then
  143. mount_fs /dev/mtdblock 1 $fs /mnt
  144. rm -rf /mnt/* >/dev/null 2>&1
  145. mkdir /mnt/boot
  146. mount_fs /dev/mtdblock 0 $fs /mnt/boot
  147. extract_archive $archive /mnt
  148. fix_perm /mnt
  149. umount /mnt/boot
  150. umount /mnt
  151. fi
  152. ;;
  153. }
  154. echo "Successfully installed OpenADK on $target."
  155. exit 0