Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/nixbox
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ NFTEOF
;;
esac

# Raise FD limit before launching virtiofsd and cloud-hypervisor
ulimit -n 65536
# Raise FD limit before launching virtiofsd and cloud-hypervisor (#18).
raise_nofile 524288

# --- Start virtiofsd for nix-store share (required by microvm config) ---
log "==> Starting virtiofsd for nix-store..."
Expand Down Expand Up @@ -542,7 +542,7 @@ do_mount() {
done

local virtiofs_sock="$run_dir/virtiofs-${mount_idx}.sock"
ulimit -n 65536
raise_nofile 524288
virtiofsd \
--socket-path="$virtiofs_sock" \
--shared-dir="$MOUNT_SOURCE" \
Expand Down
34 changes: 34 additions & 0 deletions lib/functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ die() { printf '\r%s\n' "ERROR: $*" >&2; exit 1; }
log() { printf '\r%s\n' "$*"; }
log_sub() { printf '\r %s\n' "$*"; }

# ---------------------------------------------------------------------------
# Process limits
# ---------------------------------------------------------------------------

# Raise the current shell's NOFILE soft limit to $1 (default 524288) without
# lowering an already-higher hard limit. virtiofsd with --cache=auto
# accumulates backing-file FDs and pins hot-cache shares at the ceiling (#18).
# If the session's hard limit is below target (e.g. locked-down CI runners),
# sudo prlimit raises this shell process's NOFILE hard+soft limits so bash's
# ulimit can then succeed. Children inherit both.
raise_nofile() {
local target="${1:-524288}"
local current_soft current_hard

current_soft=$(ulimit -Sn) || die "Failed to read NOFILE soft limit"
current_hard=$(ulimit -Hn) || die "Failed to read NOFILE hard limit"

if [ "$current_soft" = "unlimited" ] || [ "$current_soft" -ge "$target" ]; then
return 0
fi

if [ "$current_hard" = "unlimited" ] || [ "$current_hard" -ge "$target" ]; then
ulimit -Sn "$target" \
|| die "Failed to raise NOFILE soft limit to $target"
return 0
fi

log "==> Raising NOFILE hard limit to $target (requires sudo)..."
sudo prlimit --pid "$BASHPID" --nofile="$target:$target" \
|| die "Failed to raise NOFILE hard limit to $target"
ulimit -Sn "$target" \
|| die "Failed to raise NOFILE soft limit to $target after raising hard limit"
}

# ---------------------------------------------------------------------------
# Network derivation (pure — depends only on slot + name)
# ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ for pidfile in .nixbox/state/virtiofsd_*_pid; do
pid=$(cat "$pidfile")
tag=$(basename "$pidfile" | sed 's/virtiofsd_//;s/_pid//')
max_fds=$(awk '/^Max open files/{print $4}' "/proc/$pid/limits")
if [ "$max_fds" -ge 65536 ]; then
if [ "$max_fds" -ge 524288 ]; then
echo " ok: virtiofsd ($tag) has $max_fds max FDs"
else
echo " FAIL: virtiofsd ($tag) has $max_fds max FDs, expected >= 65536"
echo " FAIL: virtiofsd ($tag) has $max_fds max FDs, expected >= 524288"
exit 1
fi
done
Expand Down
Loading