Skip to content

Adding volume

Judit Lantos edited this page Aug 17, 2016 · 2 revisions

#Adding Volume to AWS EC2 Instances

Run the following command on your instance to view its mounted volumes

node:~$ df -h

Depending on your instance type, your output should look something like this.

The /dev/xvda1 volume is the root device volume. It contains the image used to boot the instance. If you need additional storage for your data, a simple solution is to add Amazon EBS volumes to your instance. An Amazon EBS volume serves as network-attached storage for your instance.

###Create and attach an Amazon EBS volume

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation bar, select the region in which you created your instance, and then click Instances in the navigation pane. The console displays the list of current instances in that region.

  1. Select your Linux instance. In the Description tab in the bottom pane note the Availability Zone for the instance.

  1. Now, click Volumes in the navigation pane. It is important to have the EBS volume in the same region as your instance.

  1. Click Create Volume.

  1. Configure the following, and then click Create:
  • Select the Magnetic volume type.
  • Enter the size of the volume you want to create.
  • Select the same Availability Zone that you used when you created your instance. Otherwise, you can't attach the volume to your instance.
  1. In the navigation pane, under Elastic Block Store, click Volumes. Notice that your newly created volume appears there and the state of the volume is available, so it's ready to be attached to an instance.

  2. Right-click the newly created volume and select Attach Volume.

  3. In the Attach Volume dialog box, configure the following, and then click Attach: Start typing in the name or ID of your instance, then select it from the list of suggested options. Specify an unused device name for that instance. We'll use /dev/sdf in this tutorial. If you select a different device name, be sure to note it as you'll need this information in the next procedure.

You'll notice that in the Details pane for your volume, the state of the volume is in-use, and the volume is attached to your instance with the device name /dev/sdf. However, if you return to your instance and run the df -h command again, you won't see the volume yet. That's because we need to mount the volume for df -h to see it. The lsblk command, however, can see all block devices attached to the instance.

Note: Some Linux distributions do not provide the lsblk command by default. If the lsblk command does not work, you can use sudo fdisk -l | grep Disk instead.

In the above example, lsblk reports that there are two block devices attached to the instance; xvda1 is mounted as the root file system (note the MOUNTPOINT value of /) and xvdf is not mounted at all.

###To make a volume available

  1. Identify the device to mount. In the previous procedure, the new volume was attached to /dev/sdf. Depending on the block device drivers on your instance's operating system, the device may appear at a different location (such as /dev/xvdf in the previous example) than what you specified in the console (/dev/sdf); in some cases, even the trailing letter may change (for example, /dev/xvdj).

  2. In the above example, the xvdf device is not mounted. Sometimes when you create a volume from a snapshot, the data on the volume is contained in a partition (such as /dev/xvdf1) instead of the root of the volume. In such a case, you would mount the /dev/xvdf1 partition (the lsblk command output omits the /dev/ portion of the file path). In this example, there is an empty volume with no partition, so you will mount /dev/xvdf.

  3. Because you created an empty volume instead of restoring a volume from a snapshot in the previous procedure, you need to format the volume using mkfs before you can mount it. Use the following command to create an ext4 file system on the volume. Substitute the device name you used if you did not use /dev/xvdf when you attached the volume.

Caution: This step assumes that you're mounting an empty volume. If you're mounting a volume that already has data on it (for example, a volume that was restored from a snapshot), don't use mkfs before mounting the volume (skip to the next step instead). Otherwise, you'll format the volume and delete the existing data.

  node:~$ sudo mkfs -t ext4 /dev/xvdf
  

To mount the device as /mnt/my-data, run the following commands.

  node:~$ sudo mkdir /mnt/my-data
  node:~$ sudo mount /dev/xvdf /mnt/my-data
  
  1. Be sure to specify the device name you identified in Step 1; otherwise, you might receive the following error when you run this mount command: "mount: you must specify the filesystem type". If you see this error, repeat Step 1 and use the correct device path (remember to add the /dev/ to the device name you get from the lsblk command).

  2. Now when you run the df -h command, you'll see output like the following.

  1. To view the contents of the new volume, run the following command.
  node:~$ ls /mnt/my-data
  
Clone this wiki locally