Ravindra BagaleCourses & study guides मराठी Track your progress

How to Add, Format and Mount an EBS Volume on EC2 (fstab)

Sooner or later a server's disk fills up: uploads, logs, a growing database. On AWS the disk is an EBS volume, and you have two answers: make the volume bigger, or add a second volume just for data. This chapter does the second one from start to finish on Amazon Linux 2023: it explains what an EC2 server is made of and which EBS volume type to pick, then creates a volume in the right Availability Zone, attaches it, checks it with lsblk, df -h and file -s, makes an XFS file system, mounts it on /data, proves the files really live on the volume, makes the mount permanent in /etc/fstab, moves the volume with its data to another server, and cleans up.

What you'll learn in this class

  • What an EC2 server is made of: CPU and RAM, an EBS disk and an ENI
  • Growing a volume vs adding a new one, and why data gets its own volume
  • EBS volume types (gp3, gp2, io2, io1, st1, sc1) and what IOPS means
  • Creating a volume in the same Availability Zone and attaching it as /dev/sdf
  • lsblk vs df -h, and file -s showing data for an empty volume
  • Why deleted data can be recovered, and why a new EBS volume is always empty
  • mkfs -t xfs on a new volume only, and mounting it on /data
  • Proving with umount that the files live on the volume
  • A permanent mount in /etc/fstab with the UUID, defaults,nofail 0 2, tested with sudo mount -a
  • Moving a data volume to another server without losing data, and the clean-up

1. What an EC2 server is made of

Why: When something runs out (CPU, memory, disk or IP), knowing which part of the server holds it tells you what to change.

What: when you launch an instance, AWS puts together three separate parts:

  1. The instance itself: CPU and RAM. This is the computer that runs your programs.
  2. An EBS volume: the disk. The first one is the root volume; the operating system boots from it. Amazon Linux 2023 gets an 8 GiB root volume by default.
  3. An ENI (Elastic Network Interface): the network card. It holds the private IP address, and the public IP if there is one.

Classroom line

What is EC2 by itself? RAM and CPU.

Classroom line

The first EBS volume is called the root volume.

One EC2 server, three partsAvailability Zone us-east-1dInstanceCPU + RAM: runs your programsEBS root volumethe disk: OS, 8 GiBEBS data volumeoptional: /dataENInetwork card: private + public IP

Figure 1. One EC2 server is three parts: the instance with CPU and RAM, the EBS root volume that holds the OS (8 GiB on Amazon Linux 2023), and the ENI that holds the private and public IP. A data volume can be added next to the root volume.

Because the disk is separate from the CPU and RAM, it can be resized, detached and attached to another server. What happens to each part on stop and terminate is in The ENI: where your IP addresses live and The EBS bill: real numbers.

2. The disk is full: grow the volume or add one?

Why: A full disk stops uploads, logs and databases. Choosing between growing and adding decides how much work and risk the fix needs.

The situation: a website stores uploaded images, or MySQL keeps growing, and after some months the 8 GiB root volume is full. There are two ways out.

Classroom line

If I can grow my disk, why take a new one?

Option A: grow the volume. Select the volume, Actions > Modify volume, and enter a bigger size, for example 8 to 16 GiB.

  1. A size can only go up, never down.
  2. The server keeps running. Then grow the partition and the file system inside Linux:

    sudo growpart /dev/nvme0n1 1
    sudo xfs_growfs -d /
    df -h /
    
  3. After a change, the same volume can be modified again only after six hours.

Option B: add a data volume. Keep the operating system on the root volume, and put the data on a second volume mounted on a folder such as /data. This is what the rest of the chapter does. It has real advantages:

  1. The data is kept apart from the operating system. If the OS breaks, the data volume can simply be attached to a new server.
  2. Each volume can have its own type and size: a cheap gp3 root and a faster volume for a database.
  3. You can back up only the data, with a snapshot of that one volume.
The disk is full: two ways outOption A: grow the volume8 GiBfull16 GiBsame volume$ sudo growpart /dev/nvme0n1 1$ sudo xfs_growfs -d /Size only goes up. Next change after 6 hours.OS and data stay on one disk.Option B: add a data volumeroot, 8 GiBthe OS on /data volumemounted on /dataData kept apart from the OS.Its own type and size, its own snapshot.Moves to a new server if the OS breaks.this chapter

Figure 2. Option A: Modify volume makes the root volume bigger, then growpart and xfs_growfs grow the partition and the file system. Option B: a second EBS volume is attached and mounted on /data, so the data is kept apart from the OS and can move to another server.

3. EBS volume types and IOPS

Why: The type sets speed and price. The wrong type either makes a database slow or costs you money for speed you never use.

IOPS (input/output operations per second) is how many reads and writes a disk can do each second. Programs and their data are loaded from the disk into RAM, so a slow disk makes a slow server, especially for databases.

The types you see in Create volume > Volume type:

  1. General Purpose SSD gp3: the default and the cheapest SSD. Every gp3 volume gets 3,000 IOPS and 125 MiB/s whatever its size, and you can pay for more: up to 80,000 IOPS and 2,000 MiB/s. Size 1 GiB to 64 TiB.
  2. General Purpose SSD gp2: the older type. Its IOPS grow with its size: 3 IOPS per GiB, at least 100 and at most 16,000; volumes smaller than 1 TiB can burst to 3,000 IOPS for a while. Use gp3 for new volumes.
  3. Provisioned IOPS SSD io2 (Block Express): for big, busy databases. You choose the IOPS yourself: up to 256,000 IOPS (at most 1,000 per GiB) and 4,000 MiB/s, size up to 64 TiB.
  4. Provisioned IOPS SSD io1: the older provisioned type, up to 64,000 IOPS.
  5. Throughput Optimized HDD st1 and Cold HDD sc1: hard disks for large files read in long runs, such as logs or archives, at a low price per GiB. They start at 125 GiB and can't be a root volume.
  6. Magnetic (standard): a previous-generation type; don't use it for new volumes.
EC2 console, Create volume page: "Create an Amazon EBS volume to attach to any EC2 instance in the same Availability Zone." Under Volume settings, the Volume type list is open with General Purpose SSD (gp3) ticked, then General Purpose SSD (gp2), Provisioned IOPS SSD (io1), Provisioned IOPS SSD (io2), Cold HDD (sc1), Throughput Optimized HDD (st1) and Magnetic (standard).

Console: The Volume type list in Create volume: gp3, gp2, io1, io2, sc1, st1 and Magnetic (standard).

EBS volume types and IOPS IOPS = read and write operations per second. More IOPS, faster disk fordatabases. gp3 General Purpose SSD (default) 1 GiB - 64 TiB 3,000 IOPS + 125 MiB/s included; up to 80,000 IOPS gp2 General Purpose SSD (older) 1 GiB - 16 TiB 3 IOPS per GiB, 100 to 16,000; burst to 3,000 io2 Provisioned IOPS SSD (Block Express) up to 64 TiB you choose: up to 256,000 IOPS, 1,000 per GiB io1 Provisioned IOPS SSD (older) 4 GiB - 16 TiB you choose: up to 64,000 IOPS st1 / sc1 Throughput HDD / Cold HDD 125 GiB - 16 TiB big files read in long runs; can't be a root volume standard Magnetic   previous generation: don't use for new volumes

Figure 3. EBS volume types: gp3 with 3,000 IOPS included and up to 80,000; gp2 with 3 IOPS per GiB, 100 to 16,000; io2 Block Express up to 256,000 IOPS; io1 up to 64,000; st1 and sc1 hard disks from 125 GiB that can't boot; Magnetic is previous generation.

Sizes: a root volume can't be smaller than its AMI needs: 8 GiB for Amazon Linux, 30 GiB for Windows Server. A gp3 or gp2 data volume can be as small as 1 GiB, which is plenty for practice.

4. The plan: four steps

  1. Create a volume in the same Availability Zone as the server.
  2. Attach it to the server.
  3. Make a file system on it (only because it is new).
  4. Mount it on a folder, /data. Everything written to /data then lands on the new volume.

Classroom line

If your EC2 is in zone A and you create the volume in zone B, it won't attach.

Add a data volume in four steps Availability Zone us-east-1d 1. Create: 1 GiB gp3 in the server's AZ data-vol · 1 GiB gp3 · Available State Available. Another AZ: the server is not even listed. 2. Attach as /dev/sdf data-vol · /dev/sdf · In-use nvme1n1 no file system State In-use. lsblk inside Linux shows it as nvme1n1, with no mountpoint. 3. sudo mkfs -t xfs /dev/nvme1n1 nvme1n1 xfs Only because file -s said data: new and empty. 4. sudo mount /dev/nvme1n1 /data nvme1n1 xfs -> /data /data: a b c x y z Files written to /data now land on the volume. EC2 server (Amazon Linux 2023) nvme0n1p1 / (root, xfs)

Figure 4. Animation: create a 1 GiB gp3 volume in the server's Availability Zone; attach it as /dev/sdf, which Linux shows as nvme1n1; make an XFS file system with mkfs; mount it on /data, where new files land on the volume.

5. Look at the disks first: lsblk, df -h and du

Why: Looking first shows what is already there, so you never format the wrong disk.

How: connect to the server and run:

lsblk
df -h

lsblk lists every disk and partition attached to the server, mounted or not:

NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1       259:0    0   8G  0 disk
├─nvme0n1p1   259:1    0   8G  0 part /
├─nvme0n1p127 259:2    0   1M  0 part
└─nvme0n1p128 259:3    0  10M  0 part /boot/efi
  1. nvme0n1 is the root volume, the whole disk. nvme0n1p1 is its main partition, mounted on /. The two small partitions are used for booting.
  2. A partition is a part of a disk, so it can't be bigger than the disk.

df -h lists only the mounted file systems, with their size, used and free space (-h means human-readable sizes such as 8.0G):

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        4.0M     0  4.0M   0% /dev
tmpfs           457M     0  457M   0% /dev/shm
/dev/nvme0n1p1  8.0G  1.7G  6.4G  21% /
/dev/nvme0n1p128 10M  1.3M  8.7M  13% /boot/efi
  1. tmpfs and devtmpfs live in RAM, not on a disk.
  2. To see how much space a folder uses, use du: sudo du -sh /var/log. Without sudo you get Permission denied for folders that belong to root.
lsblk vs df -h lsblk: every attached disk and partition $ lsblk nvme0n1 8G disk ├─nvme0n1p1 8G part / ├─nvme0n1p127 1M part └─nvme0n1p128 10M part /boot/efi nvme1n1 1G disk The new volume is listed, with no mount point. df -h: only mounted file systems, with free space $ df -h devtmpfs 4.0M 0 4.0M 0% /dev tmpfs 457M 0 457M 0% /dev/shm /dev/nvme0n1p1 8.0G 1.7G 6.4G 21% / /dev/nvme0n1p128 10M 1.3M 8.7M 13% /boot/efi (nvme1n1: not here until mounted) tmpfs and devtmpfs live in RAM. The new volume appears after mount.

Figure 5. lsblk shows every attached disk and partition, mounted or not, so a new empty volume appears as nvme1n1 with no mount point. df -h shows only mounted file systems with their used and free space, so the new volume appears there only after it is mounted.

6. Create the volume in the same Availability Zone

Why: A volume can attach only to a server in its own AZ. A volume made in another AZ is useless to this server.

How:

  1. Find the server's AZ: select the instance, Networking tab, Availability Zone, for example us-east-1d.
  2. Elastic Block Store > Volumes > Create volume.
  3. Volume type: General Purpose SSD (gp3). Size: 1 GiB. Leave IOPS at 3000 and throughput at 125.
  4. Availability Zone: the server's AZ, us-east-1d. Check this twice; it is the most common mistake.
  5. Add a tag Name = data-vol, so you can find it later, and Create volume. Its state goes from Creating to Available.

Classroom line

Select the Availability Zone carefully: pick your EC2's AZ.

EC2 console, Create volume, Volume settings: Volume type General Purpose SSD (gp3); Size (GiB) 1, Min 1 GiB, Max 65536 GiB; IOPS 3000, Min 3000 IOPS, Max 80000 IOPS; Throughput (MiB/s) 125, Min 125 MiB, Max 2000 MiB, Baseline 125 MiB/s; Availability Zone use1-az6 (us-east-1a).

Console: Create volume: gp3, 1 GiB, 3000 IOPS and 125 MiB/s, which gp3 includes. The Availability Zone must be the server's AZ.

7. Attach the volume

Why: Until it is attached, the volume is just storage in AWS. Attaching is what makes Linux see it as a new disk.

How:

  1. Select data-vol, Actions > Attach volume.
  2. Instance: choose your server. Only instances in the volume's AZ are listed.
  3. Device name: /dev/sdf. The console recommends /dev/xvda for a root volume and /dev/sdf to /dev/sdp for data volumes.
  4. Attach volume. The state changes to In-use, and the instance's Storage tab shows two block devices.
EC2 console, Attach volume, Basic details: Volume ID (hidden), Availability Zone use1-az4 (us-east-1d), Instance (hidden, running) with the note "Only instances in the same Availability Zone as the selected volume are displayed.", Device name "Select a device name" with the note "Recommended device names for Linux: /dev/xvda for root volume. /dev/sd[f-p] for data volumes."

Console: Attach volume: the Instance list shows only instances in the volume's Availability Zone, and the device name for a data volume comes from /dev/sdf to /dev/sdp.

The name inside Linux is different. On current instance types (Nitro, such as t3) every EBS volume is an NVMe device, so /dev/sdf shows up as /dev/nvme1n1; on older types it can be /dev/xvdf. Always check with lsblk:

NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1       259:0    0   8G  0 disk
├─nvme0n1p1   259:1    0   8G  0 part /
├─nvme0n1p127 259:2    0   1M  0 part
└─nvme0n1p128 259:3    0  10M  0 part /boot/efi
nvme1n1       259:4    0   1G  0 disk

nvme1n1 is attached, but it has no partition and no mount point, and df -h doesn't show it yet.

Classroom line

Pay attention here.

8. Is there a file system? sudo file -s

Why: mkfs erases everything. Checking first that the volume is empty (data) is what keeps you from wiping a disk that already holds files.

How:

sudo file -s /dev/nvme0n1p1
sudo file -s /dev/nvme1n1
  1. The root partition answers SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs): it has an XFS file system.
  2. The new volume answers just /dev/nvme1n1: data. That means: no file system, only raw blocks.
  3. sudo lsblk -f shows the same thing as a table: the FSTYPE column is empty for the new volume.

What a file system is: a disk is only a long row of blocks of 0s and 1s. A file system keeps a table on the disk: each file's name, its folder, and which blocks hold its content. Without that table, Linux can't store a file by name, so a volume must have a file system before it can be mounted.

The file system table, delete and wipe 1. Files saved notes.txt -> blocks 1-3 photo.jpg -> blocks 4-6 table (file system) 2. rm notes.txt: only the table entry goes ✗ notes.txt -> blocks 1-3 photo.jpg -> blocks 4-6 content still there: recovery tools can find it Really erase a disk: shred, dd if=/dev/zero or the drive's secure erase 3. A new EBS volume: AWS wiped the storage before reuse $ sudo file -s /dev/nvme1n1 /dev/nvme1n1: data all zeros: no table, no old data

Figure 6. A file system keeps a table of names and block locations. A normal delete removes only the table entry; the content stays in the blocks until something overwrites it, which is how recovery tools work. AWS wipes the storage of a deleted EBS volume before it is reused, so a new volume reads as zeros: file -s says data.

Why deleted files can come back:

  1. A normal delete (rm, or emptying the Recycle Bin) removes the file's entry from the table and marks its blocks as free. The content stays in those blocks until new data overwrites them.
  2. That is why recovery tools can often find deleted content, though usually without the file names. On a test disk, a line written to a file, then deleted with rm, could still be found on the raw device afterwards.
  3. To really erase a disk, overwrite it, for example with shred or a full dd from /dev/zero, or use the drive's secure-erase command.
  4. AWS wipes the storage of a deleted EBS volume before anyone else gets it. That is why a brand-new volume is all zeros and file -s says data.

9. Make a file system: mkfs -t xfs

Why: A raw volume has no table of file names, so Linux cannot store files on it. mkfs creates that table.

What: making a file system, also called formatting, writes that empty table onto the volume. It is not installing a driver; it only prepares the disk to store files.

How:

sudo mkfs -t xfs /dev/nvme1n1
  1. mkfs prints a summary that starts with meta-data=/dev/nvme1n1 and lists the block size and sections. sudo file -s /dev/nvme1n1 now says SGI XFS filesystem data.
  2. XFS is the default file system on Amazon Linux 2023. Ubuntu usually uses ext4 (sudo mkfs -t ext4 /dev/nvme1n1). Windows uses NTFS.
  3. NFS is not a disk format: it is a protocol for sharing folders over the network. On AWS that is Amazon EFS.

Only on a new, empty volume. mkfs writes a new, empty table: every file on the volume is lost. If the volume already has a file system, mkfs.xfs refuses with appears to contain an existing filesystem (xfs) and asks for -f. That message means the volume has data: stop, and mount it instead.

Classroom line

We make a file system only when the volume is new.

10. Mount the volume on /data

Why: Linux reaches a disk only through a folder. Mounting connects the volume to /data, so files written there land on it.

What: mounting connects a folder to a file system. After the mount, whatever you write into the folder is stored on that volume.

Classroom line

Connecting this directory to this volume is called mounting.

How:

sudo mkdir /data
sudo mount /dev/nvme1n1 /data
df -h
  1. df -h now has a new line: /dev/nvme1n1 960M 40M 921M 5% /data. The 1 GiB volume shows a little less, because the file system itself takes some space.
  2. Create a few files and folders:

    cd /data
    sudo touch a b c
    sudo mkdir x y z
    ls
    
  3. /data belongs to root, so plain touch gives Permission denied. To let ec2-user write there without sudo, run sudo chown ec2-user:ec2-user /data once.

11. Prove that the files live on the volume

Why: Seeing the files disappear on unmount and come back on mount proves they are on the volume, not on the root disk.

The test: unmount the volume and look into the folder.

  1. Leave the folder first: cd ~. You can't unmount a folder you are standing in; umount answers target is busy.
  2. Unmount and look:

    sudo umount /data
    ls /data
    
  3. /data is empty. The files were never in the folder; they are on the volume.

  4. Mount it again, and they are back:
    sudo mount /dev/nvme1n1 /data
    ls /data
    

Classroom line

I can't cut the branch I'm standing on.

Classroom line

How will we verify these were really created on that volume?

