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"
readonly BACKUP_SWAP_FILE = "/backup.swap" # used when doing app backups
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-01-04 15:57:52 +01:00
# detect device
if [ [ -b "/dev/vda1" ] ] ; then
disk_device = "/dev/vda1"
fi
if [ [ -b "/dev/xvda1" ] ] ; then
disk_device = "/dev/xvda1"
fi
2015-08-26 12:15:47 -07:00
# all sizes are in mb
2015-08-26 11:30:45 -07:00
readonly physical_memory = $( free -m | awk '/Mem:/ { print $2 }' )
2016-02-25 18:19:59 -08:00
readonly swap_size = " ${ physical_memory } " # 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
2016-01-04 15:57:52 +01:00
readonly disk_size_gb = $( fdisk -l ${ disk_device } | grep " Disk ${ disk_device } " | awk '{ print $3 }' )
2015-08-26 12:15:47 -07:00
readonly disk_size = $(( disk_size_gb * 1024 ))
readonly backup_swap_size = 1024
2016-01-01 16:29:16 +01:00
readonly system_size = 10240 # 10 gigs for system libs, apps images, installer, box code 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 } "
echo " Disk size: ${ disk_size } "
2015-08-26 09:23:30 -07:00
# Allocate two sets of swap files - one for general app usage and another for backup
# The backup swap is setup for swap on the fly by the backup scripts
if [ [ ! -f " ${ APPS_SWAP_FILE } " ] ] ; then
2015-08-26 12:22:32 -07:00
echo " Creating Apps swap file of size ${ swap_size } M "
fallocate -l " ${ 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 } "
echo " ${ APPS_SWAP_FILE } none swap sw 0 0 " >> /etc/fstab
else
echo "Apps Swap file already exists"
fi
if [ [ ! -f " ${ BACKUP_SWAP_FILE } " ] ] ; then
2015-08-26 12:15:47 -07:00
echo " Creating Backup swap file of size ${ backup_swap_size } M "
fallocate -l " ${ backup_swap_size } m " " ${ BACKUP_SWAP_FILE } "
2015-08-26 09:23:30 -07:00
chmod 600 " ${ BACKUP_SWAP_FILE } "
mkswap " ${ BACKUP_SWAP_FILE } "
else
echo "Backups Swap file already exists"
fi
2015-08-26 15:40:48 -07:00
echo "Resizing data volume"
2015-08-28 10:46:22 -07:00
home_data_size = $(( disk_size - system_size - swap_size - backup_swap_size - ext4_reserved))
2015-08-26 15:40:48 -07:00
echo " Resizing up btrfs user data to size ${ home_data_size } M "
umount " ${ USER_DATA_DIR } "
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
2015-08-26 15:40:48 -07:00
mount " ${ USER_DATA_FILE } "
btrfs filesystem resize max " ${ USER_DATA_DIR } "
2015-08-26 14:29:54 -07:00