Adding swap space to an attached volume via mkswap and swapon
To attach a volume to an existing AWS instance, please visit AWS : Attaching Amazon EBS volume to an instance.
We'll add a 2GB swap space to an instance.
Before we create a swap space, our system looks like this:
ubuntu@ip-172-31-25-188:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 7.8G 6.9G 513M 94% / none 4.0K 0 4.0K 0% /sys/fs/cgroup udev 240M 12K 240M 1% /dev tmpfs 49M 352K 49M 1% /run none 5.0M 0 5.0M 0% /run/lock none 245M 0 245M 0% /run/shm none 100M 0 100M 0% /run/user
Memory:
ubuntu@ip-172-31-25-188:~$ free total used free shared buffers cached Mem: 500212 485204 15008 12540 18276 151168 -/+ buffers/cache: 315760 184452 Swap: 0 0 0
After we attached an Amazon EBS volume (/dev/xvdf) to our instance, it is exposed as a block device:
ubuntu@ip-172-31-25-188:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk |-xvda1 202:1 0 8G 0 part / xvdf 202:80 0 2G 0 disk
Let's make the volume as a swap space using mkswap command:
ubuntu@ip-172-31-25-188:~$ sudo mkswap -f /dev/xvdf Setting up swapspace version 1, size = 2097148 KiB no label, UUID=ecc29d61-f428-4f6c-9217-27d846a86b5e
Now, turn the swap on using swapon command:
ubuntu@ip-172-31-25-188:~$ sudo swapon /dev/xvdf ubuntu@ip-172-31-25-188:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk |-xvda1 202:1 0 8G 0 part / xvdf 202:80 0 2G 0 disk [SWAP]
We may get "busy device" error while we do swapon:
ubuntu@ip-172-31-25-188:~$ sudo swapon /dev/xvdf swapon: /dev/xvdf: swapon failed: Device or resource busy
In that case, we can turn it off because somehow it's been on:
ubuntu@ip-172-31-25-188:~$ sudo swapoff /dev/xvdf ubuntu@ip-172-31-25-188:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk -xvda1 202:1 0 8G 0 part / xvdf 202:80 0 2G 0 disk
Then, turn the swap back on:
ubuntu@ip-172-31-25-188:~$ sudo swapon /dev/xvdf ubuntu@ip-172-31-25-188:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk |-xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk [SWAP]
We can check to see if our swap file is being used with free. Here is the output from free on my AWS instance:
ubuntu@ip-172-31-25-188:~$ free total used free shared buffers cached Mem: 500212 485204 15008 12540 18276 151168 -/+ buffers/cache: 315760 184452 Swap: 2097148 0 2097148
To make the space available automatically when our system rebooted, we need to add the following line to /etc/fstab file:
/dev/xvdf swap swap defaults 0 0
Unlike we've done in previous sections (swap on a attached volume), in this section, we'll create a swap file on an existing partition.
We can check if we already have some swap space available using swapon with the -s flag. It will display a summary of swap usage and availability on our storage device:
$ swapon -s Filename Type Size Used Priority /dev/xvdb partition 262140 262140 -1
Another way of checking for swap space is with the free utility, which shows us the system's overall memory usage. We can see our current memory and swap usage (in megabytes):
$ free -m total used free shared buff/cache available Mem: 987 838 10 59 138 48 Swap: 255 255 0
Now we want to check our current drive usage:
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 24G 14G 9.3G 60% / devtmpfs 492M 0 492M 0% /dev tmpfs 494M 0 494M 0% /dev/shm tmpfs 494M 58M 437M 12% /run tmpfs 494M 0 494M 0% /sys/fs/cgroup tmpfs 99M 0 99M 0% /run/user/0 tmpfs 99M 0 99M 0% /run/user/1000
As we can see on the first line, our storage partition has 9.3G available, so we have a bit of space to work with.
Although there are many opinions about the appropriate size of a swap space, in general, an amount equal to or double the amount of memory on our system is a good starting point.
Let's a swap file within our filesystem. We will create a file called swapfile in our root (/) directory.
First, we must allocate the amount of space that we want for our swap file using fallocate command which creates a file of a preallocated size (2G):
$ sudo fallocate -l 2G /swapfile
After entering password to authorize sudo privileges, the swap file will be created. We can verify that the correct amount of space was reserved for swap by using ls:
$ ls -lh /swapfile -rw-r--r-- 1 root root 2.0G Aug 1 18:37 /swapfile
Though our file is created, our system does not know that this is supposed to be used for swap. We need to tell our system to format this file as swap and then enable it.
We need to adjust the permissions on our swap file so that it isn't readable by anyone besides the root account. We can lock down the permissions with chmod:
$ sudo chmod 600 /swapfile $ ls -lh /swapfile -rw------- 1 root root 2.0G Aug 1 18:37 /swapfile
Let's tell our system to set up the swap space for use:
$ sudo mkswap /swapfile Setting up swapspace version 1, size = 2097148 KiB no label, UUID=0e257f17-8201-40fd-ae17-42245237810b
Now, our swap file is ready to be used as a swap space. We can start using it by typing the following command:
$ sudo swapon /swapfile
To verify that the procedure was successful, we can check whether our system reports swap space now:
$ swapon -s Filename Type Size Used Priority /dev/xvdb partition 262140 262140 -1 /swapfile file 2097148 9188 -2
The output assures us that we have a new swap file. We can use the free command again to make it sure:
$ free -m total used free shared buff/cache available Mem: 987 820 9 59 157 65 Swap: 2303 277 2026
Our swap has been set up successfully, and our operating system will begin to use it as needed.
Our swap file is enabled at the moment, but when we reboot, the server will not automatically enable the file for use. We can change that by modifying the fstab file, which is a table that manages filesystems and partitions.
Edit /etc/fstab file with sudo privileges, and add the following line to tell the operating system to automatically use the swap file that we just created:
# /etc/fstab # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/xvda / ext3 noatime,errors=remount-ro 0 1 /dev/xvdb none swap sw 0 0 /proc /proc proc defaults 0 0 tmpfs /dev/shm tmpfs nodev,nosuid,noexec,mode=1777 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 /swapfile swap swap sw 0 0
The server will check this file on each bootup, so the swap file will be ready for use from now on.
There are several command that checks swap space:
$ cat /proc/meminfo ... SwapTotal: 2359288 kB SwapFree: 2060708 kB ... $ cat /proc/swaps Filename Type Size Used Priority /dev/xvdb partition 262140 262140 -1 /swapfile file 2097148 36440 -2 $ swapon -s Filename Type Size Used Priority /dev/xvdb partition 262140 262140 -1 /swapfile file 2097148 36440 -2
Note that we can make another swapfile such as swapfile2:
$ sudo fallocate -l 2G /swapfile2 $ sudo chmod 600 /swapfile2 $ sudo mkswap /swapfile2 $ sudo swapon /swapfile2 $ echo "/swapfile2 swap swap sw 0 0" | sudo tee -a /etc/fstab
$ swapon -s Filename Type Size Used Priority /swapfile file 2097148 1951224 -2 /dev/xvdb partition 262140 262140 -1 /swapfile2 file 2097148 4 -3
AWS (Amazon Web Services)
- AWS : EKS (Elastic Container Service for Kubernetes)
- AWS : Creating a snapshot (cloning an image)
- AWS : Attaching Amazon EBS volume to an instance
- AWS : Adding swap space to an attached volume via mkswap and swapon
- AWS : Creating an EC2 instance and attaching Amazon EBS volume to the instance using Python boto module with User data
- AWS : Creating an instance to a new region by copying an AMI
- AWS : S3 (Simple Storage Service) 1
- AWS : S3 (Simple Storage Service) 2 - Creating and Deleting a Bucket
- AWS : S3 (Simple Storage Service) 3 - Bucket Versioning
- AWS : S3 (Simple Storage Service) 4 - Uploading a large file
- AWS : S3 (Simple Storage Service) 5 - Uploading folders/files recursively
- AWS : S3 (Simple Storage Service) 6 - Bucket Policy for File/Folder View/Download
- AWS : S3 (Simple Storage Service) 7 - How to Copy or Move Objects from one region to another
- AWS : S3 (Simple Storage Service) 8 - Archiving S3 Data to Glacier
- AWS : Creating a CloudFront distribution with an Amazon S3 origin
- AWS : Creating VPC with CloudFormation
- AWS : WAF (Web Application Firewall) with preconfigured CloudFormation template and Web ACL for CloudFront distribution
- AWS : CloudWatch & Logs with Lambda Function / S3
- AWS : Lambda Serverless Computing with EC2, CloudWatch Alarm, SNS
- AWS : Lambda and SNS - cross account
- AWS : CLI (Command Line Interface)
- AWS : CLI (ECS with ALB & autoscaling)
- AWS : ECS with cloudformation and json task definition
- AWS Application Load Balancer (ALB) and ECS with Flask app
- AWS : Load Balancing with HAProxy (High Availability Proxy)
- AWS : VirtualBox on EC2
- AWS : NTP setup on EC2
- AWS: jq with AWS
- AWS & OpenSSL : Creating / Installing a Server SSL Certificate
- AWS : OpenVPN Access Server 2 Install
- AWS : VPC (Virtual Private Cloud) 1 - netmask, subnets, default gateway, and CIDR
- AWS : VPC (Virtual Private Cloud) 2 - VPC Wizard
- AWS : VPC (Virtual Private Cloud) 3 - VPC Wizard with NAT
- DevOps / Sys Admin Q & A (VI) - AWS VPC setup (public/private subnets with NAT)
- AWS - OpenVPN Protocols : PPTP, L2TP/IPsec, and OpenVPN
- AWS : Autoscaling group (ASG)
- AWS : Setting up Autoscaling Alarms and Notifications via CLI and Cloudformation
- AWS : Adding a SSH User Account on Linux Instance
- AWS : Windows Servers - Remote Desktop Connections using RDP
- AWS : Scheduled stopping and starting an instance - python & cron
- AWS : Detecting stopped instance and sending an alert email using Mandrill smtp
- AWS : Elastic Beanstalk with NodeJS
- AWS : Elastic Beanstalk Inplace/Rolling Blue/Green Deploy
- AWS : Identity and Access Management (IAM) Roles for Amazon EC2
- AWS : Identity and Access Management (IAM) Policies, sts AssumeRole, and delegate access across AWS accounts
- AWS : Identity and Access Management (IAM) sts assume role via aws cli2
- AWS : Creating IAM Roles and associating them with EC2 Instances in CloudFormation
- AWS Identity and Access Management (IAM) Roles, SSO(Single Sign On), SAML(Security Assertion Markup Language), IdP(identity provider), STS(Security Token Service), and ADFS(Active Directory Federation Services)
- AWS : Amazon Route 53
- AWS : Amazon Route 53 - DNS (Domain Name Server) setup
- AWS : Amazon Route 53 - subdomain setup and virtual host on Nginx
- AWS Amazon Route 53 : Private Hosted Zone
- AWS : SNS (Simple Notification Service) example with ELB and CloudWatch
- AWS : Lambda with AWS CloudTrail
- AWS : SQS (Simple Queue Service) with NodeJS and AWS SDK
- AWS : Redshift data warehouse
- AWS : CloudFormation
- AWS : CloudFormation Bootstrap UserData/Metadata
- AWS : CloudFormation - Creating an ASG with rolling update
- AWS : Cloudformation Cross-stack reference
- AWS : OpsWorks
- AWS : Network Load Balancer (NLB) with Autoscaling group (ASG)
- AWS CodeDeploy : Deploy an Application from GitHub
- AWS EC2 Container Service (ECS)
- AWS EC2 Container Service (ECS) II
- AWS Hello World Lambda Function
- AWS Lambda Function Q & A
- AWS Node.js Lambda Function & API Gateway
- AWS API Gateway endpoint invoking Lambda function
- AWS API Gateway invoking Lambda function with Terraform
- AWS API Gateway invoking Lambda function with Terraform - Lambda Container
- Amazon Kinesis Streams
- AWS: Kinesis Data Firehose with Lambda and ElasticSearch
- Amazon DynamoDB
- Amazon DynamoDB with Lambda and CloudWatch
- Loading DynamoDB stream to AWS Elasticsearch service with Lambda
- Amazon ML (Machine Learning)
- Simple Systems Manager (SSM)
- AWS : RDS Connecting to a DB Instance Running the SQL Server Database Engine
- AWS : RDS Importing and Exporting SQL Server Data
- AWS : RDS PostgreSQL & pgAdmin III
- AWS : RDS PostgreSQL 2 - Creating/Deleting a Table
- AWS : MySQL Replication : Master-slave
- AWS : MySQL backup & restore
- AWS RDS : Cross-Region Read Replicas for MySQL and Snapshots for PostgreSQL
- AWS : Restoring Postgres on EC2 instance from S3 backup
- AWS : Q & A
- AWS : Security
- AWS : Security groups vs. network ACLs
- AWS : Scaling-Up
- AWS : Networking
- AWS : Single Sign-on (SSO) with Okta
- AWS : JIT (Just-in-Time) with Okta
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization