-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswap_add.sh
84 lines (72 loc) · 1.63 KB
/
swap_add.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
#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
#set -x
# path like /swapfile
SW_FILE="$1"
# size like 1G
SW_SIZE="$2"
#========================================
# Handle errors
if [[ -z $SW_FILE || -z $SW_SIZE ]]
then
echo "ERROR. Please write correct arguments for command, for ex: ./swap_add.sh /swapfile 1G"
exit 0
fi
swapon -s | grep "$SW_FILE" > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "ERROR. $SW_FILE is already created. Exit"
exit 0
fi
#========================================
# Determine OS
OS_TYPE=$(uname -a | awk '{print $2}')
if [ "$OS_TYPE" = "ubuntu" ]
then
echo "UBUNTU"
fallocate -l "$SW_SIZE" "$SW_FILE"
chmod 600 "$SW_FILE"
chown root:root "$SW_FILE"
mkswap "$SW_FILE"
swapon "$SW_FILE"
grep -q "$SW_FILE" /etc/fstab
if [ $? -ne 0 ]
then
echo "$SW_FILE none swap sw 0 0" >> /etc/fstab
fi
grep -q "vm.swappiness" /etc/sysctl.conf
if [ $? -ne 0 ]
then
sysctl vm.swappiness=5
echo "vm.swappiness = 5" >> /etc/sysctl.conf
fi
elif [ "$OS_TYPE" = "centos" ]
then
echo "CENTOS"
fallocate -l "$SW_SIZE" "$SW_FILE"
chmod 600 "$SW_FILE"
chown root:root "$SW_FILE"
mkswap "$SW_FILE"
swapon "$SW_FILE"
grep -q "$SW_FILE" /etc/fstab
if [ $? -ne 0 ]
then
echo "$SW_FILE swap swap sw 0 0" >> /etc/fstab
fi
grep -q "vm.swappiness" /etc/sysctl.conf
if [ $? -ne 0 ]
then
sysctl vm.swappiness=5
echo "vm.swappiness = 5" >> /etc/sysctl.conf
fi
else
echo "ERROR. Unsupport system"
exit 0
fi
#========================================
# Rezalt
swapon -s | grep "$SW_FILE" > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "$SW_SIZE $SW_FILE is successfully created"
fi