From 8bbe18b379359e8b4b1aaf1e7c3ba7e3fad20192 Mon Sep 17 00:00:00 2001 From: ThomasKaiser Date: Sat, 26 Mar 2016 18:27:37 +0100 Subject: [PATCH 1/2] Provide custom partition resizing in firstrun --- scripts/firstrun | 110 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 28 deletions(-) diff --git a/scripts/firstrun b/scripts/firstrun index 5ce005bc97..3b4164d300 100644 --- a/scripts/firstrun +++ b/scripts/firstrun @@ -44,10 +44,6 @@ swapon /var/swap >/dev/null 2>&1 if ! grep -q swap /etc/fstab; then echo "/var/swap none swap sw 0 0" >> /etc/fstab; fi if ! grep -q swap /etc/sysctl.conf; then echo "vm.swappiness=0" >> /etc/sysctl.conf; fi fi -# Package updating -if [ "$(fping 8.8.8.8 | grep alive)" != "" ]; then - apt-get update >/dev/null -fi # RAMLOG if [[ "$(apt-cache policy ramlog | grep Installed)" != "" ]]; then service ramlog enable @@ -75,24 +71,33 @@ collect_informations() { HARDWARE=$(awk '/Hardware/ {print $3}' /dev/tty1 + if [ "${DISTRIBUTION}" == "wheezy" ]; then + echo -e "[\e[0;32m ok \x1B[0m] ${1}" > /dev/tty1 else - echo -e " * $1" > /dev/tty1 + echo -e " * ${1}" > /dev/tty1 fi } @@ -113,9 +118,15 @@ autodetect_sunxi() { echo heartbeat >/sys/class/leds/blue_led/trigger fi - # wait for armhwinfo - sleep 3 - MACHINE="$(tail -n1 /run/machine.id)" + # check whether the auto detection override exists and if true use this as $MACHINE + if [ -f /root/.machine.id ]; then + read MACHINE /dev/null || true + # get device node for boot media + DEVICE="/dev/"$(lsblk -idn -o NAME | grep mmcblk0) + if [ "${DEVICE}" = "/dev/" ]; then return ; fi + QUOTED_DEVICE=$(echo "${DEVICE}" | sed 's:/:\\\/:g') + + # get count of partitions and their boundaries + PARTITIONS=$(( $(grep -c ${DEVICE##*/} /proc/partitions) - 1 )) + PARTSTART=$(parted ${DEVICE} unit s print -sm | tail -1 | cut -d: -f2 | sed 's/s//') # start of first partition + PARTEND=$(parted ${DEVICE} unit s print -sm | head -3 | tail -1 | cut -d: -f3 | sed 's/s//') # end of first partition + STARTFROM=$(( ${PARTEND} + 1 )) + [[ ${PARTITIONS} == 1 ]] && STARTFROM=${PARTSTART} + + # check whether a resizing rule is defined. We will take this value if it's not too low. In + # this case the value will be ignored and resizing to the whole card size happens. + if [ -f "/root/.rootfs_resize" ]; then + read RESIZE_VALUE <"/root/.rootfs_resize" + case ${RESIZE_VALUE} in + *%) + # percentage value, we try to use 16MiB to align partitions since this is + # the erase block size of more recent SD cards (512 byte sectors, so we use 32 + # as divider and substract 1) + PERCENTAGE=$(echo ${RESIZE_VALUE} | tr -c -d '[:digit:]') + LASTSECTOR=$(( 32 * $(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", ( \$2 * ${PERCENTAGE} / 3200))}") -1 )) + if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi + ;; + *s) + # sector value, we use it directly + LASTSECTOR=$(echo ${RESIZE_VALUE} | tr -c -d '[:digit:]') + if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi + ;; + esac + else + # check device capacity. If 4GB or below do not use whole card but leave a 5% spare area + # to help older cards with wear leveling and garbage collection. In case this reduced card + # capacity is less than the actual image capacity this is a clear sign that someone wants + # to use Armbian on a card of inappropriate size so he gets what he deserves (at least he + # should know what he's doing) + CAPACITY=$(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", \$2 / ( 1024 / \$4 ))}") + if [ ${CAPACITY} -lt 4000000 ]; then + SPAREAREA=$(( ${CAPACITY} / 20000 )) + LASTSECTOR=$(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {print \$2 - (${SPAREAREA} * 1024 * ( 1024 / \$4 ))}") + if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi + fi + fi + + # Start resizing + ((echo d; echo $PARTITIONS; echo n; echo p; echo ; echo $STARTFROM; echo ${LASTSECTOR} ; echo w;) | fdisk ${DEVICE}) >/dev/null || true s=0 fsck -f $root_partition >/dev/null 2>&1 || true - partprobe $device >/dev/null 2>&1 || s=$? + partprobe ${DEVICE} >/dev/null 2>&1 || s=$? resize2fs $root_partition >/dev/null 2>&1 || true + + # check whether reboot is necessary for resize2fs to take effect FREESIZE=$(df -hm / | awk '/\// {print $(NF-2)}') if [[ "$DISTRIBUTION" == "wheezy" || "$s" != "0" || "$FREESIZE" -lt "152" ]]; then touch /var/run/reboot @@ -225,3 +278,4 @@ main() { main exit 0 + From 278e48b2d83723e99a53fc7c998a574c9ac34be9 Mon Sep 17 00:00:00 2001 From: ThomasKaiser Date: Sun, 27 Mar 2016 16:05:39 +0000 Subject: [PATCH 2/2] Documentation fix for partition resizing features modified: documentation/geek-faq.md modified: scripts/firstrun --- documentation/geek-faq.md | 10 ++++++++-- scripts/firstrun | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/documentation/geek-faq.md b/documentation/geek-faq.md index 16c7f03e49..66fa9a8b86 100644 --- a/documentation/geek-faq.md +++ b/documentation/geek-faq.md @@ -57,7 +57,7 @@ Run the script ### Hidden options to minimize user input for build automation: - **BOARD** (string): you can set name of board manually to skip dialog prompt - **BRANCH** (default|next|dev): you can set kernel and u-boot branch manually to skip dialog prompt; some options may not be available for all devices -- **RELEASE** (wheezy|jessie|trusty): you can set OS release manually to skip dialog prompt; use this option with `KERNEL_ONLY=yes` to create board support package +- **RELEASE** (wheezy|jessie|trusty|xenial): you can set OS release manually to skip dialog prompt; use this option with `KERNEL_ONLY=yes` to create board support package ### Hidden options for advanced users (default values are marked **bold**): - **USE_CCACHE** (**yes**|no): use a C compiler cache to speed up the build process @@ -102,6 +102,12 @@ and place your code here. You may test values of variables noted in the file to To add files to image easily, put them in `userpatches/overlay` and access them in `/tmp/overlay` from `customize-image.sh` +## Partitioning of the SD card + +In case you define `$FIXED_IMAGE_SIZE` at build time the partition containing the rootfs will be made of this size. Default behaviour when this is not defined and `$ROOTFS_TYPE` is set to _ext4_ is to shrink the partition to minimum size at build time and expand it to the card's maximum capacity at boot time (leaving an unpartitioned spare area of ~5% when the size is 4GB or less to help the SD card's controller with wear leveling and garbage collection on old/slow cards). + +You can prevent the partition expansion from within `customize-image.sh` by a `touch /root/.no_rootfs_resize` or configure the resize operation by either a percentage or a sector count using `/root/.rootfs_resize` (`50%` will use only half of the card's size if the image size doesn't exceed this or `3887103s` for example will use sector 3887103 as partition end. Values without either `%` or `s` will be ignored) + # What is behind the build process? Build process summary: @@ -222,4 +228,4 @@ It will be something like this: ## Additional info ## -- [Allwinner SBC community](https://linux-sunxi.org/) \ No newline at end of file +- [Allwinner SBC community](https://linux-sunxi.org/) diff --git a/scripts/firstrun b/scripts/firstrun index 3b4164d300..b323a22565 100644 --- a/scripts/firstrun +++ b/scripts/firstrun @@ -223,6 +223,9 @@ do_expand_rootfs() { # should know what he's doing) CAPACITY=$(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {printf (\"%0d\", \$2 / ( 1024 / \$4 ))}") if [ ${CAPACITY} -lt 4000000 ]; then + cd /root + echo -e "\n### quick iozone test:\c" >>/var/log/armhwinfo.log + iozone -e -I -a -s 1M -r 4k -i 0 -i 1 -i 2 | grep '^ 1024' | sed 's/ 1024 //' >>/var/log/armhwinfo.log SPAREAREA=$(( ${CAPACITY} / 20000 )) LASTSECTOR=$(parted ${DEVICE} unit s print -sm | awk -F":" "/^${QUOTED_DEVICE}/ {print \$2 - (${SPAREAREA} * 1024 * ( 1024 / \$4 ))}") if [ ${LASTSECTOR} -lt ${PARTEND} ]; then unset LASTSECTOR ; fi