#!/bin/bash

set -e

read -r MEM_INFO </proc/meminfo
if [[ "${MEM_INFO}" =~ ^MemTotal:\ *([0-9]+)\ kB$ ]]; then
    MEM_KB=${BASH_REMATCH[1]}
else
    echo "Failed to read memory size!" >&2
    exit 1
fi

SIZE_KB=$(( MEM_KB / 2 ))

truncate --size="${SIZE_KB}k" /root.btrfs
mkfs.btrfs --data single --metadata single --mixed /root.btrfs
# Disable whatever default size limit rootfs has
mount -o remount,size=0 /
