-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemergency_swap.sh
executable file
·117 lines (93 loc) · 2.83 KB
/
emergency_swap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env bash
#
# This script creates, enables, and resets an emergency swap file. What is an
# emergency swap file? If you are close to running out of memory, you can enable
# a previously unused but created swap file to prevent and OOM. Later, when
# whatever you are doing is gone, you can reset this swap file, by swapoff,
# overwrite, and re-initalization so its ready for next time.
# Licensed under the GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
# Swapfile size, in megabytes. Default is 16GB
SIZE=16384
# Path of emergency swap file
SWAP_FILE="/emergency_swap"
# Source for creating file
FILL_SRC="/dev/zero"
WIPE_CMD="shred -f -n 1"
help_and_exit(){
cat 1>&2 << EOF
emergency_swap.sh:
Create, activate and reset emergency swap. Emergency swap is a ready to go swap
file that can be quickly turned on if you are in danger of running out of
memory. upon turning off, this script will wipe and reset the swapfile to
prevent data leakage. Config on the top of the script
Usage:
emergency_swap.sh [init|on|off]
commands:
init - create the swap file, wipe and re-create if exists
on - turn on emergency swap
off - turn off and reset emergency swap. swapoff, wipe and recreate
swap file
EOF
exit 4
}
message(){
echo "emergency_swap.sh: ${@}"
}
exit_with_error(){
echo 1>&2 "emergency_swap.sh: ERROR: ${2}"
exit ${1}
}
warn(){
echo 1>&2 "emergency_swap.sh: WARN: ${@}"
}
submsg(){
echo "[+] ${@}"
}
create_swap_file(){
local -i exit_code=0
local block_size="1024k" # One Megabyte
if [ -f "${SWAP_FILE}" ];then
message "${SWAP_FILE} exits, removing first..."
submsg "Wipe"
${WIPE_CMD} --remove "${SWAP_FILE}" || \
exit_with_error 1 "Cannot Remove stale swap file, exiting!"
fi
message "Creating emergency swap"
dd if="${FILL_SRC}" of="${SWAP_FILE}" bs=${block_size} count=${SIZE} status=progress || exit_code+=1
mkswap ${SWAP_FILE} || exit_code+=1
chmod 0 ${SWAP_FILE} || exit_code+=1
return ${exit_code}
}
emergency_swap_on(){
message "Turning emergency swap on"
[ ! -f "${SWAP_FILE}" ] && exit_with_error 2 "Swapfile not found, did you init first?"
chmod 0 "${SWAP_FILE}" || warn "Could not set permissions on ${SWAP_FILE}"
swapon "${SWAP_FILE}" || exit_with_error 1 "Could not swapon, root?"
}
emergency_swap_off(){
message "Turning emergency swap off, and resetting"
submsg "Swapoff"
swapoff "${SWAP_FILE}" || exit_with_error 1 "Could not swapoff, root?"
submsg "Wipe"
${WIPE_CMD} "${SWAP_FILE}" || warn "Wipe on ${SWAP_FILE} failed"
submsg "Reset"
mkswap "${SWAP_FILE}" || exit_with_error 1 "Could not recreate swap."
}
main(){
local command="${1}"
case ${command} in
init)
create_swap_file || exit_with_error 1 "Swapfile creation failed"
;;
on)
emergency_swap_on
;;
off)
emergency_swap_off
;;
*)
help_and_exit
;;
esac
}
main "${@}"