-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathswapfile
More file actions
executable file
·163 lines (142 loc) · 4.01 KB
/
swapfile
File metadata and controls
executable file
·163 lines (142 loc) · 4.01 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
# SPDX-FileCopyrightText: 2023 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command blkid grep mkswap swapoff swapon && exit 3
function set_swap_on {
# First, make sure that SWAPFILE is defined.
if ! [ -v SWAPFILE ] || [ -z "${SWAPFILE}" ]; then
show_error "ERROR: SWAPFILE is not defined. Exiting."
exit 3
fi
# Create destination dir for swap file if it doesn't exist."
local swapdir
swapdir="$(dirname "${SWAPFILE}")"
if [ "${swapdir}" != '/' ] && ! [ -d "${swapdir}" ]; then
mkdir -p "${swapdir}"
fi
# Create swapfile if it doesn't exist.
if ! [ -f "${SWAPFILE}" ]; then
show_info "Creating ${SWAPFILE@Q}"
dd if=/dev/zero of="${SWAPFILE}" bs=1M count="${SWAPSIZE}" status=progress
sync
fi
# Set permissions.
if [[ "$(stat -c "%a" "${SWAPFILE}")" != 600 ]]; then
show_info "Setting ${SWAPFILE@Q} permissions"
chmod 0600 "${SWAPFILE}"
fi
# Create UUID.
if ! blkid -o value "${SWAPFILE}" | grep -q "^swap$"; then
show_info "Setting ${SWAPFILE@Q} UUID"
mkswap -U clear "${SWAPFILE}"
fi
# Check if swap partition active. If not run
if ! swapon --show | grep -q "^${SWAPFILE}\s"; then
show_info "Activating ${SWAPFILE@Q}"
swapon "${SWAPFILE}"
fi
}
function set_swap_off {
# First, make sure that SWAPFILE is defined.
if ! [ -v SWAPFILE ] || [ -z "${SWAPFILE}" ]; then
show_error "ERROR: SWAPFILE is not defined. Exiting."
exit 3
fi
# Disable swap partition if it is active.
if swapon --show | grep -q "^${SWAPFILE}\s"; then
show_info "Flushing cache"
sync
[ -e /proc/sys/vm/drop_caches ] && echo 3 > /proc/sys/vm/drop_caches
sync
show_info "Deactivating ${SWAPFILE@Q}"
swapoff "${SWAPFILE}"
fi
# Delete swap file if it exists.
if [ -f "${SWAPFILE}" ]; then
show_info "Deleting ${SWAPFILE@Q}"
rm "${SWAPFILE}"
fi
}
function print_usage {
show_header "Usage: swapfile <on|off> ..."
show_listitem "\
-f|--file path for swap file (default: /swapfile)
-s|--size size of swap file in Mb (default: 8192)
-h|--help print (this) help message"
}
OPTIONS=f:hs:
LONGOPTIONS=file:,help,size:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-f | --file)
SWAPFILE="${2}"
shift 2
;;
-h | --help)
print_usage
exit
;;
-s | --size)
SWAPSIZE="${2}"
shift 2
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid option."
exit 3
;;
esac
done
if ! [ "${#}" -eq 1 ]; then
show_error "ERROR: Incorrect # of parameters. Exiting."
exit 3
fi
SWAPFILE="${SWAPFILE:-/swapfile}"
SWAPSIZE="${SWAPSIZE:-8192}"
case "${1}" in
on)
if [ ${EUID} -eq 0 ]; then
set_swap_on
else
sudo bash -c "SWAPSIZE=${SWAPSIZE}; SWAPFILE=${SWAPFILE}; source ${ROOT}/globals; $(declare -f set_swap_on); set_swap_on"
fi
;;
off)
if [ ${EUID} -eq 0 ]; then
set_swap_off
else
sudo bash -c "SWAPSIZE=${SWAPSIZE}; SWAPFILE=${SWAPFILE}; source ${ROOT}/globals; $(declare -f set_swap_off); set_swap_off"
fi
;;
help)
print_usage
exit
;;
*)
show_error "ERROR: ${1@Q} not understood. Exiting."
print_usage
exit 3
;;
esac