first_boot 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. # This is to avoid expanding '*' in fdisk results
  5. set -f
  6. source /usr/local/lib/utils
  7. SELF=$(basename $0)
  8. # Find out the root partition number from the kernel command line
  9. root_part=$(cat /proc/cmdline | sed -n 's|^.*root=\([^ ]*\).*|\1|p')
  10. root_part_num=${root_part#/dev/mmcblk0p}
  11. if [ "${root_part_num}" -eq 1 ]; then
  12. die_notif 0 "recovery mode"
  13. elif [ "${root_part_num}" = "{$root_part}" ]; then
  14. die_notif 1 "${root_part} is not an SD card partition, aborting"
  15. elif [ "${root_part_num}" -ne 2 ]; then
  16. die_notif 2 "unknown partition layout, aborting"
  17. fi
  18. let swap_part_num=${root_part_num}+1
  19. swap_part=/dev/mmcblk0p${swap_part_num}
  20. let usb_part_num=${swap_part_num}+1
  21. usb_part=/dev/mmcblk0p${usb_part_num}
  22. check_root_id () {
  23. [ $(id -u) -ne 0 ] && die_notif 3 "this script must be run as root, aborting"
  24. return 0
  25. }
  26. resize_rootfs_partition () {
  27. # Check if root partition is already resized
  28. local rootfs_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | grep ${root_part})
  29. set ${rootfs_part_line}
  30. local rootfs_part_size=${6}
  31. if [ "${rootfs_part_size}" = "1G" ]; then
  32. info "root partition is already resized"
  33. return 0
  34. fi
  35. # Check that the last partition is the rootfs partition
  36. local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
  37. set ${last_part_line}
  38. local last_part_num=${1#/dev/mmcblk0p}
  39. local part_start=${3}
  40. if [ "${last_part_num}" != "${root_part_num}" ]; then
  41. die_notif 4 "rootfs is not the last partition. Don't know how to expand, aborting"
  42. fi
  43. # Remove (temporarily) the rootfs partition
  44. # Re-create the rootfs partition with a 1GB size
  45. fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
  46. d
  47. ${root_part_num}
  48. n
  49. p
  50. ${root_part_num}
  51. ${part_start}
  52. +1G
  53. w
  54. EOF
  55. # Mark the rootfs partition as bootable
  56. sfdisk -A /dev/mmcblk0 ${root_part_num} >/dev/null 2>&1 || die_notif 7 "cannot make the rootfs partition bootable, aborting"
  57. # Reload the partition table
  58. partprobe /dev/mmcblk0 >/dev/null 2>&1 || die_notif 8 "cannot reload the partition table, aborting"
  59. return 0
  60. }
  61. resize_rootfs_filesystem () {
  62. local rootfs_line=$(df | grep /dev/root)
  63. set ${rootfs_line}
  64. local rootfs_size=${2}
  65. if [ ${rootfs_size} -gt 1000000 ]; then
  66. info "rootfs already resized"
  67. return 0
  68. fi
  69. rw
  70. resize2fs ${root_part} >/dev/null 2>&1 || die_notif 9 "cannot resize the root filesystem, aborting"
  71. ro
  72. return 0
  73. }
  74. create_swap () {
  75. # Check if swap partition already exists
  76. fdisk -l /dev/mmcblk0 2>/dev/null | grep "Linux swap" >/dev/null 2>&1
  77. if [ $? -eq 0 ]; then
  78. info "swap partition already exists"
  79. else
  80. mount | grep -q ${usb_part}
  81. if [ $? -ne 0 ]; then
  82. # Check that the last partition is the rootfs partition
  83. local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
  84. set ${last_part_line}
  85. local last_part_num=${1#/dev/mmcblk0p}
  86. if [ "$last_part_num" != "$root_part_num" ]; then
  87. die_notif 10 "rootfs is not the last partition. Don't know how to create the backing store partition"
  88. fi
  89. # Create an additional linux swap partition
  90. let swap_part_num=${last_part_num}+1
  91. swap_part=/dev/mmcblk0p${swap_part_num}
  92. fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
  93. n
  94. p
  95. ${swap_part_num}
  96. +128M
  97. t
  98. ${wap_part_num}
  99. 82
  100. w
  101. EOF
  102. fi
  103. fi
  104. # Check if swap is enabled
  105. local swap_line=$(free | grep Swap)
  106. set ${swap_line}
  107. local swap_size=${2}
  108. if [ ${swap_size} -eq 0 ]; then
  109. mkswap ${swap_part} >/dev/null 2>&1
  110. if [ $? -ne 0 ]; then
  111. die_notif 11 "cannot create swap file, aborting"
  112. fi
  113. # Enable swap
  114. swapon -a >/dev/null 2>&1 || die_notif 12 "cannot enable swap file, aborting"
  115. fi
  116. return 0
  117. }
  118. create_usb_partition () {
  119. # Check if the USB partition already exists
  120. fdisk -l /dev/mmcblk0 2>/dev/null | grep "W95 FAT32" >/dev/null 2>&1
  121. if [ $? -eq 0 ]; then
  122. info "USB partition already exists"
  123. return 0
  124. fi
  125. mount | grep -q ${usb_part}
  126. if [ $? -ne 0 ]; then
  127. # Check that the last partition is the swap partition
  128. local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
  129. set ${last_part_line}
  130. local last_part_num=${1#/dev/mmcblk0p}
  131. if [ "${last_part_num}" != "${swap_part_num}" ]; then
  132. die_notif 13 "rootfs is not the last partition. Don't know how to create the backing store partition"
  133. fi
  134. # Create an additional FAT32 USB partition that fills the disk
  135. let usb_part_num=${last_part_num}+1
  136. usb_part=/dev/mmcblk0p${usb_part_num}
  137. fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
  138. n
  139. p
  140. ${usb_part_num}
  141. t
  142. ${usb_part_num}
  143. c
  144. w
  145. EOF
  146. sync
  147. fi
  148. # Reload the partition table
  149. partprobe /dev/mmcblk0 >/dev/null 2>&1 || die_notif 14 "cannot reload the partition table, aborting"
  150. return 0
  151. }
  152. format_usb_partition () {
  153. # Check if the USB partition is already mounted
  154. mount | grep /mnt > /dev/null 2>&1
  155. if [ $? -eq 0 ]; then
  156. info "USB partition already mounted"
  157. return 0
  158. fi
  159. # Format the backing store as FAT32
  160. mkfs.vfat ${usb_part} >/dev/null 2>&1 || die_notif 15 "cannot format the backing store partition"
  161. return 0
  162. }
  163. copy_files_to_usb_partition () {
  164. # Check if the USB partition is already mounted
  165. mount | grep /mnt > /dev/null 2>&1
  166. if [ $? -ne 0 ]; then
  167. mount /mnt/ || die_notif 16 "Cannot mount /mnt"
  168. fi
  169. # Copy freware games and other necessary mnt files
  170. unzip -q -o /usr/local/share/mnt_files.zip -d /mnt/
  171. # Copy OPKs
  172. set +f
  173. cp -r /usr/local/share/OPKs/* /mnt
  174. set -f
  175. # Unmount USB partition
  176. umount /mnt/ || die_notif 17 "Cannot unmount /mnt"
  177. return 0
  178. }
  179. check_root_id
  180. notif set 0 " FIRST BOOT DETECTED"
  181. notif set 0 " 1/6 RESIZE ROOT PARTITION"
  182. resize_rootfs_partition
  183. notif set 0 " 2/6 RESIZE ROOT FILESYSTEM"
  184. resize_rootfs_filesystem
  185. notif set 0 " 3/6 CREATE SWAP"
  186. create_swap
  187. notif set 0 " 4/6 CREATE USB PARTITION"
  188. create_usb_partition
  189. notif set 0 " 5/6 FORMAT USB PARTITION"
  190. format_usb_partition
  191. notif set 0 " 6/6 COPY FILES TO ^ USB PARTITION"
  192. copy_files_to_usb_partition
  193. notif set 0 " FIRST BOOT SETUP FINISHED!"
  194. fw_setenv first_boot_ok 1
  195. sleep 1
  196. notif clear