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

后端 未结 5 1897
迷失自我
迷失自我 2021-02-04 19:08

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         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 19:56

    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.

提交回复
热议问题