cleaned up diagnostics
[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 $(basename $0):
21 This script will add a swap partition for Linux that uses hard drive space for
22 swap memory.  This increases the amount of available memory on RAM constrained
23 systems.  It accepts two parameters: (1) the instance of the swap file, which
24 should be a small number not already used for a swap partition, and (2) the
25 size of the swap file in megabytes."
26   exit 0
27 fi
28
29 # if the swap instance variable is already set, then we'll use it.
30 # this allows multiple different swap partitions to be added.
31 if [ -z "$SWAP_INSTANCE" ]; then
32   SWAP_INSTANCE=1
33 fi
34
35 # allow the amount of swap space to be determined from outside the script.
36 # this is measured in megabytes.
37 if [ -z "$SWAP_SIZE" ]; then
38   SWAP_SIZE=2048
39 fi
40
41 /bin/dd if=/dev/zero of=/var/swap.${SWAP_INSTANCE} bs=1M count=${SWAP_SIZE}
42 test_or_die "creating swap file"
43
44 /bin/chmod 600 /var/swap.${SWAP_INSTANCE}
45 test_or_die "setting swap file permissions"
46
47 /sbin/mkswap /var/swap.${SWAP_INSTANCE}
48 test_or_die "formatting swap file as swap partition"
49
50 /sbin/swapon /var/swap.${SWAP_INSTANCE}
51 test_or_die "enabling new swap partition"
52
53 free
54