AWS : Attaching Amazon EBS volume to an instance
Each Amazon EC2 instance that we launch has an associated root device volume, either an Amazon Elastic Block Store (Amazon EBS) volume or an instance store volume. We can use block device mapping to specify additional Amazon EBS volumes or instance store volumes to attach to an instance when it's launched. We can also attach additional Amazon EBS volumes to a running instance.
We can attach our Amazon EBS volume to one of our instances in the same Availability Zone.
To attach an Amazon EBS volume to an instance using the console:
- Open the Amazon EC2 console.
- Click Volumes in the navigation pane.
- Select a volume and then click Attach Volume.
- In the Attach Volume dialog box, start typing the name or ID of the instance to attach the volume to in the Instance box, and select it from the list of suggestion options (only instances in the same Availability Zone as the volume are displayed). Encrypted volumes can only be attached to instances that support Amazon EBS encryption.
- Verify that the suggested device name is suitable, or enter a device name in the Device box.
- Click Attach to attach the volume to the instance. The volume and instance must be in the same Availability Zone.
(Note) Volumes that have been mounted using the mount command are lost after restarting. To mount this Amazon EBS volume on every system reboot, add an entry for the device to the /etc/fstab file. See the next section.
After we attached an Amazon EBS volume to our instance, it is exposed as a block device. We can format the volume with any file system and then mount it. After we make the Amazon EBS volume available for use, we can access it in the same ways that we access any other volume. Any data written to this file system is written to the Amazon EBS volume and is transparent to applications using the device.
Use the following procedure to make an Amazon EBS volume available for use on Linux:
- Connect to our instance using SSH.
k@laptop:~/.ssh$ ssh bogo-ami The authenticity of host 'ec2-54-172-197-164.compute-1.amazonaws.com (126.96.36.199)' can't be established. ECDSA key fingerprint is 96:07:4e:2d:95:82:70:ec:2f:8c:20:83:86:a7:b0:0b. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'ec2-54-172-197-164.compute-1.amazonaws.com,188.8.131.52' (ECDSA) to the list of known hosts. Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64) ... ubuntu@ip-172-31-52-150:~$
- Depending on the block device driver of the kernel, the device might be attached with a different name than what we specify. For example, if we specify a device name of /dev/sdh, our device might be renamed /dev/xvdh or /dev/hdh by the kernel; in most cases, the trailing letter remains the same. In some versions of Red Hat Enterprise Linux (and its variants, such as CentOS), even the trailing letter might also change (where /dev/sda could become /dev/xvde). In these cases, each device name trailing letter is incremented the same number of times. For example, /dev/sdb would become /dev/xvdf and /dev/sdc would become /dev/xvdg. Amazon Linux AMIs create a symbolic link from the renamed device path to the name we specify, but other AMIs might behave differently.
Use the lsblk command to view our available disk devices and their mount points (if applicable) to help us determine the correct device name to use.
ubuntu@ip-172-31-52-150:~$ 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 8G 0 diskThe output of lsblk removes the /dev/ prefix from full device paths. In this example, /dev/xvda1 is mounted as the root device (note the MOUNTPOINT is listed as /, the root of the Linux file system hierarchy), and /dev/xvdf is attached, but it has not been mounted yet.
- Determine whether we need to create a file system on the volume. New volumes are raw block devices, and we need to create a file system on them before we can mount and use them. Volumes that have been restored from snapshots likely have a file system on them already; if we create a new file system on top of an existing file system, the operation overwrites our data. Use the sudo file -s device command to list special information, such as file system type.
ubuntu@ip-172-31-52-150:~$ 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 8G 0 disk ubuntu@ip-172-31-52-150:~$ sudo file -s /dev/xvda1 /dev/xvda1: Linux rev 1.0 ext4 filesystem data, UUID=a09befc1-cf11-45ac-b1f4-52736ff06125, volume name "cloudimg-rootfs" (needs journal recovery) (extents) (large files) (huge files) ubuntu@ip-172-31-52-150:~$ sudo file -s xvdf xvdf: ERROR: cannot open `xvdf' (No such file or directory)The device contains Linux rev 1.0 ext4 filesystem data, so this volume does not need a file system created. We can skip Step 4 if your output shows file system data.
- (Conditional) Use the following command to create an ext4 file system on the volume. Substitute the device name (such as /dev/xvdf) for device_name. Depending on the requirements of our application or the limitations of our operating system, we can choose a different file system type, such as ext3 or XFS.
This step assumes that we're mounting an empty volume. If we'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, we'll format the volume and delete the existing data.
ubuntu@ip-172-31-52-150:~$ sudo mkfs -t ext4 /dev/xvdf mke2fs 1.42.9 (4-Feb-2014) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 524288 inodes, 2097152 blocks 104857 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2147483648 64 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
- Use the following command to create a mount point directory for the volume. The mount point is where the volume is located in the file system tree and where we read and write files to after we mount the volume. Substitute a location for mount_point, such as /vol.
- Use the following command to mount the volume at the location we just created. This will mount the filesystem on /dev/xvdf into the folder /vol. That means that going into /vol will show us the filesystem which is on /dev/xvdf.
- We can check if xvdf is mounted properly:
- (Optional I) To mount this Amazon EBS volume on every system reboot, add an entry for the device to the /etc/fstab file.
- Create a backup of our /etc/fstab file that we can use if we accidentally destroy or delete this file while we are editing it.
- Open the /etc/fstab file and add a new line to the end of the file for our volume using the following format:
device_name mount_point file_system_type fs_mntops fs_freq fs_passnoThe last three fields on this line are the file system mount options, the dump frequency of the file system, and the order of file system checks done at boot time. If we don't know what these values should be, then use the values in the example below for them (defaults, nofail 0 2). For more information on /etc/fstab entries, see the fstab manual page (by entering man fstab on the command line). For example, to mount the ext4 file system on the device /dev/xvdf at the mount point /vol, add the following entry to /etc/fstab.
If we ever intend to boot our instance without this volume attached (for example, so this volume could move back and forth between different instances), we should add the nofail mount option that allows the instance to boot even if there are errors in mounting the volume. Debian derivatives, such as Ubuntu, must also add the nobootwait mount option.
/dev/xvdf /vol ext4 defaults,nofail 0 2
- After we've added the new entry to /etc/fstab, we need to check that our entry works. Run the sudo mount -a command to mount all file systems in /etc/fstab.
- Let's check out dish using df -h command:
ubuntu@ip-172-31-52-150:~$ sudo cp /etc/fstab /etc/fstab.orig
ubuntu@ip-172-31-52-150:~$ sudo mount -a
ubuntu@ip-172-31-52-150:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/xvda1 50G 1.4G 46G 3% / udev 1.9G 12K 1.9G 1% /dev tmpfs 377M 240K 377M 1% /run none 5.0M 0 5.0M 0% /run/lock none 1.9G 0 1.9G 0% /run/shm /dev/xvdb 4.0G 73M 3.7G 2% /mnt /dev/xvdf 50G 180M 47G 1% /vol
- (Optional II) We can run the following script with root privilege (To run a script for the steps #4-8):
#!/bin/bash mkfs.ext4 /dev/xvdf mkdir /vol echo "/dev/xvdf /vol auto noatime 0 0" | sudo tee -a /etc/fstab
ubuntu@ip-172-31-52-150:~$ sudo mkdir /vol
ubuntu@ip-172-31-52-150:~$ sudo mount /dev/xvdf /vol
ubuntu@ip-172-31-52-150:~$ 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 8G 0 disk /vol
AWS (Amazon Web Services)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization