Skip to content

Latest commit

 

History

History
679 lines (470 loc) · 17.2 KB

Linux_Volume_Management.md

File metadata and controls

679 lines (470 loc) · 17.2 KB

Linux Volume Management

List of Contents


Introduction to Linux Volumes and AWS EBS

🚀 Let's Start With a Real-World Scenario:

Imagine you're launching an EC2 instance on AWS — basically, your personal cloud server. Now, when you want to store files or run applications, you’ll need storage space, right?

In Linux (and AWS), that storage is called a volume. Think of it as a virtual hard disk.

🗃️ What is a Volume?

When you create an EC2 instance, AWS gives you default storage (usually 8 GB) — this is called the root volume. But as your application grows (logs, user data, backups), you’ll need more space.

💡 That’s where extra volumes come in — we create additional disks and attach them to the instance.

📦 What is AWS EBS (Elastic Block Store)?

AWS EBS is a service that lets you create extra storage volumes and attach them to your EC2 instance.

Think of EC2 as a computer and EBS volumes as external pen drives or hard disks you plug into it.


🛠️ Hands-On: Create Volumes & Attach to EC2

1️⃣ Create an EC2 Instance

  • Go to AWS EC2 console
  • Choose Ubuntu as the OS
  • Select instance type: t2.micro
  • Create or use an existing key pair (used for SSH login)
  • Allow SSH (port 22) in the security group (very important!)
  • In storage section: default 8 GB is pre-set (root volume)
  • Launch the instance ✅

2️⃣ Connect to EC2 via SSH

  • Download the .pem key file
  • Open terminal and run:
chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@<your-ec2-ip>
  • 🎉 You’re now inside your cloud server!

🔍 Check Existing Volumes (Root Disk) Run:

lsblk

This shows all attached volumes:

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
```xvda` → Your main/root volume (disk)

`xvda1` → Partition of the root volume

`/` → Means it’s mounted to root of the file system

</section>

<section id="view_disk_usage">

#### 📊 View Disk Usage
Run:

