@@ -11,36 +11,65 @@ use crate::{
1111/// The target directory inside the VM guest OS where Orion is deployed
1212const ORION_TARGET_DIR : & str = "/home/orion/orion-runner" ;
1313
14- /// Swap file path and size for Orion build VMs (16 GB RAM by default).
14+ /// Swap file path and size bounds for Orion build VMs (16 GB RAM by default).
1515const VM_SWAP_FILE : & str = "/swapfile" ;
16- const VM_SWAP_SIZE_GB : u32 = 8 ;
16+ const VM_SWAP_TARGET_SIZE_GB : u32 = 8 ;
17+ const VM_SWAP_MIN_SIZE_GB : u32 = 1 ;
18+ const VM_SWAP_FREE_SPACE_RESERVE_GB : u32 = 1 ;
1719
1820/// Create and enable a swap file if not already active.
1921async fn setup_vm_swap ( machine : & KeepAliveMachine ) -> Result < ( ) > {
2022 info ! (
21- "[orion-deploy] Configuring {} GB swap at {}" ,
22- VM_SWAP_SIZE_GB , VM_SWAP_FILE
23+ "[orion-deploy] Configuring up to {} GB swap at {}" ,
24+ VM_SWAP_TARGET_SIZE_GB , VM_SWAP_FILE
2325 ) ;
2426 let cmd = format ! (
2527 r#"set -e
28+ swap_active=0
2629if swapon --show --noheadings 2>/dev/null | grep -q '{swap_file}'; then
2730 echo 'swap already active'
31+ swap_active=1
2832elif [ -f '{swap_file}' ]; then
2933 swapon '{swap_file}'
34+ swap_active=1
3035else
31- fallocate -l {size_g}G '{swap_file}' 2>/dev/null \
32- || dd if=/dev/zero of='{swap_file}' bs=1M count=$(( {size_g} * 1024 )) status=none
33- chmod 600 '{swap_file}'
34- mkswap '{swap_file}'
35- swapon '{swap_file}'
36+ target_kb=$(( {target_gb} * 1024 * 1024 ))
37+ min_kb=$(( {min_gb} * 1024 * 1024 ))
38+ reserve_kb=$(( {reserve_gb} * 1024 * 1024 ))
39+ avail_kb=$(df -Pk "$(dirname '{swap_file}')" | awk 'NR == 2 {{ print $4 }}')
40+ if [ -z "$avail_kb" ]; then
41+ echo 'unable to determine available disk space for swap' >&2
42+ exit 1
43+ fi
44+ usable_kb=$(( avail_kb - reserve_kb ))
45+ if [ "$usable_kb" -lt "$min_kb" ]; then
46+ echo "skipping swap: only ${{avail_kb}} KiB available, reserving ${{reserve_kb}} KiB"
47+ else
48+ if [ "$usable_kb" -gt "$target_kb" ]; then
49+ swap_kb="$target_kb"
50+ else
51+ swap_kb="$usable_kb"
52+ fi
53+ echo "creating ${{swap_kb}} KiB swap file"
54+ fallocate -l "${{swap_kb}}K" '{swap_file}' 2>/dev/null \
55+ || dd if=/dev/zero of='{swap_file}' bs=1024 count="$swap_kb" status=none
56+ chmod 600 '{swap_file}'
57+ mkswap '{swap_file}'
58+ swapon '{swap_file}'
59+ swap_active=1
60+ fi
61+ fi
62+ if [ "$swap_active" -eq 1 ]; then
63+ grep -qF '{swap_file}' /etc/fstab 2>/dev/null \
64+ || echo '{swap_file} none swap sw 0 0' >> /etc/fstab
3665fi
37- grep -qF '{swap_file}' /etc/fstab 2>/dev/null \
38- || echo '{swap_file} none swap sw 0 0' >> /etc/fstab
3966swapon --show
4067free -h | head -2
4168"# ,
4269 swap_file = VM_SWAP_FILE ,
43- size_g = VM_SWAP_SIZE_GB ,
70+ target_gb = VM_SWAP_TARGET_SIZE_GB ,
71+ min_gb = VM_SWAP_MIN_SIZE_GB ,
72+ reserve_gb = VM_SWAP_FREE_SPACE_RESERVE_GB ,
4473 ) ;
4574 let output = machine. exec ( & cmd) . await ?;
4675 info ! (
0 commit comments