5ba4274efc8519daa526cf6f5d019e2c382580a0
[feisty_meow.git] / scripts / system / add_swap_mount.sh
1 #!/bin/bash
2
3 # auto-find the scripts, since we might want to run this as sudo.
4 export WORKDIR="$( \cd "$(\dirname "$0")" && /bin/pwd )"  # obtain the script's working directory.
5 export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )"
6 source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh"
7
8 if [[ $EUID != 0 ]]; then
9   echo "This script must be run as root or sudo."
10   exit 1
11 fi
12
13 # optional parameters: the instance number of the swap file (best as a number),
14 # and the size of the swap file to add.
15 SWAP_INSTANCE="$1"; shift
16 SWAP_SIZE="$1"; shift
17
18 if [ "$SWAP_INSTANCE" == "--help" ]; then
19   echo "\
20 This script will add a swap partition for Linux that uses hard drive space for
21 swap memory.  This increases the amount of available memory on RAM constrained
22 systems.  It accepts two parameters: (1) the instance of the swap file, which
23 should be a small number not already used for a swap partition, and (2) the
24 size of the swap file in megabytes."
25   exit 0
26 fi
27
28 # if the swap instance variable is already set, then we'll use it.
29 # this allows multiple different swap partitions to be added.
30 if [ -z "$SWAP_INSTANCE" ]; then
31   SWAP_INSTANCE=1
32 fi
33
34 # allow the amount of swap space to be determined from outside the script.
35 # this is measured in megabytes.
36 if [ -z "$SWAP_SIZE" ]; then
37   SWAP_SIZE=2048
38 fi
39
40 /bin/dd if=/dev/zero of=/var/swap.${SWAP_INSTANCE} bs=1M count=${SWAP_SIZE}
41 test_or_die "creating swap file"
42
43 /bin/chmod 600 /var/swap.${SWAP_INSTANCE}
44 test_or_die "setting swap file permissions"
45
46 /sbin/mkswap /var/swap.${SWAP_INSTANCE}
47 test_or_die "formatting swap file as swap partition"
48
49 /sbin/swapon /var/swap.${SWAP_INSTANCE}
50 test_or_die "enabling new swap partition"
51
52 free
53