2015-08-26 09:23:30 -07:00
#!/bin/bash
set -eu -o pipefail
2015-08-26 13:51:32 -07:00
readonly USER_HOME = "/home/yellowtent"
2015-08-26 09:23:30 -07:00
readonly APPS_SWAP_FILE = "/apps.swap"
2015-08-26 14:29:54 -07:00
readonly USER_DATA_FILE = "/root/user_data.img"
readonly USER_DATA_DIR = "/home/yellowtent/data"
2015-08-26 09:23:30 -07:00
2016-10-08 21:31:11 -07:00
# detect device of rootfs (http://forums.fedoraforum.org/showthread.php?t=270316)
disk_device = " $( for d in $( find /dev -type b) ; do [ " $( mountpoint -d /) " = " $( mountpoint -x $d ) " ] && echo $d && break; done ) "
2016-01-04 15:57:52 +01:00
2015-08-26 12:15:47 -07:00
# all sizes are in mb
2017-03-13 10:32:27 -07:00
readonly physical_memory = $( LC_ALL = C free -m | awk '/Mem:/ { print $2 }' )
2017-03-23 12:04:25 -07:00
readonly swap_size = $(( ${ physical_memory } > 4096 ? 4096 : ${ physical_memory } )) # min(RAM, 4GB) if you change this, fix enoughResourcesAvailable() in client.js
2015-08-26 11:30:45 -07:00
readonly app_count = $(( ${ physical_memory } / 200 )) # estimated app count
2017-03-22 14:21:40 +01:00
readonly disk_size_bytes = $( LC_ALL = C df | grep " ${ disk_device } " | awk '{ printf $2 }' )
readonly disk_size = $(( ${ disk_size_bytes } / 1024 ))
2017-01-24 10:09:05 -08:00
readonly system_size = 10240 # 10 gigs for system libs, apps images, installer, box code, data and tmp
2015-08-28 10:46:22 -07:00
readonly ext4_reserved = $(( disk_size * 5 / 100 )) # this can be changes using tune2fs -m percent /dev/vda1
2015-08-26 11:30:45 -07:00
2016-01-04 15:57:52 +01:00
echo " Disk device: ${ disk_device } "
2015-08-26 22:54:23 -07:00
echo " Physical memory: ${ physical_memory } "
echo " Estimated app count: ${ app_count } "
2016-12-30 11:49:57 +01:00
echo " Disk size: ${ disk_size } M "
2015-08-26 22:54:23 -07:00
2016-04-10 20:46:30 -07:00
# Allocate swap for general app usage
2017-03-23 12:04:25 -07:00
readonly current_swap = $( swapon --show= "name,size" --noheadings --bytes | awk 'BEGIN{s=0}{s+=$2}END{printf "%.0f", s/1024/1024}' )
readonly needed_swap_size = $(( swap_size - current_swap))
if [ [ ${ needed_swap_size } -gt 0 ] ] ; then
echo " Need more swap of ${ needed_swap_size } M "
# compute size of apps.swap ignoring what is already set
without_apps_swap = $( swapon --show= "name,size" --noheadings --bytes | awk 'BEGIN{s=0}{if ($1!="/apps.swap") s+=$2}END{printf "%.0f", s/1024/1024}' )
apps_swap_size = $(( swap_size - without_apps_swap))
echo " Creating Apps swap file of size ${ apps_swap_size } M "
if [ [ -f " ${ APPS_SWAP_FILE } " ] ] ; then
echo "Swapping off before resizing swap"
swapoff " ${ APPS_SWAP_FILE } " || true
fi
fallocate -l " ${ apps_swap_size } m " " ${ APPS_SWAP_FILE } "
2015-08-26 09:23:30 -07:00
chmod 600 " ${ APPS_SWAP_FILE } "
mkswap " ${ APPS_SWAP_FILE } "
swapon " ${ APPS_SWAP_FILE } "
2017-03-23 12:04:25 -07:00
if ! grep -q " ${ APPS_SWAP_FILE } " /etc/fstab; then
echo "Adding swap to fstab"
echo " ${ APPS_SWAP_FILE } none swap sw 0 0 " >> /etc/fstab
fi
2015-08-26 09:23:30 -07:00
else
2017-03-23 12:04:25 -07:00
echo "Swap requirements already met"
2015-08-26 09:23:30 -07:00
fi
2016-12-30 11:53:35 +01:00
# see start.sh for the initial default size of 8gb. On small disks the calculation might be lower than 8gb resulting in a failure to resize here.
2015-08-26 15:40:48 -07:00
echo "Resizing data volume"
2016-04-10 20:46:30 -07:00
home_data_size = $(( disk_size - system_size - swap_size - ext4_reserved))
2015-08-26 15:40:48 -07:00
echo " Resizing up btrfs user data to size ${ home_data_size } M "
2016-03-07 10:37:26 -08:00
umount " ${ USER_DATA_DIR } " || true
2016-02-26 08:27:07 -08:00
# Do not preallocate (non-sparse). Doing so overallocates for data too much in advance and causes problems when using many apps with smaller data
# fallocate -l "${home_data_size}m" "${USER_DATA_FILE}" # does not overwrite existing data
2016-02-25 19:26:46 -08:00
truncate -s " ${ home_data_size } m " " ${ USER_DATA_FILE } " # this will shrink it if the file had existed. this is useful when running this script on a live system
2016-03-07 10:48:07 -08:00
mount -t btrfs -o loop,nosuid " ${ USER_DATA_FILE } " ${ USER_DATA_DIR }
2015-08-26 15:40:48 -07:00
btrfs filesystem resize max " ${ USER_DATA_DIR } "