> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/disk-management.md).

# Disk Management

A disk is a computer data storage device that stores, retrieves, and manages digital information. Linux supports various types of disks, each with unique features and limitations.

***

## Types of Disks

#### 1. Advanced Technology Attachment (ATA)

ATA is an older standard for connecting storage devices to a computer. It allows direct memory access and supports up to 40 GB in size.

#### 2. Parallel Advanced Technology Attachment (PATA)

PATA is an improved version of ATA that uses parallel signalling. It supports up to 512 GB and was widely used before SATA.

#### 3. Serial Advanced Technology Attachment (SATA)

SATA is the successor to PATA, offering faster data transfer rates and greater storage capacity, up to 8 TB. It operates at 7200 RPM for high-speed performance.

#### 4. Small Computer System Interface (SCSI)

SCSI is designed for high-performance storage devices, commonly used in enterprise environments due to its speed and reliability.

#### 5. Solid State Drive (SSD)

SSDs use flash memory instead of spinning disks, providing faster performance, lower power consumption, and increased durability.

<figure><img src="/files/irCoscgjsRQ0HzmDHyK8" alt=""><figcaption></figcaption></figure>

***

## Disk Structure

#### 1. PATA Disk Structure

| Interface | Master   | Slave    |
| --------- | -------- | -------- |
| Primary   | /dev/hda | /dev/hdb |
| Secondary | /dev/hdc | /dev/hdd |

#### 2. SATA Disk Structure

| Interface | Device                                 |
| --------- | -------------------------------------- |
| SATA      | /dev/sda, /dev/sdb, /dev/sdc, /dev/sdd |

***

## Partition Types

1. **Primary Partition** - The main partition used for booting and storing data.
2. **Extended Partition** - A special partition that can contain multiple logical partitions.
3. **Logical Partition** - A subdivision of an extended partition, allowing multiple partitions on a single disk.

#### 1. Example Partition Layout:

```
/dev/sda
├── /dev/sda1 (Primary)
├── /dev/sda2 (Primary)
└── /dev/sda3 (Extended)
    ├── /dev/sda4 (Logical)
    └── /dev/sda5 (Logical)
```

***

## Disk Management Commands

* Show Unmounted Disks:

  ```
  blkid
  ```
* Show All Disks:

  ```
  lsblk
  ```
* Show All Disk Details:

  ```
  fdisk -l
  ```

### Creating a Primary Partition

```
fdisk /dev/diskname
```

<figure><img src="/files/kwaTDMK0mNOUMSXWP5tq" alt=""><figcaption></figcaption></figure>

```
Use m for help
Use p for print partition table
Use n for new partition
Select partition type [primary]
Select partition number
Define start and end size
Use w (Save changes)
Use q (Exit without saving)
```

### Creating an Extended Partition and Logical Partition

```
fdisk /dev/diskname
```

```
Use m for help
Use p for print partition table
Use n for new partition
Select partition type 'e' [extended]
Enter twice with default configurations
Use n for new partition
Select partition type 'l' [logical]
Define start and end size
Use w (Save changes)
Use q (Exit without saving)
```

### Changing Partition Type

* Select Disk:

  ```
  fdisk /dev/diskname
  ```
* Show List of Partition Types:

  ```
  Use l
  ```
* Change Disk Type:

  ```
  Use t
  Select partition
  Select type
  Use w (Save changes)
  ```

### Formatting a Partition

```
mkfs.<filesystem_type> /dev/partition_name
```

### **Temporary Mount**

```
mount /dev/partition_name /mount_directory
```

### **Permanent Mount (FSTAB Configuration)**

1. Get the UUID:

   ```
   blkid
   ```
2. Edit `/etc/fstab`:

   ```
   UUID=57b5fb0f-7754-4355-bb60-11f44c7f7df4 /boot xfs defaults 0 0
   ```

   **Explanation:**

   | Field        | Description                                                   |
   | ------------ | ------------------------------------------------------------- |
   | **UUID**     | Unique identifier of the partition                            |
   | **/boot**    | Mounting point                                                |
   | **xfs**      | File system type                                              |
   | **defaults** | Partition permissions                                         |
   | **0**        | On boot check                                                 |
   | **0**        | Checking order (1 for root, 2 for others, 0 to disable check) |
3. Apply changes:

   ```
   mount -a
   ```

### **Mount Using Labels**

```
e2label /dev/ext4_partition_name LABEL_NAME
xfs_admin -L LABEL_NAME /dev/partition_name
mount -a
```

**Explanation:**

| Field          | Description           |
| -------------- | --------------------- |
| **LABEL=data** | Label name            |
| **/d4**        | Mount name            |
| **xfs**        | File system type      |
| **defaults**   | Partition permissions |
| **0**          | On boot check         |
| **0**          | Checking order        |