12. Make the mount permanent: /etc/fstab

Why: mount is temporary. After a reboot, or a stop and start, /data is an empty folder again until someone mounts the volume.

Classroom line

What we mounted just now is a temporary mount.

What: /etc/fstab (file system table) is a configuration file that Linux reads at boot. Each line names a file system and the folder it is mounted on.

How:

  1. Get the volume's UUID, a unique ID stored in the file system:

    sudo blkid /dev/nvme1n1
    
  2. It prints /dev/nvme1n1: UUID="af9001ed-20fb-42b7-b0b7-f2d8de17df67" BLOCK_SIZE="512" TYPE="xfs". Copy the UUID without the quotes.

  3. Back up the file, then open it:

    sudo cp /etc/fstab /etc/fstab.bak
    sudo nano /etc/fstab
    
  4. Leave the existing lines alone, and add one line at the end with your own UUID:

    UUID=af9001ed-20fb-42b7-b0b7-f2d8de17df67  /data  xfs  defaults,nofail  0  2
    
  5. Test it before any reboot:

    sudo umount /data
    sudo mount -a
    df -h
    
  6. mount -a mounts everything in /etc/fstab that isn't mounted yet. If /data is back in df -h with no error, the line is right. If mount prints a hint to run systemctl daemon-reload, run sudo systemctl daemon-reload.

  7. Then sudo reboot, connect again, and check df -h: /data is mounted without you doing anything.

Classroom line

It should mount by itself when the server starts. That's called a permanent mount.

One line in /etc/fstab, six fields UUID=af9001ed-... /data xfs defaults,nofail 0 2 UUID=af9001ed-... which file system   from sudo blkid; never changes /data mount point   an existing folder xfs type   ext4 on Ubuntu defaults,nofail options   nofail: boot even if the volume is missing 0 dump   old backup tool: 0 2 fsck   root 1, others 2, 0 never $ sudo umount /data $ sudo mount -a # test before any reboot: no output = OK $ df -h # /data is back

Figure 7. The six fields of the fstab line: UUID of the file system, mount point /data, type xfs, options defaults,nofail, dump 0 (no backup by the dump tool) and fsck order 2 (check after the root, which is 1; 0 means never check).

Each field of the line:

  1. UUID=...: which file system. Device names such as /dev/nvme1n1 can change between boots or instance types; the UUID never does.
  2. /data: the mount point, an existing folder.
  3. xfs: the file system type.
  4. defaults,nofail: normal options, plus nofail: if the volume is missing, for example detached, the server still boots. Without nofail the boot can stop and wait, and you can't connect with SSH.
  5. 0: the dump field, for the old dump backup tool; always 0.
  6. 2: the order for the file system check at boot: 1 for the root, 2 for other disks, 0 for never.

A typing mistake shows up in the test: a misspelt option such as nofial makes mount -a fail with Unknown parameter 'nofial'. Fix it before you reboot.

Classroom line

Take a new volume and show me a permanent mount.

That is a common interview task, and it is exactly sections 6 to 12.

13. Move a data volume to another server

Why: the data lives on the volume, not in the server. If a server breaks or is replaced, the data volume moves to a new server with all its files.

