Merge pull request #245 from ThomasKaiser/master

Improve/document rootfs resizing in firstrun
This commit is contained in:
Igor Pečovnik 2016-03-27 18:44:38 +02:00
commit cea4cc6c78
2 changed files with 93 additions and 30 deletions

View File

@ -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/)
- [Allwinner SBC community](https://linux-sunxi.org/)

View File

@ -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}' </proc/cpuinfo)
# Mainline kernel fix
[ -f /proc/device-tree/model ] && HARDWARE=$(awk '/Hardware/ {print $4}' </proc/cpuinfo)
root_device=$(mountpoint -d /)
for file in /dev/* ; do
CURRENT_DEVICE=$(printf "%d:%d" $(stat --printf="0x%t 0x%T" $file))
if [ $CURRENT_DEVICE = $root_device ]; then
root_partition=$file
break;
fi
done
rootfstype=$(blkid -s TYPE -o value $root_partition)
case ${DISTRIBUTION} in
wheezy)
root_device=$(mountpoint -d /)
for file in /dev/* ; do
CURRENT_DEVICE=$(printf "%d:%d" $(stat --printf="0x%t 0x%T" $file))
if [ $CURRENT_DEVICE = $root_device ]; then
root_partition=$file
break;
fi
done
rootfstype=$(blkid -s TYPE -o value $root_partition)
;;
*)
ROOTFS=$(findmnt / | awk -F" " '/\/dev\// {print $2"\t"$3}')
set ${ROOTFS}
root_partition=$1
rootfstype=$2
;;
esac
set -e
} # collect_informations
display_alert() {
if [ "$DISTRIBUTION" == "wheezy" ]; then
echo -e "[\e[0;32m ok \x1B[0m] $1" > /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 </root/.machine.id
else
# wait for armhwinfo
sleep 3
MACHINE="$(tail -n1 /run/machine.id)"
fi
NEWHOSTNAME="$(echo "${MACHINE}" | tr '[:upper:]' '[:lower:]' | sed -e 's/+/plus/' -e 's/\ //g')"
ScriptBinName="$(echo "${NEWHOSTNAME}" | sed -e 's/2mini$/2/g' -e 's/plus2$/plus/g').bin"
ScriptBinUsed="$(readlink -f "/boot/script.bin")"
@ -173,17 +184,62 @@ autodetect_sunxi() {
} # autodetect_sunxi
do_expand_rootfs() {
device="/dev/"$(lsblk -idn -o NAME | grep mmcblk0)
PARTITIONS=$(($(fdisk -l $device | grep $device | wc -l)-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
((echo d; echo $PARTITIONS; echo n; echo p; echo ; echo $STARTFROM; echo ; echo w;) | fdisk $device) >/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
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
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 +281,4 @@ main() {
main
exit 0