How to add swap and Amazon EC2 instance with Ubuntu 12.04 LTS?

default Ubuntu 12.04 LTS for some reason does not create swap. Is there a β€œright” way to add it after installation?

  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 a workaround through the swap file:

(I'm still looking for the "right" way)

  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
+6
source share
5 answers

Because I spent most of the day understanding this issue for myself, and because pulling up the ayurchen link requires a google cache search, I thought I would post a slightly more detailed walkthrough, extracting a very useful answer from ayurchen.

What is swap space and why use it?

The swap space is the disk space used by Linux (and most other operating systems) to store objects from memory when real physical memory begins to overflow. Since a drive is generally slower than memory, Linux first saves the least used objects in the swap and stores as much as possible in memory. It is generally recommended, if possible, to have a swap space of equal size compared to your memory. Read more about the swap area here .

Where should I install this?

Ubuntu 12.04 LTS AMI, which I also started working with, is first configured without swap space, as the size and number of your storage devices may vary. But most of them come with a large free ephemeral data storage device. Since S3 storage is limited in cost, an ephemeral disk is a good place for a page file. Mine has the same device name as in the question, /dev/xvdb , but you can configure it during instance startup.

How do I adjust the swap space on my ephemeral disk?

Swap space can be configured in a single file or on a device partition. A file can be represented by non-contiguous blocks on disk, while a partition is a predefined set of contiguous blocks. Since reading and reading the disc is faster, you do not need to constantly move the read heads over long distances, we get better performance using the partition. (That is why the question is above discounts using the file as a workaround.)

Linux comes with many disk partition management programs, including fdisk, sfdisk, parted, etc. We will use 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 installed the script in a file that runs automatically every time the instance starts, /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 installation information in /etc/fstab , which is a Ubuntu system file that tells you how to automatically process the various available devices. It also starts at startup.

 /dev/xvdb1 swap swap sw,nobootwait 0 0 /dev/xvdb2 /mnt2 ext4 defaults,nobootwait 0 0 

The nobootwait parameter should ensure that Ubuntu does not freeze when booting from "The drive for / dev / xvdb (1/2) is not ready or not. Keep waiting or Press S to skip the installation or M to manually restore."

Make sure you create a folder in /mnt2 or wherever you plan to install it using mkdir.

How to debug problems that I encountered?

You can run the rc.local script with sudo /etc/rc.local to see how it starts troubleshooting. sudo fdisk -l should show your new partitions after startup. If this looks ok, try installing devices using sudo mount /dev/xvdb1 . This will use the configuration stored in fstab. If this fails, try playing with your settings and adjust fstab accordingly.

+9
source

Perhaps you are 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 partition your ephemeral storage device. I would put something in these lines in /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 higher is the size in blocks (82 is the Linux swap partition type). It seems that for different cases the block sizes can be different (as well as device names). So, first experiment or calculate what is right for you at the output of sfdisk.
  • Usually mkfs.xfs takes seconds. mkfs.ext4 can take half an hour (1 TB). YMMV depending on the file system you select.
+3
source

Found a swappace daemon that takes care of creating and removing swapfiles on demand. It just needed a little tweaking to save the swap files to an ephemeral disk.

This seems to me the most elegant solution:

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

I set up swapspace first (I had to build one of the sources), but then decided to step back into a manual solution, since I prefer more memory management in a production environment.

I assume that the installation of 2 instances of block blocks is already configured in /etc/fstab as /.inst0 and /.inst1 .

Add something like this to /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 the EC2 instance repository (aka SSD, otherwise called "ephemeral", which is destroyed every time you stop the instance) and is rebooted.

Please keep in mind that it takes some time to create and / or enable swapfiles, so give it a little time after a reboot to make sure it worked. :)

+1
source

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

 /mnt/swap1 swap swap defaults 0 0 
0
source

Source: https://habr.com/ru/post/919800/


All Articles