How:

  1. On the old server: cd ~, then sudo umount /data, and delete or comment out (#) the /data line in /etc/fstab.
  2. In the console: select data-vol, Actions > Detach volume. Its state becomes Available.
  3. The new server must be in the same AZ. When you launch it, use Network settings > Edit > Subnet and pick the subnet in that AZ.
  4. Actions > Attach volume to the new server, device /dev/sdf.
  5. On the new server: lsblk shows nvme1n1, and sudo file -s /dev/nvme1n1 says SGI XFS filesystem data: the file system is already there. Don't run mkfs.
  6. Mount it and look:

    sudo mkdir /data
    sudo mount /dev/nvme1n1 /data
    ls /data
    
  7. a b c x y z are all there. Add the fstab line on this server too (section 12), with the same UUID.

Classroom line

Launch it in the same zone as your earlier EC2.

Move a data volume to another server 1. Old server: cd ~ and sudo umount /data old server: /data mounted → /data unmounted data-vol: In-use Also remove the /data line from /etc/fstab. 2. Console: Detach volume data-vol: Available State Available. Force detach only as the last resort: it can corruptdata. 3. Attach to a new server in the same AZ new server: data-vol In-use · /dev/sdf no mkfs! file -s says XFS: the file system is already there. No mkfs! /data: a b c x y z ls /data: a b c x y z. Add the fstab line on this server too.

Figure 8. Animation: unmount /data on the old server; detach the volume, which becomes Available; attach it to a new server in the same Availability Zone; mount it on /data without mkfs, and the files a b c x y z are there.

Unmount first, then detach. Detaching a mounted volume can lose data that was still being written. Force detach exists for a volume that is stuck in Detaching, but it can corrupt the file system, so use it only as the last resort.

The root volume can be moved too, by swapping it on a stopped server: see Recover data by swapping the root volume.

14. Clean up

Why: Volumes and snapshots keep costing money until you delete them, even with no server attached.

  1. sudo umount /data and remove the /data line from /etc/fstab.
  2. Volumes: select data-vol, Actions > Detach volume, wait for Available, then Actions > Delete volume. An Available volume is still billed.
  3. Instances: terminate the practice servers.

Classroom line

Don't keep it for nothing; delete it if you don't need it.

15. When something goes wrong

Why: Each error names one step: AZ, attach, file system, mount or fstab. Matching the error to the step saves you from starting over.

  1. The server isn't in the Attach volume list: the volume is in another AZ. Create the volume in the server's AZ.
  2. mount: /data: wrong fs type, bad option, bad superblock: the volume has no file system yet (file -s says data), or you named the wrong device. Check with lsblk and sudo file -s; run mkfs only if the volume is new.
  3. mount point /data does not exist: create the folder first with sudo mkdir /data. mkdir makes a folder; mkfs makes a file system. Don't mix them up.
  4. /dev/sdf: No such file or directory: inside Linux the volume is /dev/nvme1n1 (or /dev/xvdf). Use the name from lsblk.
  5. umount: /data: target is busy: you, or a program, are inside /data. Run cd ~ and try again.
  6. mkfs.xfs: ... appears to contain an existing filesystem: the volume has data. Don't add -f; mount it.
  7. touch: cannot touch ... Permission denied in /data: use sudo, or sudo chown ec2-user:ec2-user /data.
  8. /data is empty after a reboot: there is no fstab line, or it has a mistake. Run sudo mount -a and read the error.
  9. The server doesn't come back after an fstab change: a wrong line without nofail. Stop the server, attach its root volume to another server as a second disk, fix the line, and attach it back; see the root volume chapter linked above.

Ravindra Bagale's Tip

Before every mkfs, run sudo file -s on the device and read the answer. Only data means "new and empty". And after every /etc/fstab change, run sudo mount -a before you reboot. One missed letter in that file can lock you out of the server; nofail and the test together make it safe.

16. The whole class on one page

  1. An EC2 server is the instance (CPU and RAM), an EBS root volume (the disk) and an ENI (the network card with the IPs).
  2. A full disk: grow the volume (Modify volume, then growpart and xfs_growfs), or add a data volume and mount it on /data.
  3. IOPS = reads and writes per second. gp3 includes 3,000 IOPS at any size (up to 80,000); gp2 gives 3 IOPS per GiB; io2 up to 256,000; st1 and sc1 are hard disks that can't boot.
  4. Four steps: create in the same AZ, attach as /dev/sdf (Linux shows nvme1n1), mkfs -t xfs (new volumes only), mount.
  5. lsblk shows attached disks; df -h shows mounted file systems; sudo file -s says data when there is no file system.
  6. A delete removes the table entry, not the content; AWS wipes deleted volumes, so a new volume is empty.
  7. sudo mkdir /data, sudo mount /dev/nvme1n1 /data; after sudo umount /data the folder is empty: the files live on the volume.
  8. /etc/fstab: UUID=... /data xfs defaults,nofail 0 2, from sudo blkid; test with sudo umount /data and sudo mount -a before a reboot.
  9. Move a data volume: umount, detach, attach in the same AZ, mount; never mkfs. Force detach only as the last resort.
  10. Clean up: unmount, detach, delete the volume, terminate the servers.

17. Hands-on lab: add a 1 GiB data volume and mount it at boot

Why this lab. This lab adds a second disk to a running server, stores files on it, and makes it mount by itself after a reboot, checking each step before the next.

  1. Look at the disks
    • Command: lsblk
    • You should see: One disk, nvme0n1, with its partitions.
    • Check: df -h / shows the 8 GiB root file system.
    • Why: You need the "before" picture to spot the new disk later.
  2. Create the volume
    • Do: Volumes → Create volume: gp3, 1 GiB, and the same AZ as the server (see the server's Networking tab).
    • Check: The new volume is Available.
    • Why: 1 GiB gp3 is enough to practise and costs very little. Another AZ would make the attach impossible.
  3. Attach it
    • Do: Actions → Attach volume → your server → device /dev/sdf.
    • Check: lsblk now also shows nvme1n1, 1G, with no partitions.
    • Why: Linux names the disk nvme1n1, not sdf. Use the name lsblk shows.
  4. Check that it is empty
    • Command: sudo file -s /dev/nvme1n1
    • You should see: /dev/nvme1n1: data
    • Check: If it says XFS filesystem data instead, stop: the disk already holds a file system.
    • Why: data means no file system, so formatting it loses nothing.
  5. Make the file system
    • Command: sudo mkfs -t xfs /dev/nvme1n1
    • You should see: A summary with meta-data=/dev/nvme1n1.
    • Check: sudo file -s /dev/nvme1n1 now says SGI XFS filesystem data.
    • Why: XFS is the default on Amazon Linux. Run mkfs only on a new volume: it erases everything.
  6. Mount it on /data
    • Command: sudo mkdir /data && sudo mount /dev/nvme1n1 /data
    • You should see: Nothing.
    • Check: df -h /data shows /dev/nvme1n1, about 1.0G.
    • Why: Linux reaches a disk only through a folder, so the volume needs one to be mounted on.
  7. Prove the files live on the volume
    • Command: cd /data && sudo touch a b c && cd ~ && sudo umount /data
    • You should see: Nothing.
    • Check: ls /data is now empty; sudo mount /dev/nvme1n1 /data and ls /data shows a b c again.
    • Why: Leave /data before unmounting, or you get target is busy.
  8. Get the UUID
    • Command: sudo blkid /dev/nvme1n1
    • You should see: /dev/nvme1n1: UUID="..." BLOCK_SIZE="512" TYPE="xfs"
    • Check: Copy the UUID without the quotes.
    • Why: The UUID belongs to the file system and never changes, while the device name can change after a reboot.
  9. Add the fstab line
    • Command: sudo cp /etc/fstab /etc/fstab.bak && sudo nano /etc/fstab
    • You should see: The existing root line. Add at the end: UUID=YOUR_UUID /data xfs defaults,nofail 0 2.
    • Check: tail -1 /etc/fstab shows your new line.
    • Why: The backup lets you undo a bad edit. nofail lets the server boot even if the volume is missing.
  10. Test before rebooting
    • Command: sudo umount /data && sudo mount -a
    • You should see: No output. If it suggests systemctl daemon-reload, run sudo systemctl daemon-reload.
    • Check: df -h shows /data again. Then sudo reboot, reconnect, and df -h still shows it.
    • Why: mount -a reads fstab exactly as boot does, so a typo shows up now, while you can still fix it.
  11. Clean up
    • Do: sudo umount /data, delete the line from /etc/fstab, then detach and delete the volume in Volumes.
    • Check: Volumes shows no available volume left.
    • Why: Remove the fstab line before detaching, so the next boot doesn't look for a missing disk. A detached volume keeps costing money.

18. Try at home

Try at home

Task 1: add and mount

  1. Launch an Amazon Linux 2023 instance and note its AZ. Create a 1 GiB gp3 volume in another AZ and try to attach it: the server isn't listed.
  2. Create one in the right AZ, attach it as /dev/sdf, and compare lsblk and df -h.
  3. Run sudo file -s on the new volume, mkfs -t xfs, then file -s again.

Task 2: prove and make it permanent

  1. Mount it on /data, create files, unmount, and look into /data. Mount it again.
  2. Add the fstab line with the UUID and nofail, test with sudo mount -a, and reboot.
  3. Misspell nofail on purpose, run sudo mount -a, read the error, and fix it.

Task 3: move it

  1. Launch a second server in the same AZ, move the volume to it without mkfs, and list the files.
  2. Clean up: unmount, detach, delete the volume and terminate both servers.

Recap

In short

  1. The disk of an EC2 server is a separate EBS volume; extra volumes keep data apart from the OS.
  2. gp3 is the default choice: 3,000 IOPS included at any size; io2 is for the busiest databases.
  3. Create the volume in the server's AZ, attach it, and find its real name with lsblk.
  4. file -s saying data is the only time to run mkfs; then mount it on /data.
  5. A mount is temporary; a UUID line with nofail in /etc/fstab, tested with sudo mount -a, makes it permanent.
  6. To move data: unmount, detach, attach in the same AZ, mount, and never format again.

Practice Tasks

About this scenario

Kolhapuri Chappal Co is a footwear business in Kolhapur. Sachin Mane looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy Check free disk space

Why: If a disk is 100% full, apps and databases stop working.

What to do: Show free and used space of all mounted disks in human-readable units.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy List the disks

Why: After you attach an EBS volume, lsblk shows its device name.

What to do: List all block devices (disks and partitions) on the server.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check a folder's size

Why: du tells you how much space a folder uses, in easy units with -h.

What to do: Show the total size of /var/www/kolhapurichappal as one line in human-readable units.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Check memory

Why: If RAM is full the server slows down or kills apps.

What to do: Show used and free memory in human-readable units.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Snapshot an EBS volume

Why: A snapshot is a point-in-time backup of one disk, kept in S3 by AWS.

What to do: Create a snapshot of volume vol-0dfc00420fccfdeed with description "nightly kolhapurichappal".

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Attach a volume to an instance

Why: A volume must be attached (same AZ) before the server can see it.

What to do: Attach volume vol-0dfc00420fccfdeed to instance i-0da02400dac111a2b as device /dev/sdf.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Create a new EBS volume

Why: Extra data disks keep app data apart from the root disk.

What to do: Create a 20 GiB gp3 volume in Availability Zone ap-south-1a.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Find a disk's UUID

Why: /etc/fstab should use the UUID, because device names can change after a reboot.

What to do: Show the UUID of /dev/xvdf.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Format a new disk

Why: A new EBS volume is empty. It needs a file system before you can store files.

What to do: Format /dev/xvdf with the xfs file system.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Grow a volume without downtime

Why: EBS volumes can grow while in use. Afterwards you grow the file system too.

What to do: Increase volume vol-0dfc00420fccfdeed to 50 GiB.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Find big files

Why: When the disk is full, find large files first.

What to do: Find files bigger than 100 MB anywhere under /var.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Resize an instance (vertical scaling)

Why: When the server is too small, change its type while it is stopped.

What to do: The stopped instance i-0da02400dac111a2b needs more power. Change its instance type to t3.large.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: Grow a volume vs Add a new volume. Grow a volume: Make an existing EBS disk bigger. Add a new volume: Create a second EBS disk and attach it.
Comparison: Grow a volume vs Add a new volume · Download PNG
Comparison card: lsblk vs df -h. lsblk: Lists disks and partitions. df -h: Shows space on mounted filesystems.
Comparison: lsblk vs df -h · Download PNG
Revision card: What is an EC2 server made of?. When CPU, RAM, disk or IP runs out, you must know which part to change. Three parts: the instance (CPU and RAM), an EBS volume (disk) and an ENI (network card with the IPs). See them in the instance details: Storage tab for EBS, Networking tab for ENI. t3.micro = 2 vCPU, 1 GiB RAM. Root EBS 8 GiB on Amazon Linux 2023.
Revision card: What is an EC2 server made of? · Download PNG
Revision card: EBS volume types and IOPS. The type sets speed and price. A wrong type makes the database slow or the bill high. IOPS = read/write operations per second. gp3/gp2 are general SSD, io2/io1 fast SSD, st1/sc1 cheap HDD. Pick the type while creating the volume. gp3 is the normal default. gp3 gives 3000 IOPS at any size. A busy MySQL may need io2 with 10000 IOPS.
Revision card: EBS volume types and IOPS · Download PNG
Revision card: Format and mount a new EBS volume. A new attached volume is raw. Linux cannot store files on it yet. Format = create a file system (xfs). Mount = join the disk to a folder like /data. Check it is empty with file -s, then format ONLY a new volume and mount. Mount the 10 GiB volume on /data:
Revision card: Format and mount a new EBS volume · Download PNG
Revision card: Permanent mount with /etc/fstab. A normal mount is gone after a reboot. /data becomes an empty folder. /etc/fstab is the list of disks Linux mounts at every boot. Use the UUID (from blkid) and nofail, then test with mount -a before rebooting. One fstab line:
Revision card: Permanent mount with /etc/fstab · Download PNG
Comparison: gp3 vs io2. gp3: General purpose SSD, the normal default EBS type. io2: Provisioned IOPS SSD for very busy databases.
Comparison: gp3 vs io2 · Download PNG
Interview card: How do you increase the root EBS volume of a running EC2?. Modify the volume size in the console, then grow the partition and file system in Linux. The disk is full; growing needs no downtime and no new volume. 8 › 16 GiB, then:
Interview card: How do you increase the root EBS volume of a running EC2? · Download PNG
Interview card: Can you attach an EBS volume to an EC2 in another AZ?. No. An EBS volume lives in one AZ and attaches only to instances in that AZ. The disk sits in that AZ's storage network for low latency. To move it: snapshot, then create a new volume from it in the other AZ.
Interview card: Can you attach an EBS volume to an EC2 in another AZ? · Download PNG
Interview card: Why use UUID and nofail in /etc/fstab?. UUID is the file system's fixed ID. nofail lets boot continue if the disk is missing. Device names like /dev/nvme1n1 can change; a missing disk without nofail can stop boot. Get the UUID with blkid, add the line, test with mount -a.
Interview card: Why use UUID and nofail in /etc/fstab? · Download PNG
Interview card: When must you NOT run mkfs on a volume?. Never on a volume that already has data, like a volume moved from another server. mkfs creates a new empty file system and wipes what was there. Check first: "data" means empty; "XFS filesystem" means just mount it.
Interview card: When must you NOT run mkfs on a volume? · Download PNG

More cards: Ravi Sir's Cards.