Disk Management in Linux

Disk management in Linux involves the process of handling disk devices, partitioning, formatting, mounting, and managing storage space on a Linux system. It’s essential for maintaining the system’s data integrity and optimizing storage usage. Here’s a comprehensive overview of disk management in Linux:

1. Understanding Disk Devices in Linux

In Linux, storage devices are represented as files in the /dev directory. These files are not actual physical files but represent disk devices. Some common device names are:

  • /dev/sda, /dev/sdb: These represent the first, second, and subsequent hard drives (SATA, SCSI, etc.).
  • /dev/sda1, /dev/sda2: These represent partitions on the first hard drive.
  • /dev/sr0: Represents an optical drive (CD/DVD).
  • /dev/mapper/: Represents logical volumes (in the case of LVM – Logical Volume Manager).

2. Disk Partitioning

Partitioning is the process of dividing a storage device into distinct sections, called partitions, each of which can act as a separate disk. Linux typically uses partitioning tools like fdisk, parted, or gparted (graphical) to create and manage partitions.

Common Partitioning Tools:

  • fdisk (for MBR – Master Boot Record):
    • sudo fdisk /dev/sda: Launch fdisk for the first disk (/dev/sda).
    • Commands: n (new partition), d (delete partition), p (print partition table), w (write changes).
  • parted (for GPT – GUID Partition Table):
    • sudo parted /dev/sda: Launch parted to manage partitions.
    • Commands: mkpart (create a new partition), rm (remove a partition), print (list partitions).
  • gparted (Graphical interface):
    • sudo apt install gparted: Install gparted for graphical partition management.
    • It provides a user-friendly interface for creating, resizing, and deleting partitions.

3. File System Creation

Once a partition is created, it needs to be formatted with a file system (e.g., ext4, xfs, btrfs, etc.) to make it usable.

Common File System Types:

  • ext4: The most commonly used file system for Linux. It supports large volumes and files.
  • xfs: A high-performance journaling file system used for large systems.
  • btrfs: A newer file system with advanced features like snapshots, RAID support, and volume management.
  • ntfs: File system used by Windows (Linux can read/write with additional drivers).
  • vfat/exFAT: Common file systems used for flash drives, compatible with Windows and macOS.

Formatting a Partition:
To format a partition, use the mkfs command:

ext4:
sudo mkfs.ext4 /dev/sda1
xfs:
sudo mkfs.xfs /dev/sda1


4. Mounting and Unmounting Partitions
Mounting is the process of making a partition or disk available for use by attaching it to a directory in the Linux file system. The directory to which it is attached is called the mount point.

lsblkLists information about block devices (disks and partitions).


This command provides details about disks, including their size, type, and mount points.
Shows detailed partition tables of all disks.
fdisk -l

 

blkid Shows block device attributes like filesystem types.


A more advanced partitioning tool, useful for managing partitions on both
MBR (Master Boot Record) and GPT (GUID Partition Table) disk schemes.
parted -l


Partitioning Disks
Partitioning a disk involves dividing it into multiple partitions.The most common tools used
for partitioning in Linux are:
fdisk: For MBR (Master Boot Record) disks.
sudo fdisk /dev/sda

 

This command starts an interactive session where you can create, delete, and modify partitions.

gdisk: For GPT (GUID Partition Table) disks..
sudo gdisk /dev/sda

 

Creating Filesystems
Once a disk or partition is created, you need to format it with a
filesystem. Some common filesystem types
include ext4, XFS, and Btrfs. To create a
filesystem, use the following commands:
sudo mkfs.ext4 /dev/sda1

Example: Create an XFS filesystem:
sudo mkfs.xfs /dev/sda1
mkfs.fat: Used for creating FAT32 filesystems,
often used for USB drives or small partitions.
sudo mkfs.fat -F 32 /dev/sda1

Disk Partition Tools (Graphical)

Ifou prefer using a graphical interface, several tools are available:

  • GParted: A GUI tool for managing partitions. It allows resizing, creating, and deleting partitions with an easy-to-use interface.
  • Disks (gnome-disk-utility): Another simple GUI tool available in many Linux distributions for partitioning and mounting drives.

How to check hdd?
sudo fdisk -l

How to check partition MBR/GPT?
gdisk /dev/sda
lsblk

 

 

How to create partition ?
sudo fdisk /dev/sda
help- m
n
enter
enter
+20G
p- print
w-save
fdisk -l

 

 

How to format partition ?
mkfs.ext4 /dev/sda
lsblk -f -chcek format or not partition

How to mount partiton?
sudo mount /dev/sda /<mount folder>
check – sudo mount
lsblk

How to Manage and Use LVM (Logical Volume Management)

LVM

Physical Volume = pv
Volume Group = vg
Logical Volume = lv

How to View Current LVM Information
pvdisplay


fdisk /dev/sdb

n = create new partition
p = creates primary partition
1 = makes partition the first on the disk


t = change partition type
8e = changes to LVM partition type


p = view partition setup so we can review before writing changes to disk
w = write changes to disk

 

1 – pvcreate /dev/sdb1 <create a LVM physical volume on the partition we just created>

Create volume Group

2 – vgcreate vgpool /dev/sdb1

Create logical volume

3 – lvcreate -L 3G -n lvstuff vgpool

Format and Mount the Logical Volume

4 – mkfs -t ext3 /dev/vgpool/lvstuff

Create a mount point and then mount the volume somewhere you can use it.

5 – mkdir /mnt/stuff
mount -t ext3 /dev/vgpool/lvstuff /mnt/stuff

Resizing a Logical Volume


6 – vgextend vgpool /dev/sdc1

Extend Logical Volume

7 – lvextend -L8G /dev/vgpool/lvstuff

8 – resize2fs /dev/vgpool/lvstuff

 

How To Cheking Linux Ubantu Disk Status ?
lsblk
fdisk -l
df -hT
lsblk -f <Cheking Formating Partition >
mount -av <cheking Mounting>

 

How To Create Linux Ubantu LVM ?
Step#01 fdisk /dev/sdb <Go To HDD >
Step#02 n, p, t, 8e, w <New Partion Create>
Step#03 partprobe /dev/sdb <Karnel Infom For New HDD Partition>
Step#04 pvcreate /dev/sdb1 <Create Pv>
Step#05 vgcreate nextcloud /dev/sdb1 <Create VG>
Step#06 lvcreate -L 500G -n nextcloud nextcloudlv <Create LV>
Step#07 mkfs.ext4 /dev/mapper/nextcloud-nextcloudlv <Formating>
Step#08 mount /dev/mapper/nextcloud-nextcloudlv /var/www/html <Create Mount Point For Use Disk Space>
Step#09 df -hT <For Checking Mount>
Step#10 cat/etc/mtab <Copy Full Path nextcloud-nextcloudlv For Entry Fstab>
Step#11 nano /etc/fstab
Step#11 /dev/mapper/nextcloud-nextcloudlv /var/www/html ext4 errors=remount-ro 0 1 <Paste This Line Fstab Then Save>

 

How To Cheking Linux Ubantu LVM Status ?
pvdisplay
vgdisplay
lvdisplay
lvs
vgs
pvs
lvscan
pvscan
vgscan

 

How To Remove Linux Ubantu LVM ?
lvremove /dev/nextcloud/nextcloudlv

How To Check Partition Style MBR or GPT

fdisk -l /dev/sda

parted /dev/sda print <Checking HDD Information>
parted /dev/sda mklavel msdos <MBR>
parted /dev/sda mklavel gpt <gpt>