first_boot 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 0 "recovery mode"
  13. elif [ "${root_part_num}" = "{$root_part}" ]; then
  14. die 1 "${root_part} is not an SD card partition, aborting"
  15. elif [ "${root_part_num}" -ne 2 ]; then
  16. die 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 share_part_num=${swap_part_num}+1
  21. share_part=/dev/mmcblk0p${share_part_num}
  22. check_root_id () {
  23. [ $(id -u) -ne 0 ] && die 3 "this script must be run as root, aborting"
  24. return 0
  25. }
  26. resize_rootfs_partition () {
  27. # Check that the last partition is the rootfs partition
  28. local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
  29. set ${last_part_line}
  30. local last_part_num=${1#/dev/mmcblk0p}
  31. local part_start=${3}
  32. if [ "${last_part_num}" != "${root_part_num}" ]; then
  33. die 4 "rootfs is not the last partition. Don't know how to expand, aborting"
  34. fi
  35. # Remove (temporarily) the rootfs partition
  36. # Re-create the rootfs partition with a 1GB size
  37. fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
  38. d
  39. ${root_part_num}
  40. n
  41. p
  42. ${root_part_num}
  43. ${part_start}
  44. +1G
  45. w
  46. EOF
  47. # Mark the rootfs partition as bootable
  48. sfdisk -A /dev/mmcblk0 ${root_part_num} >/dev/null 2>&1 || die 7 "cannot make the rootfs partition bootable, aborting"
  49. return 0
  50. }
  51. reload_partition_table () {
  52. partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 9 "cannot reload the partition table, aborting"
  53. return 0
  54. }
  55. resize_rootfs_filesystem () {
  56. rw
  57. resize2fs ${root_part} >/dev/null 2>&1 || die 10 "cannot resize the root filesystem, aborting"
  58. ro
  59. return 0
  60. }
  61. create_swap () {
  62. mount | grep -q ${share_part}
  63. if [ $? -ne 0 ]; then
  64. # Check that the last partition is the rootfs partition
  65. local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
  66. set ${last_part_line}
  67. local last_part_num=${1#/dev/mmcblk0p}
  68. if [ "$last_part_num" != "$root_part_num" ]; then
  69. die 11 "rootfs is not the last partition. Don't know how to create the backing store partition"
  70. fi
  71. # Create an additional linux swap partition
  72. let swap_part_num=${last_part_num}+1
  73. swap_part=/dev/mmcblk0p${swap_part_num}
  74. fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
  75. n
  76. p
  77. ${swap_part_num}
  78. +128M
  79. t
  80. ${wap_part_num}
  81. 82
  82. w
  83. EOF
  84. mkswap ${swap_part} >/dev/null 2>&1
  85. if [ $? -ne 0 ]; then
  86. die 14 "cannot create swap file, aborting"
  87. fi
  88. fi
  89. return 0
  90. }
  91. enable_swap () {
  92. swapon -a >/dev/null 2>&1 || die 15 "cannot enable swap file, aborting"
  93. return 0
  94. }
  95. create_backing_store_partition () {
  96. mount | grep -q ${share_part}
  97. if [ $? -ne 0 ]; then
  98. # Check that the last partition is the swap partition
  99. local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
  100. set ${last_part_line}
  101. local last_part_num=${1#/dev/mmcblk0p}
  102. if [ "${last_part_num}" != "${swap_part_num}" ]; then
  103. die 15 "rootfs is not the last partition. Don't know how to create the backing store partition"
  104. fi
  105. # Create an additional FAT32 share partition that fills the disk
  106. let share_part_num=${last_part_num}+1
  107. share_part=/dev/mmcblk0p${share_part_num}
  108. fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
  109. n
  110. p
  111. ${share_part_num}
  112. t
  113. ${share_part_num}
  114. c
  115. w
  116. EOF
  117. sync
  118. fi
  119. return 0
  120. }
  121. format_backing_store_partition () {
  122. # Format the backing store as FAT32
  123. mkfs.vfat ${share_part} >/dev/null 2>&1 || die 17 "cannot format the backing store partition"
  124. return 0
  125. }
  126. copy_files_to_store_partition () {
  127. # Add file to force assembly tests
  128. mount /mnt/ || die 18 "Cannot mount /mnt"
  129. unzip -q -o /usr/local/share/mnt_freware_games.zip -d /mnt/
  130. umount /mnt/ || die 20 "Cannot unmount /mnt"
  131. return 0
  132. }
  133. check_root_id
  134. notif " FIRST BOOT DETECTED"
  135. notif " 1/9 RESIZE ROOT PARTITION"
  136. resize_rootfs_partition
  137. notif " 2/9 RELOAD ROOT PARTITION"
  138. reload_partition_table
  139. notif " 3/9 RESIZE ROOT FILESYSTEM"
  140. resize_rootfs_filesystem
  141. notif " 4/9 CREATE SWAP"
  142. create_swap
  143. notif " 5/9 ENABLE SWAP"
  144. enable_swap
  145. notif " 6/9 CREATE USB PARTITION"
  146. create_backing_store_partition
  147. notif " 7/9 RELOAD PARTITION TABLE"
  148. reload_partition_table
  149. notif " 8/9 FORMAT USB PARTITION"
  150. format_backing_store_partition
  151. notif " 9/9 COPY FILES TO ^ USB PARTITION"
  152. copy_files_to_store_partition
  153. notif " FIRST BOOT SETUP FINISHED!"
  154. sleep 1
  155. clear_notif