-
Notifications
You must be signed in to change notification settings - Fork 12
/
swap-fix
executable file
·123 lines (102 loc) · 3.87 KB
/
swap-fix
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
#!/bin/bash
#
# swap-fix - fix references of swap partition in fstab, initramfs, etc
#
# Copyright (C) 2013 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
#
# 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. See <http://www.gnu.org/licenses/gpl.html>
label=SWAP
fstab=/etc/fstab
resume=/etc/initramfs-tools/conf.d/resume
myname="${0##*/}"
verbose=1
fatal() { [[ "$1" ]] && echo "$myname: error: $1" >&2 ; exit ${2:-1} ; }
message() { ((verbose)) && printf "%s\n" "$1"; }
argerr() { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
missing() { argerr "missing ${2:+$2 }argument${1:+ from $1}." ; }
invalid() { argerr "invalid option: $1" ; }
usage() {
cat <<-USAGE
Usage: $myname [options]
USAGE
if [[ "$1" ]] ; then
cat >&2 <<- USAGE
Try '$myname --help' for more information.
USAGE
exit 1
fi
cat <<-USAGE
Add a $swaplabel label to your swap partition, and updates references
in $fstab and $resume to use LABEL instead of UUID.
Both files are only modified if necessary. $fstab is backed up prior
to edit, and $resume is created if it does not exist.
Refuses to run if none or multiple swap partitions are found on the
system. Multiple swap entries in $fstab are removed.
Options:
-h|--help - show this page.
-q|--quiet - supress informative messages, only output errors.
--label LABEL - set swap partition label to LABEL (default $label)
Copyright (C) 2013 Rodrigo Silva (MestreLion) <linux@rodrigosilva.com>
License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
USAGE
exit 0
}
# Option handling
files=()
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (( $# )); do
case "$1" in
-q|--quiet ) verbose=1 ;;
--label=* ) label="${1#*=}" ;;
--label ) shift ; label="$1" ;;
* ) invalid "$1" ;;
esac
shift
done
[[ "$(id -u)" -eq 0 ]] || fatal "$myname must be run as root"
[[ "$label" ]] || missing "--label" "LABEL"
uuid=( $(blkid -t TYPE=swap -s UUID -o value) )
(( "${#uuid[@]}" < 1 )) && fatal "no swap partitions found. Use 'mkswap' to create one"
(( "${#uuid[@]}" > 1 )) && fatal "multiple swap partitions found, too complex for me"
dev=$(blkid -U "$uuid")
devlabel=$(swaplabel "$dev" | grep 'LABEL:' | cut -d' ' -f2)
message "Found swap partition: $dev (UUID=$uuid${devlabel:+, label=$devlabel})"
if [[ "$devlabel" != "$label" ]]; then
message "Labeling swap partition $label"
swaplabel --label "$label" "$dev"
fi
if [[ $(awk -v label="$label" '$3 == "swap" && $1 ~ "^LABEL=" label "$" {print}' "$fstab") ]]; then
message "no changes necessary in $fstab"
elif [[ -z $(awk '$3 == "swap" && $1 !~ /^#/{print}' "$fstab") ]]; then
message "updating $fstab, adding reference to swap"
cp "$fstab"{,.swapfix.bak}
echo "LABEL=$label none swap sw 0 0" >> "$fstab"
else
message "updating $fstab, replacing swap reference to LABEL=$label"
cp "$fstab"{,.swapfix.bak}
awk -v label="$label" \
'$3 == "swap" && $1 !~ /^#/ {
if (swap) next
swap=1
len=length($1)-1
sub($1, sprintf("%*s", -len, "LABEL=" label))
} 1' "$fstab".swapfix.bak > "$fstab"
fi
if [[ -f "$resume" ]] && grep -q "^RESUME=LABEL=$label" "$resume"; then
message "no changes necessary in $resume"
else
message "updating $resume"
echo "RESUME=LABEL=$label" > "$resume"
fi
swapon -a