***

## Swap Management

#### 1. Creating a Swap Partition

```
fdisk /dev/partition_name
```

```
Enter t
Select partition
Select type 82 (Swap Partition)
Enter w
```

#### 2. Formatting and Enabling Swap

```
mkswap /dev/partition_name
swapon /dev/partition_name
swapon -s
```

#### 3. Permanent Swap Configuration

1. Edit `/etc/fstab`:

   ```
   /dev/partition_name swap swap defaults 0 0
   ```
2. Apply changes:

   ```
   mount -a
   ```

#### 4. Disabling Swap

```
swapoff /dev/partition_name
```

1. Edit `/etc/fstab` to remove the swap entry.
2. Apply changes:

   ```
   mount -a
   ```

***

## Quota Management

#### What is Quota?

Quota is a system feature used to limit disk usage for users and groups in Linux.

#### Types of Quota

1. **Soft Quota** - A warning limit that allows temporary exceedance.
2. **Hard Quota** - A strict limit that cannot be exceeded.

#### Setting Up Quota

**Install Quota Package**

```
yum install quota
```

**5.3.2 Run Quota Check**

```
quotacheck 
```

**Options:**

| Switch | Description        |
| ------ | ------------------ |
| -c     | Create quota files |
| -u     | Apply to users     |
| -g     | Apply to groups    |
| -v     | Verbose output     |

**Edit Quota Configuration**

Edit the `/etc/fstab` file to enable quota.

**Apply Quota**

```
quotacheck -cugv /dirname
quotaon /dirname
```

**Set Quota for a User or Group**

```
edquota username/groupname
```

| Field  | Description     |
| ------ | --------------- |
| Blocks | Disk size used  |
| Soft   | Soft quota size |
| Hard   | Hard quota size |

**Generate Quota Report**

```
repquota -a
```

***

## RAID (Redundant Array of Independent Disks)

RAID is a data storage virtualization technology that combines multiple physical disk drives into a single logical unit to enhance performance, redundancy, or both.

#### 1. Uses of RAID

* Provides data redundancy and fault tolerance.
* Improves performance for reading and writing data.
* Used in environments that require high availability and large storage capacity.

#### 2. RAID Levels

**RAID 0 (Striping)**

* Data is divided into blocks and written across multiple disks simultaneously.
* **Advantage**: Increased performance.
* **Disadvantage**: No redundancy; if a single disk fails, all data is lost.

**RAID 1 (Mirroring)**

* Data is duplicated across two or more disks.
* **Advantage**: Provides redundancy; if one disk fails, the data is still available on the other.
* **Disadvantage**: Requires twice the storage space.

**RAID 5 (Striping with Parity)**

* Data and parity (error correction) information are distributed across multiple disks.
* **Advantage**: Can tolerate a single disk failure without data loss.
* **Disadvantage**: Requires at least three disks.

**RAID 10 (1+0, Mirroring and Striping)**

* Combines RAID 1 and RAID 0 by striping data across mirrored pairs of disks.
* **Advantage**: Provides both speed and redundancy.
* **Disadvantage**: Requires at least four disks.

***

## Logical Volume Manager (LVM)

LVM is a storage management method that provides flexible disk space allocation.

#### Why Use LVM?

* Allows resizing of logical volumes dynamically.
* Supports multiple physical disks within a single volume group.
* Provides snapshots for backups.

#### LVM Components

1. **Physical Volume (PV)** - A physical disk or partition that is initialized for LVM.
2. **Volume Group (VG)** - A pool of storage made from one or more PVs.
3. **Logical Volume (LV)** - A virtual partition created from a VG that acts as a standard block device.

<figure><img src="/files/s9vOpuAJwSm86d8LNyxa" alt=""><figcaption></figcaption></figure>

#### LVM Partition Creation Commands

* Display Physical Volumes:

  ```
  pvdisplay
  ```
* Create a Physical Volume:

  ```
  pvcreate /dev/partition_name
  ```
* Display Volume Groups:

  ```
  vgdisplay
  ```
* Create a Volume Group:

  ```
  vgcreate VGname /dev/PV_partition1 /dev/PV_partition2
  ```
* Display Logical Volumes:

  ```
  lvdisplay
  ```
* Create a Logical Volume:

  ```
  lvcreate VGname --name LVname --size LVsize
  ```

#### LVM Partition Removal Commands

* Remove a Logical Volume:

  ```
  lvremove LVname
  ```
* Remove a Volume Group:

  ```
  vgremove VGname
  ```
* Remove a Physical Volume:

  ```
  pvremove /dev/partition_name
  ```
