Once you’ve created an EBS Volume in the AWS web interface and attached it to an EC2 instance, how do you actually use it on your virtual server?
Here’s how! The following commands assume you’re logged into your system as root. I’ve created a 13GB volume and attached it to my running instance.
Before we begin
Let’s get a quick overview of our file system before we get started:
# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde 7.9G 1.7G 5.9G 23% / tmpfs 296M 0 296M 0% /dev/shm
Looks like right now I’ve only got one 8GB partition available. Let’s take a look at what else may be available to mount:
# fdisk -l Disk /dev/xvde: 8589 MB, 8589934592 bytes 255 heads, 63 sectors/track, 1044 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/xvdj: 14.0 GB, 13958643712 bytes 255 heads, 63 sectors/track, 1697 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
The second block tells us that an unused partition is at our disposal. That’s good news – let’s see how we can make that available for storage.
Find out what your EBS Volume is called
Depending on your Linux distribution, what you see in Amazon isn’t what your file system sees. Amazon may have told you that you’ve attached a volume as /dev/sdf, but your kernel may give it a different name – as seen with fdisk command above. Here’s another way to see your partitions using lsblk:
# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvde 202:64 0 8G 0 disk / xvdj 202:144 0 13G 0 disk
We can see that xvde is mounted on root (or /) and xvdj is not – that’s our new EBS volume. Let’s attach it so we can use it.
Format the EBS Volume
Before we do, we may have to format the partition. If you’ve used this EBS Volume before and it already contains data you don’t want to do this as it will – obviously – erase all your data. In our case however it’s a fresh volume so it needs formatting. This will take a moment, depending on the size of your volume:
# mkfs -t ext3 /dev/xvdj Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 851968 inodes, 3407872 blocks 170393 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=3489660928 104 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, 2654208 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Nice! The above command formats our volume as ext3 filesystem and it’s now ready for use.
Mounting your EBS Volume
Let’s create a directory so that Linux can speak to the new partition. I’ll call mine “/storage” and mount the partition to it:
# mkdir /storage # mount /dev/xvdj /storage
No news means good news. Let’s check what our file system looks like now:
# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde 7.9G 1.7G 5.9G 23% / tmpfs 296M 0 296M 0% /dev/shm /dev/xvdj 13G 161M 12G 2% /storage
Smashing! 12GB of usable storage has been attached to our EC2 instance, ready to be populated with tons of files.
EBS Volumes are persistent, which means that whatever is saved on it will remain intact, even if you terminate your instance. In fact, you can detach your volume and attach it to another running instance and start using your data. Here’s how:
Unmounting and reusing your EBS Volume
Rather than “force detaching” the volume in the web interface, let’s ask our instance to unmount it first. This is the equivalent of saying to Windows “Safely Eject my USB stick”:
# umount /storage # df -h Filesystem Size Used Avail Use% Mounted on /dev/xvde 7.9G 1.7G 5.9G 23% / tmpfs 296M 0 296M 0% /dev/shm
Make sure you’re not cd’d into your directory or you’ll get an error message that it’s “in use”. I’m also checking that I no longer have /storage in my file system.
You can now attach this volume to another instance, mount it again and see all your data on another server.
Is this cool or what?