Commands for Managing Linux Volumes and AWS EBS
1. Introduction to Linux Volumes and AWS EBS
- List block devices:
lsblk
- Check attached EBS volumes (after attaching via AWS Console):
lsblk
2. Physical vs Logical Volumes in Linux
- List block devices:
lsblk
- Create physical volumes (LVM context):
sudo pvcreate /dev/xvdf /dev/xvdg
- Create a volume group:
sudo vgcreate my_vg /dev/xvdf /dev/xvdg
- Create a logical volume:
sudo lvcreate -L 10G -n my_lv my_vg
- Format the logical volume:
sudo mkfs.ext4 /dev/my_vg/my_lv
- Mount the logical volume:
sudo mkdir /mnt/lvm_volume
sudo mount /dev/my_vg/my_lv /mnt/lvm_volume
3. Mounting Volumes in Linux
- List block devices (to identify the device):
lsblk
- Format the device:
sudo mkfs -t ext4 /dev/xvdf
- Create a mount point:
sudo mkdir /mnt/myvolume
- Mount the volume:
sudo mount /dev/xvdf /mnt/myvolume
- Verify the mount (check filesystem usage):
df -h
- Make mount persistent (edit /etc/fstab):
sudo nano /etc/fstab
- Add the line:
/dev/xvdf /mnt/myvolume ext4 defaults,nofail 0 2
- Unmount the volume:
sudo umount /mnt/myvolume
- Remount the volume:
sudo mount /dev/xvdf /mnt/myvolume
4. Managing AWS EBS on EC2 Instances
- List block devices (to verify EBS attachment):
lsblk
- Format the EBS volume:
sudo mkfs -t ext4 /dev/xvdf
- Create a mount point:
sudo mkdir /mnt/ebs_volume
- Mount the EBS volume:
sudo mount /dev/xvdf /mnt/ebs_volume
- Verify the mount (check filesystem usage):
df -h
- Make the EBS volume mount persistent (edit /etc/fstab):
sudo nano /etc/fstab
- Add the line:
/dev/xvdf /mnt/ebs_volume ext4 defaults,nofail 0 2
- Unmount the EBS volume:
sudo umount /mnt/ebs_volume
5. Introduction to LVM (Logical Volume Manager)
- Install LVM tools (if needed):
sudo apt-get install lvm2
- Initialize physical volumes (create PVs):
sudo pvcreate /dev/xvdf /dev/xvdg
- Create a volume group (VG):
sudo vgcreate my_vg /dev/xvdf /dev/xvdg
- Create a logical volume (LV):
sudo lvcreate -L 10G -n my_lv my_vg
- Format the logical volume:
sudo mkfs.ext4 /dev/my_vg/my_lv
- Mount the logical volume:
sudo mkdir /mnt/lvm_volume
sudo mount /dev/my_vg/my_lv /mnt/lvm_volume
- Extend the logical volume (on the fly):
sudo lvextend -L +5G /dev/my_vg/my_lv
- Resize the filesystem:
sudo resize2fs /dev/my_vg/my_lv
6. Using LVM with EBS for Dynamic Storage Management
- Initialize new EBS volumes as physical volumes:
sudo pvcreate /dev/xvdf /dev/xvdg /dev/xvdh
- Extend the volume group:
sudo vgextend my_vg /dev/xvdg /dev/xvdh
- Extend the logical volume:
sudo lvextend -L +30G /dev/my_vg/my_lv
- Resize the filesystem:
sudo resize2fs /dev/my_vg/my_lv
- Verify the logical volume and filesystem:
sudo lvdisplay
df -h