```bash
df -h

This shows used/free space:

Filesystem       Size Used Avail Use% Mounted on
/dev/root        7.8G  1.6G  6.0G  22% /

🤔 What Does “Mount” Mean?

“Mounting” means connecting a volume to a directory path in Linux.

So when your volume is mounted to /, it means that Linux is reading/writing to it via that location.


🔗 Now, Let’s Add Extra Volumes (10GB, 12GB, 14GB)

1. Go to “Elastic Block Store” → “Volumes”

Click “Create Volume”

  • Type: General Purpose (gp2)
  • Size: 10 GB (similarly repeat for 12 GB & 14 GB)
  • Availability Zone: must match your EC2’s zone (e.g., ap-south-1a)
  • Click Create Volume

🔁 Repeat for 12 GB and 14 GB volumes.

🔌 Attaching Volumes to EC2

For each volume:

  • Click → Actions → Attach Volume
  • Select your EC2 Instance
- Set Device Name as /dev/sdf (first one), then /dev/sdg, /dev/sdh

💡 Don't use /dev/sdb, /dev/sdc, etc. — those are usually reserved for system use.

✅ Verify Volume Attachment

Run:

lsblk

You’ll now see:

NAME   8G   ← root disk
xvdf  10G   ← attached EBS
xvdg  12G
xvdh  14G

👉 But notice: their MOUNTPOINT is empty — this means they’re attached but not mounted yet.


Physical vs Logical Volumes vs Volume Groups in Linux

💡 Context: You’ve Attached Extra Disks… Now What?

In the previous steps, you added 3 extra volumes (10 GB, 12 GB, and 14 GB) to your EC2 instance. But right now, they’re just raw blocks of storage — like unformatted pen drives.

To use them properly, Linux uses a smart system called LVM — Logical Volume Manager.


What is LVM?

LVM Layer Real-World Analogy
Physical Volume (PV) Raw bricks (your disk space)
Volume Group (VG) A big room made by combining bricks
Logical Volume (LV) Separate cabins (partitions) built in room

🔹 Step-by-Step LVM Process (With Commands)

🔑 Step 1: Become Root User

You’ll need root permissions to use LVM. You can either run sudo before each command or switch to root:

sudo su
lvm   # optional: drops into LVM shell

📌 Step 2: Create Physical Volumes

Right now your new disks (/dev/xvdf, /dev/xvdg, /dev/xvdh) are not usable by LVM — we need to convert them to Physical Volumes (PV).

pvcreate /dev/xvdf /dev/xvdg /dev/xvdh
# ✅ This tells Linux: “These disks are ready to be managed by LVM.”

You can verify:

pvs

🧱 Step 3: Create a Volume Group (VG)

Now combine some physical volumes into a Volume Group (VG). For example:

vgcreate my_vg /dev/xvdf /dev/xvdg

# This creates a Volume Group named my_vg with 10 GB + 12 GB = 22 GB.

Check with:

vgs

# It will show:
# - Volume Group name
# - Size
# - Number of PVs and LVs inside it

📦 Step 4: Create Logical Volumes (LV)

Now let’s cut out a slice from that VG and create a Logical Volume (LV) — say, a 10 GB slice:

lvcreate -L 10G -n my_lv my_vg

# -L = size of logical volume
# -n = name of logical volume
# my_vg = the group from which this slice is carved out

Verify:

lvs

🧾 Bonus: Display Commands (For Debugging)

Show physical volume info:

pvdisplay

Show volume group details:

vgdisplay

Show logical volume details:

lvdisplay

📌 Summary of LVM Workflow

1. pvcreate       → Make raw disks usable by LVM
2. vgcreate       → Combine PVs into a volume group
3. lvcreate       → Create partitions (LVs) from the VG

At this point, your logical volume (like /dev/my_vg/my_lv) is ready — but you can’t use it yet. You still need to:

  1. Format it
  2. Mount it to a folder

Mounting Volumes in Linux (LVM)

🧠 Recap Before Mounting:

You’ve already created:

  • ✅ Physical Volumes (pvcreate)
  • ✅ Volume Group (vgcreate)
  • ✅ Logical Volume (lvcreate)

Now it’s time to make the Logical Volume usable — and that’s where mounting comes in.


❓ Why Isn’t My Volume Showing in df -h?

If you run:

lsblk

# You’ll see your disks and LVM layout like /dev/xvdf, /dev/my_vg/my_lv.

But if you run:

df -h

👉 You won’t see your new volume here yet — because it’s not mounted.

🧱 Step-by-Step: Mount a Logical Volume

🔍 1. Verify the Logical Volume

lvs

# Make sure your LV (e.g., my_lv) exists inside your VG (e.g., my_vg).

📁 2. Create a Mount Point

Think of this as a folder where your volume will be plugged in:

mkdir /mnt/my_mount

🧹 3. Format the Logical Volume

Before mounting, you need to create a filesystem (e.g., EXT4) on the volume:

mkfs.ext4 /dev/my_vg/my_lv

#⚠️ This will erase everything on the volume, just like formatting a pen drive.

📌 4. Mount the Volume

Now bind (mount) the LV to your mount folder:

mount /dev/my_vg/my_lv /mnt/my_mount
# ✅ Now your storage is usable from that folder!

✅ 5. Check If It’s Mounted

df -h

# You’ll see a new line like:

/dev/mapper/my_vg-my_lv   10G   50M   9.9G   1% /mnt/my_mount

# 🎉 Success! The volume is now mounted and ready to store files.

📂 Let’s Test It

Go inside the mount directory:

cd /mnt/my_mount
echo "Hello from LVM!" > test.txt
cat test.txt

# 💡 If you see the content, it means your mounted volume is working perfectly.

❓ Difference Between Attached vs Mounted Volume

Concept Meaning
Attached Disk is plugged into the system (via AWS EBS, etc.)
Mounted Disk is connected to a directory and can be used

👉 Attached is like plugging in a USB drive

👉 Mounted is like opening it and starting to use it


🔌 How to Unmount and Remount

🛑 Unmount:

umount /mnt/my_mount

If you now try:

cat /mnt/my_mount/test.txt

❌ You won’t be able to access it (because it’s unmounted). But don’t worry — your data is safe.

🔁 Remount:

mount /dev/my_vg/my_lv /mnt/my_mount
# Now the file is back!

🧠 Summary

  • lsblk shows block devices
  • df -h shows mounted filesystems
  • mkfs.ext4 formats the LV
  • mount makes it usable
  • You can store files, read them, unmount, remount — your data will remain safe on disk

💾 Managing AWS EBS on EC2 Instances (Without LVM)

🎯 Objective: Mount an AWS EBS Volume without using LVM So far, you've used LVM to manage storage. But what if you just want to use an EBS volume directly, like a plug-and-play hard disk?

Let’s say you have one unused volume left: /dev/xvdh (14 GB). Let’s mount it directly to your EC2 instance.


🔧 Step-by-Step: Mount EBS Volume Without LVM

📁 1. Create a Mount Directory

Create a folder where you want to mount the disk:

mkdir /mnt/new_disk_mount

# So now in /mnt/, you’ll have:
/mnt/new_lv_mount ← for LVM-managed volume
/mnt/new_disk_mount ← for directly-mounted EBS disk

🧹 2. Format the Volume

Before using a raw disk, you must format it and apply a file system.

mkfs -t ext4 /dev/xvdh

# mkfs: Make filesystem
# -t ext4: Choose file system type (EXT4 is default and most used in Linux)
# ⚠️ This will wipe the disk clean!

🔗 3. Mount the Disk

Now mount the volume (bind it to the directory):

mount /dev/xvdh /mnt/new_disk_mount

Now it's ready to use. Check it:

df -h

You’ll see something like:

/dev/xvdh   14G  24M  13.9G   1% /mnt/new_disk_mount

✅ You can now create files inside /mnt/new_disk_mount and use the disk like normal storage!


💡 Summary: EBS Without LVM

Feature With LVM Without LVM
Disk flexibility Extend/Shrink volumes dynamically Fixed size, no flexibility
Setup complexity Higher (PV, VG, LV setup needed) Very simple (just format & mount)
Use case Dynamic apps, servers, scaling Simple static storage (logs, media, etc.)

🔄 Using LVM with EBS for Dynamic Storage Management

Now let’s say your app is growing and you need more storage on an LVM volume — LVM makes this easy.

🔧 How to Extend a Logical Volume (LV)

Let’s assume your volume group still has free space. To add 5 GB more to your logical volume:

lvextend -L +5G /dev/new_vg/new_lv

# -L +5G: Add 5 GB to existing size
# /dev/new_vg/new_lv: Your target LV

✅ This extends the volume at the LVM level.

📢 Important: Resize the Filesystem Too

After extending the LV, you must tell the file system to use the new space:

resize2fs /dev/new_vg/new_lv

Without this, your file system will still act like it’s using the old size!

✅ Final Check

df -h

You’ll see that the /mnt/new_lv_mount volume now shows the increased space.

🔚 Final Thoughts

  • LVM is powerful for managing growing applications or dynamic storage.
  • Direct EBS volumes are quick and simple, great for static use cases.
  • Combine both depending on your DevOps need and system architecture.