123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #!/bin/sh
- # Uncomment the following line to get debug info
- #set -x
- # This is to avoid expanding '*' in fdisk results
- set -f
- source /usr/local/lib/utils
- SELF=$(basename $0)
- # Find out the root partition number from the kernel command line
- root_part=$(cat /proc/cmdline | sed -n 's|^.*root=\([^ ]*\).*|\1|p')
- root_part_num=${root_part#/dev/mmcblk0p}
- if [ "${root_part_num}" -eq 1 ]; then
- die 0 "recovery mode"
- elif [ "${root_part_num}" = "{$root_part}" ]; then
- die 1 "${root_part} is not an SD card partition, aborting"
- elif [ "${root_part_num}" -ne 2 ]; then
- die 2 "unknown partition layout, aborting"
- fi
- let swap_part_num=${root_part_num}+1
- swap_part=/dev/mmcblk0p${swap_part_num}
- let share_part_num=${swap_part_num}+1
- share_part=/dev/mmcblk0p${share_part_num}
- check_root_id () {
- [ $(id -u) -ne 0 ] && die 3 "this script must be run as root, aborting"
- return 0
- }
- resize_rootfs_partition () {
- # Check that the last partition is the rootfs partition
- local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
- set ${last_part_line}
- local last_part_num=${1#/dev/mmcblk0p}
- local part_start=${3}
- if [ "${last_part_num}" != "${root_part_num}" ]; then
- die 4 "rootfs is not the last partition. Don't know how to expand, aborting"
- fi
- # Remove (temporarily) the rootfs partition
- # Re-create the rootfs partition with a 1GB size
- fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
- d
- ${root_part_num}
- n
- p
- ${root_part_num}
- ${part_start}
- +1G
- w
- EOF
- # Mark the rootfs partition as bootable
- sfdisk -A /dev/mmcblk0 ${root_part_num} >/dev/null 2>&1 || die 7 "cannot make the rootfs partition bootable, aborting"
- return 0
- }
- reload_partition_table () {
- partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 9 "cannot reload the partition table, aborting"
- return 0
- }
- resize_rootfs_filesystem () {
- rw
- resize2fs ${root_part} >/dev/null 2>&1 || die 10 "cannot resize the root filesystem, aborting"
- ro
- return 0
- }
- create_swap () {
- mount | grep -q ${share_part}
- if [ $? -ne 0 ]; then
- # Check that the last partition is the rootfs partition
- local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
- set ${last_part_line}
- local last_part_num=${1#/dev/mmcblk0p}
- if [ "$last_part_num" != "$root_part_num" ]; then
- die 11 "rootfs is not the last partition. Don't know how to create the backing store partition"
- fi
- # Create an additional linux swap partition
- let swap_part_num=${last_part_num}+1
- swap_part=/dev/mmcblk0p${swap_part_num}
- fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
- n
- p
- ${swap_part_num}
- +128M
- t
- ${wap_part_num}
- 82
- w
- EOF
- mkswap ${swap_part} >/dev/null 2>&1
- if [ $? -ne 0 ]; then
- die 14 "cannot create swap file, aborting"
- fi
- fi
- return 0
- }
- enable_swap () {
- swapon -a >/dev/null 2>&1 || die 15 "cannot enable swap file, aborting"
- return 0
- }
- create_backing_store_partition () {
- mount | grep -q ${share_part}
- if [ $? -ne 0 ]; then
- # Check that the last partition is the swap partition
- local last_part_line=$(fdisk -l /dev/mmcblk0 2>/dev/null | tail -n 1)
- set ${last_part_line}
- local last_part_num=${1#/dev/mmcblk0p}
- if [ "${last_part_num}" != "${swap_part_num}" ]; then
- die 15 "rootfs is not the last partition. Don't know how to create the backing store partition"
- fi
- # Create an additional FAT32 share partition that fills the disk
- let share_part_num=${last_part_num}+1
- share_part=/dev/mmcblk0p${share_part_num}
- fdisk /dev/mmcblk0 >/dev/null 2>&1 <<EOF
- n
- p
- ${share_part_num}
- t
- ${share_part_num}
- c
- w
- EOF
- sync
- fi
- return 0
- }
- format_backing_store_partition () {
- # Format the backing store as FAT32
- mkfs.vfat ${share_part} >/dev/null 2>&1 || die 17 "cannot format the backing store partition"
- return 0
- }
- copy_files_to_store_partition () {
- # Add file to force assembly tests
-
-
- mount /mnt/ || die 18 "Cannot mount /mnt"
- unzip -q -o /usr/local/share/mnt_freware_games.zip -d /mnt/
- umount /mnt/ || die 20 "Cannot unmount /mnt"
- return 0
- }
- check_root_id
- notif " FIRST BOOT DETECTED"
- notif " 1/9 RESIZE ROOT PARTITION"
- resize_rootfs_partition
- notif " 2/9 RELOAD ROOT PARTITION"
- reload_partition_table
- notif " 3/9 RESIZE ROOT FILESYSTEM"
- resize_rootfs_filesystem
- notif " 4/9 CREATE SWAP"
- create_swap
- notif " 5/9 ENABLE SWAP"
- enable_swap
- notif " 6/9 CREATE USB PARTITION"
- create_backing_store_partition
- notif " 7/9 RELOAD PARTITION TABLE"
- reload_partition_table
- notif " 8/9 FORMAT USB PARTITION"
- format_backing_store_partition
- notif " 9/9 COPY FILES TO ^ USB PARTITION"
- copy_files_to_store_partition
- notif " FIRST BOOT SETUP FINISHED!"
- sleep 1
- clear_notif
|