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

后端 未结 5 1902
迷失自我
迷失自我 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:47

    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. :)

提交回复
热议问题