How to add swap to and Amazon EC2 instance running

2019-03-16 07:24发布

问题:

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 Avail Use% Mounted on
/dev/xvda1      8.0G  1.4G  6.3G  18% /
udev            1.9G   12K  1.9G   1% /dev
tmpfs           751M  188K  750M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G     0  1.9G   0% /run/shm
/dev/xvdb       394G   79G  296G  21% /mnt

root@aux3:/root# swapon -s
Filename
root@aux3:/root# free -m
             total       used       free     shared    buffers     cached
Mem:          3750       3730         20          0         75       3433
-/+ buffers/cache:        221       3528
Swap:            0          0          0
root@aux3:/root# 

Update: found temporary solution via swap file:

(I'm still looking for "proper" way though)

dd if=/dev/zero of=/mnt/swap1 bs=1024 count=4M
mkswap /mnt/swap1
chown root.root /mnt/swap1
chmod 600 /mnt/swap1
swapon /mnt/swap1

回答1:

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.

What is swap space, and why use it?

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.

Where should I set this up?

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.

How can I set up a swap space on my ephemeral storage drive?

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.

How do I debug the problems I'm experiencing with this?

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.



回答2:

Perhaps you're looking for this: http://inprvt.com/index.php/blogs/entry/how-to-add-swap-space-on-a-linux-based-ec2-server

See the second approach. You need to re-partition your ephemeral storage device. I'd put something along these lines to /etc/rc.local:

umount /dev/xvdb # in case it is already mounted
sfdisk /dev/xvdb << EOF
,1024,82
,
;
;
EOF
mkswap /dev/xvdb1 && swapon /dev/xvdb1
mkfs.xfs -f /dev/xvdb2 && mount /dev/xvdb2 /mnt

Two things to note:

  • 1024 above is the size in blocks (82 is Linux swap partition type). It seems that for different instances block sizes can be different (just like device names). So experiment first or calculate what is right for you based on sfdisk output.
  • mkfs.xfs normally takes seconds. mkfs.ext4 may take half an hour (on a 1TB volume). YMMV depending on the filesystem you choose.


回答3:

Found swapspace daemon that takes care of creating and removing swapfiles on demand. It just needed little tuning to save swapfiles on ephemeral drive.

This seem to me most elegant solution:

DEBIAN_FRONTEND=noninteractive apt-get -y install swapspace
echo 'swappath="/mnt"' >> /etc/swapspace.conf
service swapspace restart


回答4:

I set up swapspace first (had to build one from sources), but then decided to fall back to a manual solution since I prefer more control over memory in production environment.

I assume that mounting of 2 instance block devices is already configured in /etc/fstab as /.inst0 and /.inst1.

Add something like this in /etc/rc.local:

setup_swap()
{
  for D in /.inst0 /.inst1; do
    findmnt $D || continue
    cd $D || continue
    test -r swapfile || dd if=/dev/zero of=swapfile bs=1M count=12292
    chmod 600 swapfile
    mkswap swapfile
    swapon swapfile
  done
}

setup_swap

The code is fully compatible with EC2 instance storage (aka SSD, aka "ephemeral", which is destroyed every time you stop the instance), and is reboot-friendly.

Please keep in mind it takes a while to create and/or enable the swapfiles, so give it a little time once you reboot to see if it worked. :)



回答5:

To enable swap at the boot time (after creating the swap file as per the instructions above), add the following entry to /etc/fstab:

/mnt/swap1 swap swap defaults 0 0