-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathswap-file
123 lines (98 loc) · 3.63 KB
/
swap-file
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
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/swap-file
# Started On - Sat 27 Jan 11:49:19 GMT 2018
# Last Change - Mon 2 Apr 04:17:32 BST 2018
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
# This is an early work in progress; don't yet run on your production machines!
#----------------------------------------------------------------------------------
_VERSION_="2018-04-02"
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
SWAP-FILE ($_VERSION_)
Written by terminalforlife ([email protected])
A simple, small tool to easily create and set up a swap file.
SYNTAX: swap-file [OPTS] [NAME] [SIZE]
OPTS: --help|-h|-? - Displays this help information.
--version|-v - Output only the version datestamp.
--quiet|-q - Runs in quiet mode. Errors still output.
--debug|-D - Enables the built-in bash debugging.
NOTE: Where NAME is the swap file name to be created in /; the file name must
be alphanumeric and without any spaces.
Where SIZE is the size of the swap file to create, in MB.
FILE: The '/etc/fstab' file is by default edited.
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--version|-v)
printf "%s\n" "$_VERSION_"
exit 0 ;;
--quiet|-q)
BEQUIET="true" ;;
--debug|-D)
DEBUGME="true" ;;
*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
esac
shift
done
FST="/etc/fstab"
NAME="/${1//[![:alnum:]]}"
SIZE="${2//[!0-9]}"
declare -i DEPCOUNT=0
for DEP in /bin/{sync,grep,cp,chown,chmod,sed,dd} /sbin/{swap{on,off},mkswap}; {
if ! [ -x "$DEP" ]; then
ERR "$LINENO" "Dependency '$DEP' not met."
DEPCOUNT+=1
fi
}
[ $DEPCOUNT -eq 0 ] || exit 1
[ $# -eq 2 ] || XERR "$LINENO" "Incorrect argument(s) specified."
[[ "$SIZE" =~ [0-9]+ ]] || XERR "$LINENO" "Incorrect swap file size specified."
[ $UID -eq 0 ] || XERR "$LINENO" "Setting up a swap file requires root access."
[ "$BEQUIET" == "true" ] && exec 1> /dev/null
[ "$DEBUGME" == "true" ] && set -x
if [ -f "$NAME" ]; then
while read -e -p "File '$NAME' already exists -- replace? "; do
case "$REPLY" in
[Yy])
break ;;
[Nn])
printf "No action taken -- quitting.\n"; exit 1 ;;
*)
XERR "$LINENO" "Invalid response. Enter 'Y' or 'N' to continue." ;;
esac
done
fi
printf "Creating %dMB swap file: %s\n" "$SIZE" "$NAME"
/bin/dd if=/dev/zero of="$NAME" bs=1024K count="$SIZE"
/bin/sync "$NAME"
/bin/chown -v 0:0 "$NAME"
/bin/chmod -v 600 "$NAME"
LIBBLKID_DEBUG=all /sbin/mkswap -v "$NAME"
#TODO - Why does mkswap fail here, saying it can't read the swap header? Yet when
# I run the command separately on the terminal, all is fine.
/bin/cp -iv "$FST"{,.bak}
printf "Updating the file system table: %s\n" "$FST"
/bin/sed -i '/^UUID=.*\s.*\sswap/s/^/#/' "$FST"
if [ -f "$FST" ]; then
if ! /bin/grep -q "/$NAME none swap sw 0 0" "$FST"; then
printf "/%s none swap sw 0 0\n" "$NAME" >> "$FST" 1> /dev/null
else
XERR "$LINENO" "Current entry already present in: $FST"
fi
else
XERR "$LINENO" "Unable to find or access: $FST"
fi
LIBBLKID_DEBUG=all /sbin/swapoff -va
LIBBLKID_DEBUG=all /sbin/swapon -v "$NAME"
# vim: ft=sh noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup