Avaldatud allikas
Laadi alla allikas ZIP
CH-J Server Manager
Vaata katalooge ja faile konkreetse rakenduse vabastamiseks.
CH-J / Server Manager
377 failid
core-20261019T111616Z-160a00bfcddc
Lisatud: · Ehitatud:
1
# CH-J Server Manager - fixed remote helper for swap inspection and management (POSIX sh).2
# Started by Core as "sh -c SCRIPT chj OPERATION ARGS..." (through sudo/root for changes). Arguments are3
# validated by Core and again here. Changes are never made from the monitoring loop.4
set -u5
umask 0776
LC_ALL=C; export LC_ALL7
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin; export PATH8
FSTAB=/etc/fstab9
MARK='# CH-J chj.system-monitor'10
LOCK=/run/chj-swap.lock11
# Overridable only for the unit tests (fake /proc and fstab; nothing on the host is touched).12
PROC=${CHJ_SWAP_TEST_PROC:-/proc}13
[ -n "${CHJ_SWAP_TEST_PROC:-}" ] && FSTAB=${CHJ_SWAP_TEST_FSTAB:-$FSTAB}14
op=${1:-}15
[ $# -gt 0 ] && shift16
printf '\036CHJ_READY\n'18
fail() { printf '\036CHJ_ERR=%s\n' "$1"; [ $# -gt 1 ] && printf '%s\n' "$2" | head -c 2000 >&2; exit 3; }19
have() { command -v "$1" >/dev/null 2>&1; }20
is_root() { [ "$(id -u)" = 0 ]; }21
need_root() { is_root || fail PRIVILEGE_REQUIRED "Root privileges are required."; }22
# Swap files may live only at /swapfile*, /swap/<name> or /var/lib/swap/<name>.23
managed_path() {24
case $1 in25
/swapfile|/swapfile[A-Za-z0-9._-]*|/swap/[A-Za-z0-9._-]*|/var/lib/swap/[A-Za-z0-9._-]*) ;;26
*) return 1 ;;27
esac28
case $1 in *..*|*/./*|*//*) return 1 ;; esac29
[ ${#1} -le 96 ]30
}31
safe_path() { case $1 in /*) ;; *) return 1 ;; esac; case $1 in *[!A-Za-z0-9._/-]*|*..*) return 1 ;; esac; [ ${#1} -le 200 ]; }32
valid_int() { case $1 in ''|*[!0-9]*) return 1 ;; esac; [ ${#1} -le 9 ]; }33
# Priority on the wire: -1 = automatic (kernel assigns decreasing negative values), 0-32767 = explicit34
# (swapon(2) SWAP_FLAG_PREFER range), keep = reuse the configured/explicit priority of the area.35
valid_prio() { case $1 in -1|keep) return 0 ;; ''|*[!0-9]*) return 1 ;; esac; [ ${#1} -le 5 ] && [ "$1" -le 32767 ]; }36
meminfo() { awk -v key="$1" '$1==key":" { print $2*1024; found=1 } END { if (!found) print 0 }' "$PROC/meminfo"; }37
active_line() { awk -v p="$1" 'NR>1 && $1==p { print; exit }' "$PROC/swaps"; }38
runtime_prio() { active_line "$1" | awk '{ print $5 }'; }39
is_active() { [ -n "$(active_line "$1")" ]; }40
used_of() { active_line "$1" | awk '{ printf "%.0f", $4*1024 }'; }41
is_swap_signature() {42
page=$(getconf PAGESIZE 2>/dev/null || echo 4096)43
[ "$(dd if="$1" bs=1 skip=$((page - 10)) count=10 2>/dev/null)" = SWAPSPACE2 ]44
}45
# Mount table first: statfs reports ext4 as "ext2/ext3". The longest mount point containing $1 wins.46
fs_type() {47
t=$(findmnt -no FSTYPE -T "$1" 2>/dev/null)48
[ -n "$t" ] || t=$(awk -v p="$1" '{ m=$2; if (m=="/" || p==m || index(p, m "/")==1) if (length(m)>=best) { best=length(m); fs=$3 } } END { if (fs!="") print fs }' "$PROC/mounts" 2>/dev/null)49
[ -n "$t" ] || t=$(stat -f -c %T "$1" 2>/dev/null)50
printf '%s\n' "${t:-unknown}"51
}53
lock() {54
mkdir "$LOCK" 2>/dev/null || fail SWAP_BUSY "Another swap operation is running on this server."55
trap 'rmdir "$LOCK" 2>/dev/null' EXIT INT TERM56
}58
# swapoff only when the pages can move to RAM (keeping a reserve) or to other active swap.59
assert_swapoff_safe() {60
target=$161
used=$(used_of "$target"); [ -n "$used" ] || used=062
[ "$used" -gt 0 ] || return 063
available=$(meminfo MemAvailable); total=$(meminfo MemTotal)64
# Reserve: 10 % of RAM, at least 512 MiB, but never more than a quarter of RAM (small servers).65
reserve=$((total / 10)); [ "$reserve" -ge 536870912 ] || reserve=53687091266
[ "$reserve" -le $((total / 4)) ] || reserve=$((total / 4))67
other_free=$(awk -v p="$target" 'NR>1 && $1!=p { free += ($3-$4)*1024 } END { printf "%.0f", free+0 }' "$PROC/swaps")68
room=$((available - reserve + other_free))69
if [ "$used" -gt "$room" ]; then70
fail SWAPOFF_UNSAFE "Deactivating $target would move $((used / 1048576)) MiB into memory, but only $((room / 1048576)) MiB can be absorbed safely (MemAvailable minus a reserve of $((reserve / 1048576)) MiB plus free space in other swap areas)."71
fi72
}74
# Canonical identity of an fstab/proc swap spec: path, /dev node, UUID=, LABEL=, PARTUUID= or PARTLABEL=.75
canon() {76
if [ -n "${CHJ_SWAP_TEST_PROC:-}" ] && [ -r "$PROC/canon" ]; then77
hit=$(awk -F'|' -v s="$1" '$1==s { print $2; exit }' "$PROC/canon"); [ -n "$hit" ] && { printf '%s\n' "$hit"; return; }78
fi79
case $1 in80
UUID=*|LABEL=*|PARTUUID=*|PARTLABEL=*)81
key=${1%%=*}; val=${1#*=}; val=${val#\"}; val=${val%\"}82
dev=$(findfs "$key=$val" 2>/dev/null) || dev=""83
if [ -z "$dev" ]; then84
dir=$(printf '%s' "$key" | tr 'A-Z' 'a-z')85
[ -e "/dev/disk/by-$dir/$val" ] && dev=$(readlink -f "/dev/disk/by-$dir/$val" 2>/dev/null)86
fi87
printf '%s\n' "${dev:-$1}" ;;88
/*) [ -e "$1" ] && readlink -f "$1" 2>/dev/null || printf '%s\n' "$1" ;;89
*) printf '%s\n' "$1" ;;90
esac91
}92
# Line numbers of active (uncommented) fstab swap entries that identify the same swap space as $1.93
fstab_matches() {94
[ -r "$FSTAB" ] || return 095
want=$(canon "$1")96
awk '!/^[[:space:]]*#/ && $3=="swap" { print NR "|" $1 }' "$FSTAB" | while IFS='|' read -r n spec; do97
if [ "$spec" = "$1" ] || [ "$(canon "$spec")" = "$want" ]; then printf '%s\n' "$n"; fi98
done99
}100
fstab_entry() { fstab_matches "$1" | head -n 1; }101
# Must run in the main shell (not inside $(...)), otherwise fail could not stop the script.102
fstab_check() {103
m=$(fstab_matches "$1")104
[ "$(printf '%s\n' "$m" | grep -c .)" -le 1 ] || fail FSTAB_AMBIGUOUS "/etc/fstab contains several active swap entries for $1 (lines $(printf '%s' "$m" | tr '\n' ' ')); resolve them manually first."105
}106
fstab_pri() {107
n=$(fstab_entry "$1"); [ -n "$n" ] || return 0108
awk -v n="$n" 'NR==n+0 { k=split($4, o, ","); for (i=1;i<=k;i++) if (o[i] ~ /^pri=[0-9]+$/) print substr(o[i],5) }' "$FSTAB" | tail -n 1109
}110
# keep -> priority from fstab, else the current explicit runtime priority, else automatic.111
resolve_prio() {112
[ "$2" = keep ] || { printf '%s\n' "$2"; return; }113
p=$(fstab_pri "$1"); [ -n "$p" ] && { printf '%s\n' "$p"; return; }114
p=$(runtime_prio "$1"); case $p in ''|-*) printf '%s\n' -1 ;; *) printf '%s\n' "$p" ;; esac115
}116
# fstab_apply PATH MODE PRIO, MODE: persist (keep/update an existing entry or append one), priority (update an117
# existing entry only), remove. PRIO: -1 removes pri=, 0-32767 sets it, keep leaves the options untouched.118
# The matching line keeps its spec, other options, dump/pass fields and comment; all other lines stay byte-identical.119
fstab_apply() {120
path=$1; mode=$2; prio=$3121
[ -f "$FSTAB" ] || fail FSTAB_MISSING "/etc/fstab does not exist."122
[ ! -L "$FSTAB" ] || fail FSTAB_UNSAFE "/etc/fstab is a symbolic link."123
fstab_check "$path"; n=$(fstab_entry "$path")124
case $mode in priority|remove) [ -n "$n" ] || return 0 ;; esac125
stamp=$(date -u +%Y%m%dT%H%M%SZ)126
# Unique name: several changes within one second must not overwrite the earlier original.127
LAST_BACKUP=$(mktemp "$FSTAB.chj-backup-$stamp-XXXXXX" 2>/dev/null) && cp -p "$FSTAB" "$LAST_BACKUP" || { [ -n "${LAST_BACKUP:-}" ] && rm -f "$LAST_BACKUP"; fail FSTAB_BACKUP_FAILED "The /etc/fstab backup could not be created."; }128
tmp=$(mktemp "$FSTAB.chj-XXXXXX") || fail FSTAB_WRITE_FAILED "Temporary file failed."129
awk -v n="$n" -v mode="$mode" -v prio="$prio" -v p="$path" -v mark="$MARK" '130
function setpri(opts, k, i, o, out) {131
if (prio == "keep") return opts132
k = split(opts, o, ","); out = ""133
for (i = 1; i <= k; i++) if (o[i] != "" && o[i] !~ /^pri=/) out = out (out == "" ? "" : ",") o[i]134
if (prio ~ /^[0-9]+$/) out = out (out == "" ? "" : ",") "pri=" prio135
return out == "" ? "defaults" : out136
}137
n != "" && NR == n + 0 {138
if (mode == "remove") next139
c = index($0, "#"); comment = c ? substr($0, c) : ""; body = c ? substr($0, 1, c - 1) : $0140
nf = split(body, f, /[ \t]+/); if (f[1] == "") { for (i = 1; i < nf; i++) f[i] = f[i + 1]; nf-- }141
if (f[nf] == "") nf--142
if (nf < 4) { f[4] = "sw"; nf = 4 }143
f[4] = setpri(f[4]); line = f[1]; for (i = 2; i <= nf; i++) line = line " " f[i]144
print line (comment != "" ? " " comment : ""); next145
}146
{ print }147
END { if (mode == "persist" && n == "") printf "%s none swap %s 0 0 %s\n", p, setpri("sw"), mark }' "$FSTAB" > "$tmp" || { rm -f "$tmp"; fail FSTAB_WRITE_FAILED "fstab could not be rewritten."; }148
chmod --reference="$FSTAB" "$tmp" 2>/dev/null || chmod 644 "$tmp"149
chown --reference="$FSTAB" "$tmp" 2>/dev/null || chown 0:0 "$tmp" 2>/dev/null150
mv -f "$tmp" "$FSTAB" || { rm -f "$tmp"; fail FSTAB_WRITE_FAILED "fstab could not be replaced."; }151
ls -1t "$FSTAB".chj-backup-* 2>/dev/null | tail -n +11 | while IFS= read -r old; do rm -f -- "$old"; done152
if [ -z "${CHJ_SWAP_TEST_PROC:-}" ] && have systemctl && [ -d /run/systemd/system ]; then systemctl daemon-reload >/dev/null 2>&1 || true; fi153
printf '\036CHJ_STEP=fstab|ok|backup %s\n' "$LAST_BACKUP"154
}155
# Puts the pre-change fstab back (atomically) after a failed runtime step.156
fstab_restore() {157
[ -n "${LAST_BACKUP:-}" ] && [ -f "$LAST_BACKUP" ] || return 0158
tmp=$(mktemp "$FSTAB.chj-XXXXXX") && cp -p "$LAST_BACKUP" "$tmp" && mv -f "$tmp" "$FSTAB" && printf '\036CHJ_STEP=fstab|restored|%s\n' "$LAST_BACKUP"159
}161
# Builds a valid swap file at $1 with $2 MiB, honouring file system rules (Btrfs: NOCOW, no holes on XFS/ext4).162
build_swapfile() {163
target=$1; size=$2164
dir=${target%/*}; [ -n "$dir" ] || dir=/165
fstype=$(fs_type "$dir")166
case $fstype in167
ext2/ext3|ext4|ext2|ext3|xfs|btrfs|f2fs) ;;168
*) fail SWAP_FS_UNSUPPORTED "Swap files are not supported safely on the $fstype file system." ;;169
esac170
free=$(df -PB1 "$dir" | awk 'NR==2 { print $4 }')171
need=$((size * 1048576 + 1073741824))172
[ "$free" -ge "$need" ] || fail SWAP_DISK_FULL "Not enough free space in $dir: $((free / 1048576)) MiB free, $((need / 1048576)) MiB needed including a 1 GiB reserve."173
tmp="$dir/.${target##*/}.chj-new"174
rm -f -- "$tmp"175
if [ "$fstype" = btrfs ]; then176
if have btrfs && btrfs filesystem mkswapfile --help >/dev/null 2>&1; then177
out=$(btrfs filesystem mkswapfile --size "${size}m" "$tmp" 2>&1) || { rm -f -- "$tmp"; fail SWAP_BTRFS_FAILED "$out"; }178
else179
: > "$tmp" && chattr +C "$tmp" 2>/dev/null || { rm -f -- "$tmp"; fail SWAP_BTRFS_NOCOW "The NOCOW attribute could not be set (chattr +C); Btrfs swap files need it."; }180
dd if=/dev/zero of="$tmp" bs=1M count="$size" status=none conv=fsync || { rm -f -- "$tmp"; fail SWAP_WRITE_FAILED "Writing the swap file failed."; }181
chmod 600 "$tmp"; out=$(mkswap "$tmp" 2>&1) || { rm -f -- "$tmp"; fail SWAP_MKSWAP_FAILED "$out"; }182
fi183
else184
# dd writes every block: fallocate/truncate can leave unwritten extents that swapon rejects (e.g. XFS).185
dd if=/dev/zero of="$tmp" bs=1M count="$size" status=none conv=fsync || { rm -f -- "$tmp"; fail SWAP_WRITE_FAILED "Writing the swap file failed (disk full?)."; }186
chmod 600 "$tmp"; out=$(mkswap "$tmp" 2>&1) || { rm -f -- "$tmp"; fail SWAP_MKSWAP_FAILED "$out"; }187
fi188
chown 0:0 "$tmp"; chmod 600 "$tmp"189
mv -f -- "$tmp" "$target" || { rm -f -- "$tmp"; fail SWAP_WRITE_FAILED "The swap file could not be moved into place."; }190
if have restorecon; then restorecon "$target" >/dev/null 2>&1 || true; fi191
}193
try_swapon() {194
case $2 in -*|'') out=$(swapon "$1" 2>&1) ;; *) out=$(swapon -p "$2" "$1" 2>&1) ;; esac195
}196
activate() { try_swapon "$1" "$2" || fail SWAPON_FAILED "swapon $1 failed: $out"; }197
# Applies a priority to a running swap area. The kernel cannot change the priority of an active area, so it is198
# swapped off and on again; on any failure the previous priority (and fstab) is restored, never left inactive.199
reprioritize() {200
target=$1; want=$2; old=$(runtime_prio "$target")201
out=$(swapoff "$target" 2>&1) || { fstab_restore; fail SWAPOFF_FAILED "$out"; }202
if ! try_swapon "$target" "$want"; then203
reason=$out204
case $old in -*|'') swapon "$target" >/dev/null 2>&1 ;; *) swapon -p "$old" "$target" >/dev/null 2>&1 ;; esac205
fstab_restore206
fail SWAPON_FAILED "swapon with priority $want failed ($reason); the swap area was reactivated with its previous priority and /etc/fstab was restored."207
fi208
}210
status() {211
printf '\036CHJ_SECTION=swaps\n'; cat /proc/swaps212
printf '\036CHJ_SECTION=meminfo\n'; grep -E '^(MemTotal|MemAvailable|SwapTotal|SwapFree):' /proc/meminfo213
}215
op_inspect() {216
printf '\036CHJ_SECTION=meta\n'217
printf 'root=%s\n' "$(is_root && echo 1 || echo 0)"218
printf 'systemd=%s\n' "$( (have systemctl && [ -d /run/systemd/system ]) && echo 1 || echo 0)"219
printf 'btrfsMkswapfile=%s\n' "$( (have btrfs && btrfs filesystem mkswapfile --help >/dev/null 2>&1) && echo 1 || echo 0)"220
printf 'os=%s\n' "$(sed -n 's/^PRETTY_NAME=//p' /etc/os-release 2>/dev/null | head -n 1 | tr -d '"')"221
status222
printf '\036CHJ_SECTION=fstab\n'223
# line|canonical device or path|raw line (the canonical identity matches UUID=/LABEL= entries to /proc/swaps).224
[ -r "$FSTAB" ] && awk '$3=="swap" || /^[^#]*[[:space:]]swap[[:space:]]/ { print NR "|" $1 "|" $0 }' "$FSTAB" | while IFS='|' read -r n spec raw; do225
printf '%s|%s|%s\n' "$n" "$(canon "$spec")" "$raw"226
done227
printf '\036CHJ_SECTION=units\n'228
if have systemctl && [ -d /run/systemd/system ]; then229
systemctl list-units --type=swap --all --no-legend --plain 2>/dev/null | awk '{ print $1 "|" $3 "|" $4 }' | head -n 32230
fi231
printf '\036CHJ_SECTION=files\n'232
listed=" $(awk 'NR>1 { printf "%s ", $1 }' /proc/swaps) $( [ -r "$FSTAB" ] && awk '$3=="swap" && $1 ~ /^\// { printf "%s ", $1 }' "$FSTAB") "233
for path in $listed /swapfile /swapfile* /swap/* /var/lib/swap/*; do234
[ -e "$path" ] || continue235
# Directory candidates count only when they are regular files carrying the swap signature.236
case $listed in *" $path "*) ;; *) [ -f "$path" ] && [ ! -L "$path" ] && is_swap_signature "$path" || continue ;; esac237
case $path in /dev/*) kind=partition; [ "${path#/dev/zram}" != "$path" ] && kind=zram ;; *) kind=file ;; esac238
if [ "$kind" = file ]; then239
printf '%s|%s|%s|%s|%s|%s\n' "$path" "$kind" "$(fs_type "${path%/*}/")" "$(stat -c '%s|%U|%a' "$path" 2>/dev/null)" "$(managed_path "$path" && echo managed || echo foreign)" "$(df -PB1 "${path%/*}/" 2>/dev/null | awk 'NR==2 { print $4 }')"240
else241
printf '%s|%s|-|%s|0|-|foreign|-\n' "$path" "$kind" "$(blockdev --getsize64 "$path" 2>/dev/null || echo 0)"242
fi243
done | awk -F'|' '!seen[$1]++'244
printf '\036CHJ_SECTION=locations\n'245
for dir in / /swap /var/lib/swap; do246
[ -d "$dir" ] || continue247
printf '%s|%s|%s\n' "$dir" "$(fs_type "$dir")" "$(df -PB1 "$dir" 2>/dev/null | awk 'NR==2 { print $4 }')"248
done249
printf '\036CHJ_SECTION=zram\n'250
for zram in /sys/block/zram*; do [ -r "$zram/mm_stat" ] && printf '%s|%s|%s\n' "$(basename "$zram")" "$(cat "$zram/disksize")" "$(tr -s ' ' ',' < "$zram/mm_stat")"; done251
printf '\036CHJ_SECTION=end\n'252
}254
op_manage() {255
[ $# -eq 5 ] || [ $# -eq 6 ] || fail ARGUMENTS_INVALID "Invalid arguments."256
action=$1; path=$2; size=$3; prio=$4; persist=$5; apply=${6:-now}257
need_root258
safe_path "$path" || fail SWAP_PATH_INVALID "Invalid swap path."259
valid_int "$size" && valid_prio "$prio" || fail ARGUMENTS_INVALID "Invalid size or priority."260
case $persist in 0|1|keep) ;; *) fail ARGUMENTS_INVALID "Invalid persistence flag." ;; esac261
case $apply in now|later) ;; *) fail ARGUMENTS_INVALID "Invalid apply mode." ;; esac262
lock263
[ -r "$FSTAB" ] && fstab_check "$path"264
case $path in /dev/zram*) fail SWAP_ZRAM_READ_ONLY "zram is shown read-only; CH-J does not configure it." ;; esac265
case $action in266
create)267
[ "$prio" != keep ] || fail ARGUMENTS_INVALID "A new swap file needs automatic or explicit priority."268
managed_path "$path" || fail SWAP_PATH_INVALID "New swap files may be created only as /swapfile*, /swap/<name> or /var/lib/swap/<name>."269
[ ! -e "$path" ] || fail SWAP_EXISTS "The file already exists."270
[ "$size" -ge 64 ] && [ "$size" -le 65536 ] || fail SWAP_SIZE_INVALID "Size must be between 64 MiB and 64 GiB."271
mkdir -p "${path%/*}" 2>/dev/null272
build_swapfile "$path" "$size"273
printf '\036CHJ_STEP=create|ok|%s MiB\n' "$size"274
activate "$path" "$prio"; printf '\036CHJ_STEP=activate|ok|\n'275
[ "$persist" = 1 ] && fstab_apply "$path" persist "$prio" ;;276
activate)277
[ -e "$path" ] || fail SWAP_NOT_FOUND "The swap space does not exist."278
is_active "$path" && fail SWAP_ALREADY_ACTIVE "The swap space is already active."279
prio=$(resolve_prio "$path" "$prio")280
activate "$path" "$prio"; printf '\036CHJ_STEP=activate|ok|\n'281
if [ "$persist" = 1 ]; then fstab_apply "$path" persist keep; elif [ "$persist" = 0 ]; then fstab_apply "$path" remove keep; fi ;;282
deactivate)283
is_active "$path" || fail SWAP_NOT_ACTIVE "The swap space is not active."284
assert_swapoff_safe "$path"285
out=$(swapoff "$path" 2>&1) || fail SWAPOFF_FAILED "$out"; printf '\036CHJ_STEP=deactivate|ok|\n'286
[ "$persist" = 0 ] && fstab_apply "$path" remove keep ;;287
priority)288
[ "$prio" != keep ] || fail ARGUMENTS_INVALID "Choose automatic or an explicit priority."289
n=$(fstab_entry "$path"); current=$(runtime_prio "$path")290
[ -n "$n" ] || [ -n "$current" ] || [ -e "$path" ] || fail SWAP_NOT_FOUND "The swap space is neither active nor configured in /etc/fstab."291
# Already effective: explicit value equal, or automatic requested and the kernel assigned one (negative).292
same=0; case $prio:$current in -1:-*) same=1 ;; *) [ "$prio" = "$current" ] && same=1 ;; esac293
runtime=0; [ -n "$current" ] && [ "$apply" = now ] && [ $same = 0 ] && runtime=1294
# Check memory first, so that nothing is changed when the area cannot be swapped off safely.295
[ $runtime = 1 ] && assert_swapoff_safe "$path"296
if [ -n "$n" ]; then fstab_apply "$path" priority "$prio"; printf '\036CHJ_STEP=priority-saved|ok|%s\n' "$prio"297
elif [ "$persist" = 1 ]; then fstab_apply "$path" persist "$prio"; printf '\036CHJ_STEP=priority-saved|ok|%s\n' "$prio"298
else printf '\036CHJ_STEP=priority-saved|none|not in /etc/fstab\n'; fi299
if [ -z "$current" ]; then printf '\036CHJ_STEP=priority-runtime|skipped|inactive\n'300
elif [ $same = 1 ]; then printf '\036CHJ_STEP=priority-runtime|ok|already %s\n' "$current"301
elif [ $runtime = 1 ]; then reprioritize "$path" "$prio"; printf '\036CHJ_STEP=priority-runtime|ok|%s\n' "$(runtime_prio "$path")"302
else printf '\036CHJ_STEP=priority-runtime|pending|next activation or restart\n'; fi ;;303
persist)304
[ -e "$path" ] || is_active "$path" || fail SWAP_NOT_FOUND "The swap space does not exist."305
if [ "$persist" = 1 ]; then306
if [ -n "$(fstab_entry "$path")" ]; then printf '\036CHJ_STEP=fstab|ok|already present\n'; else fstab_apply "$path" persist "$(resolve_prio "$path" "$prio")"; fi307
else fstab_apply "$path" remove keep; fi ;;308
resize)309
managed_path "$path" || fail SWAP_MANUAL_ONLY "Only swap files at /swapfile*, /swap/* or /var/lib/swap/* are resized by CH-J."310
[ -f "$path" ] && [ ! -L "$path" ] || fail SWAP_NOT_FOUND "The swap file does not exist."311
[ "$size" -ge 64 ] && [ "$size" -le 65536 ] || fail SWAP_SIZE_INVALID "Size must be between 64 MiB and 64 GiB."312
prio=$(resolve_prio "$path" "$prio"); old=$(runtime_prio "$path")313
was_active=0314
if is_active "$path"; then assert_swapoff_safe "$path"; was_active=1; fi315
# The new file is built next to the old one first; the old file is replaced only after mkswap succeeded.316
if [ $was_active = 1 ]; then out=$(swapoff "$path" 2>&1) || fail SWAPOFF_FAILED "$out"; fi317
( build_swapfile "$path" "$size" ) || { [ $was_active = 1 ] && try_swapon "$path" "$old"; fail SWAP_RESIZE_FAILED "The new swap file could not be created; the previous file was reactivated."; }318
printf '\036CHJ_STEP=resize|ok|%s MiB\n' "$size"319
if [ $was_active = 1 ]; then activate "$path" "$prio"; printf '\036CHJ_STEP=activate|ok|\n'; fi ;;320
delete)321
managed_path "$path" || fail SWAP_MANUAL_ONLY "Only swap files at /swapfile*, /swap/* or /var/lib/swap/* are deleted by CH-J; partitions are never deleted."322
[ -f "$path" ] && [ ! -L "$path" ] || fail SWAP_NOT_FOUND "The swap file does not exist."323
if is_active "$path"; then assert_swapoff_safe "$path"; out=$(swapoff "$path" 2>&1) || fail SWAPOFF_FAILED "$out"; printf '\036CHJ_STEP=deactivate|ok|\n'; fi324
fstab_apply "$path" remove keep325
rm -f -- "$path" || fail SWAP_DELETE_FAILED "The file could not be removed."326
printf '\036CHJ_STEP=delete|ok|\n' ;;327
*) fail ACTION_INVALID "Unsupported swap action." ;;328
esac329
status330
printf '\036CHJ_RESULT=ok\n'331
}333
case $op in334
safety-check) [ -n "${CHJ_SWAP_TEST_PROC:-}" ] || fail ACTION_INVALID "Test-only operation."; assert_swapoff_safe "$1"; printf 'safe\n' ;;335
fstab-test) [ -n "${CHJ_SWAP_TEST_PROC:-}" ] && [ -n "${CHJ_SWAP_TEST_FSTAB:-}" ] || fail ACTION_INVALID "Test-only operation."336
case $1 in resolve) resolve_prio "$2" "$3" ;; *) fstab_apply "$2" "$1" "$3" ;; esac ;;337
inspect) op_inspect ;;338
manage) op_manage "$@" ;;339
*) fail ACTION_INVALID "Unsupported operation." ;;340
esacSHA-256
69e8fba89e27de0c05c5b6606ebae04fe1e37ac8bd45f332abd0f2f8ecaf63afCH-J Proprietary Software License 1.14 · Allikas on esitatud CH-J Proprietary Software License 1.14 all. Selle kättesaadavus ei muuda litsentsitingimusi ega anna täiendavaid õigusi.
Arhiiv SHA-256
a9605c0870a223b6a8542bb0705edd51d8b04f3069582653417a8397fabcc09a