How can I create a swap partition on Amazon EC2 with ephemeral storage?

7,582

Solution 1

/dev/xvdb is indeed mounted, you need to check to see if anything is stored on there that you want to keep, although keeping important stuff on an ephemeral drive is a REALLY bad idea.

You will need to unmount /dev/xvdb before you do anything with it.

While you can

mkswap /dev/xvdb 

it will make a swap space of the whole ephemeral drive, which you almost certainly don't need. Also, if you partition your swap, you can use the rest of the ephemeral drive for things like the tmp folder, or storing sessions (if your host is a webserver). Ephemeral drives are very quick, but sadly not very persisent.

Anyway, back to swap partitions!

Better to either sfdisk as Abhishek mentions, or manually create a swap partition using fdisk:

fdisk /dev/xvdb
Press N to create a new partition
P for primary
1 for the first partition
Press Enter to accept the first location
Enter +xG where x is the size of the swapspace you want. I typically use twice the amount of RAM, but this is not a hard and fast rule
Enter T to change the type
Enter 82 for Linux Swap
Enter W to write the changes
Enter q to quit

You can now create your swap space with

mkswap /dev/xvdb1

And then enable it with

swapon /dev/xvdb1

One word of warning however, and I apologise If Im "Teaching granny to suck eggs" But as the name implies, an Ephemeral drive is... well, Ephemeral. If you ever shutdown your instance, you will have to recreate your swap partition and enable it. For this reason, dont add your newly created swap space to your fstab.

Rebooting should be fine however.

Solution 2

I have created a script that may be helpful for creating swap on ephemeral devices. It uses lvm to create the swap volume and also creates a volume that might be useful as /tmp. You could use cloud-init to trigger it.

bootcmd:
 - [ cloud-init-per, once, mk-eph, /usr/local/sbin/mk-eph.sh]

# Filesystem setup
fs_setup:
 - label: 'tmp'
   filesystem: 'xfs'
   device: '/dev/ephemeral/tmp'
   partition: 'auto'

mounts:
 - [ /dev/ephemeral/tmp, /tmp, auto, "defaults,nobootwait" ]
 - [ ephemeral0, null ]

runcmd:
 - [ chmod, 1777, /tmp ]

Solution 3

First unmount your epermal storage and remount like below

  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
Share:
7,582

Related videos on Youtube

Pandora
Author by

Pandora

Updated on September 18, 2022

Comments

  • Pandora
    Pandora over 1 year

    This is the output of df -k:

    Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/xvda1            10317860   7059008   2734732  73% /
    none                    847584         0    847584   0% /dev/shm
    /dev/xvdb            153899044    192068 145889352   1% /mnt/ephemeral
    

    I am using the Centos EBS boot image.

    I have read various questions regarding this but they tell to mount the new drive. But I think that drive is already mounted at /dev/xvdb. Am I correct?

    Can I just use this:

    mkswap -f /dev/xvdb
    #add in /etc/fstab
    /dev/xvdb       swap    swap    defaults        0       0
    swapon /dev/xvdb
    

    Will it work?

  • Pandora
    Pandora almost 11 years
    how do i enter the lines like ,1024. ,;;. i mean do i need to enter line by line of copy all and then paste. Also how much are you making the swap drive. can i make it 10GB
  • Pandora
    Pandora almost 11 years
    I have put that code of creating swap in rc.local. is that ok. so that swap automatically gets created everytime it starts
  • GeoSword
    GeoSword almost 11 years
    I wouldnt. If the host reboots, the swap space will still be there, so there's no need to recreate it. If the host terminates (shuts down) and is then restarted, then the ephemeral drive you had before will no longer be available, so either way, you will have to create the swap space from scratch.
  • Brian C
    Brian C over 9 years
    The advantage of recreating every time is that you have a stable environment that doesn't require manual intervention to reboot. Just a thought; not sure if it takes a long time to recreate swap or not (and if it does, perhaps a smaller partition could be used).
  • Mark Stosberg
    Mark Stosberg over 9 years
    What this code does should be documented. It makes a swap and XFS partition of particular sizes, and it's not not clear how these commands would persist through reboots.