default Ubuntu 12.04 LTS doesn\'t create swap for some reason. Is there \"proper\" way to add it after install?
root@aux3:/root# df -h Filesystem Size Used Avai
Because I've spent the better part of a day understanding this problem for myself, and because pulling up ayurchen's link required a google cache search, I thought I'd post a slightly more detailed walkthrough, drawing from ayurchen's very helpful answer.
Swap space is disk space used by Linux (and most other operating systems) to store objects from memory when real physical memory starts becoming crowded. Because disk is generally slower than memory, Linux stores in swap the least recently used objects first, and keeps as much as it can in memory. It's generally recommended to have a swap space equal in size to your memory, if possible. See more details about swap space here.
The Ubuntu 12.04 LTS AMI, which I also started with, is configured with no swap space initially, as the size and number of your storage devices can vary. But most come with a large free ephemeral storage device automatically. Since S3 storage is limited by cost, the ephemeral drive is a good place for a swap file. Mine has the same device name mentioned in the question, /dev/xvdb
, but you can configure this during instance launch.
Swap space can be set up in a single file or on a partition of a device. A file can be represented by non-adjacent blocks on a disk, whereas a partition is a predefined set of adjacent blocks. Since disk read-write is faster you don't have to keep moving the read head long distances, we get better performance using a partition. (This is why the question above discounts using a file as a temporary solution.)
Linux comes with many programs to manage disk partitions, including fdisk, sfdisk, parted, etc. We will be using sfdisk
, because it can accept all the necessary arguments from a shell script. This is important, because ephemeral storage is lost every time we "stop" our instance. Thus, we set up a script in a file that automatically runs each time the instance is started, /etc/rc.local
.
# Unmount the drive in-case it is already mounted. Umount throws an error if
# it wasn't mounted, so we add || : to continue the script in that case
umount /dev/xvdb || :
# Each line below is a partition (4 maximum master partitions for this partition
# type). Can can generally use the default arguments, supplying only the amount
# of space we want in blocks (512 came out to ~4gb for me), and the partition
# type (82 for swap, 83 for general linux is default). This will create:
# dev/xvdb1 with 1024 blocks
# dev/xvdb2 with the remainder of the disk
sfdisk /dev/xvdb << EOF
,512,82
;
;
;
EOF
# Now we format the swap partition:
mkswap /dev/xvdb1
# And the remainder. You can choose amoung the various filesystem types, but
# make sure you have the necessary formatter installed. To check, ls /sbin/mk*
mkfs.ext4 /dev/xvdb2
Finally, I preferred to include my mounting information in /etc/fstab
, which is a system file on Ubuntu that prescribes how to handle various available devices automatically. It is also run at start up.
/dev/xvdb1 swap swap sw,nobootwait 0 0
/dev/xvdb2 /mnt2 ext4 defaults,nobootwait 0 0
The nobootwait option is to ensure Ubuntu doesn't hang on boot with "The disk drive for /dev/xvdb(1/2) is not ready yet or not present. Continue to wait, or Press S to skip mounting or M for manual recovery."
Make sure you create a folder at /mnt2
or wherever you plan on mounting this with mkdir.
You can run the rc.local script with sudo /etc/rc.local
to watch it run an look for problems. sudo fdisk -l
should show your new partitions after it runs. If that looks ok, try mounting your devices with sudo mount /dev/xvdb1
. This will use the configuration you've saved in fstab. If that fails, try playing around with your mounting parameters and adjust fstab accordingly.