-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcreateSwapfile.sh
executable file
·62 lines (57 loc) · 1.72 KB
/
createSwapfile.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
#!/bin/bash
#NVIDIA Jetson TX1
# Create a swap file and set up permissions
# If a parameter is passed, it should be the place to create the swapfile
SWAPDIRECTORY=$PWD
SWAPSIZE=8
AUTOMOUNT="N"
function usage
{
echo "usage: createSwapFile [[[-d directory ] [-s size] -a] | [-h]]"
echo "-d | --dir <directoryname> Directory to place swapfile"
echo "-s | --size <gigabytes>"
echo "-a | --auto Enable swap on boot in /etc/fstab "
echo "-h | --help This message"
}
while [ "$1" != "" ]; do
case $1 in
-d | --dir ) shift
SWAPDIRECTORY=$1
;;
-s | --size ) shift
SWAPSIZE=$1
;;
-a | --auto ) AUTOMOUNT="Y"
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
echo "Creating Swapfile at: " $SWAPDIRECTORY
echo "Swapfile Size: " $SWAPSIZE"G"
echo "Automount: " $AUTOMOUNT
#Create a swapfile for Ubuntu at the current directory location
sudo fallocate -l $SWAPSIZE"G" $SWAPDIRECTORY"/swapfile"
cd $SWAPDIRECTORY
#List out the file
ls -lh swapfile
# Change permissions so that only root can use it
sudo chmod 600 swapfile
#List out the file
ls -lh swapfile
#Set up the Linux swap area
sudo mkswap swapfile
#Now start using the swapfile
sudo swapon swapfile
#Show that it's now being used
swapon -s
if [ "$AUTOMOUNT" = "Y" ]; then
echo "Modifying /etc/fstab to enable on boot"
SWAPLOCATION=$SWAPDIRECTORY"/swapfile"
echo $SWAPLOCATION
sudo sh -c 'echo "'$SWAPLOCATION' none swap sw 0 0" >> /etc/fstab'
fi