> For the complete documentation index, see [llms.txt](https://ghoulsec.gitbook.io/ghoulsec-vault/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ghoulsec.gitbook.io/ghoulsec-vault/server-are-fun/virtualization/proxmox/creating-an-lvm-thin-pool.md).

# Creating an LVM Thin Pool

## Documentation: Creating and Managing a Thin Pool for Storage

This guide outlines the steps to create a thin pool and prepare it for use in a Proxmox environment. The following tasks will be covered:

1. Formatting the Disk
2. Creating a Physical Volume (PV) and Volume Group (VG)
3. Creating an LVM Thin Pool
4. Creating a Logical Volume for Storage
5. Formatting the Logical Volume
6. Mounting the Logical Volume

***

### Task 2: Creating the Thin Pool

#### Step 1: Format the Disk

Before using a disk (e.g., `/dev/sdb`), a new partition must be created:

```bash
sgdisk -N 1 /dev/sdb
```

* **Command Explanation**:
  * `sgdisk`: Command-line utility to manage GPT partitions.
  * `-N 1`: Creates a new partition spanning the entire free space on the disk as Partition 1.
  * `/dev/sdb`: Target disk.

***

#### Step 2: Create Physical Volume (PV) and Volume Group (VG)

Prepare the partition for use with LVM by creating a Physical Volume (PV) and a Volume Group (VG).

**Create Physical Volume:**

```bash
pvcreate --metadatasize 1024M -y -ff /dev/sdb1
```

* **Command Explanation**:
  * `pvcreate`: Initializes a physical volume for LVM.
  * `--metadatasize 1024M`: Sets metadata size to 1024 MB.
  * `-y`: Auto-confirm prompts.
  * `-ff`: Forces the creation even if warnings occur.
  * `/dev/sdb1`: Partition created in Step 1.

**Create Volume Group:**

```bash
vgcreate --metadatasize 1024M proxvg /dev/sdb1
```

* **Command Explanation**:
  * `vgcreate`: Creates a volume group for LVM.
  * `--metadatasize 1024M`: Ensures metadata size compatibility with the PV.
  * `proxvg`: Name of the volume group.
  * `/dev/sdb1`: Physical volume added to the volume group.

***

#### Step 3: Create an LVM Thin Pool

Define a thin pool in the volume group:

```bash
lvcreate -l 100%FREE --poolmetadatasize 1024M --chunksize 256 -T -n proxthin proxvg
```

* **Command Explanation**:
  * `lvcreate`: Creates a logical volume.
  * `-l 100%FREE`: Uses all remaining free space in the volume group.
  * `--poolmetadatasize 1024M`: Allocates 1024 MB for pool metadata.
  * `--chunksize 256`: Sets the chunk size for thin provisioning to 256 KB.
  * `-T`: Specifies thin pool creation.
  * `-n proxthin`: Names the thin pool.
  * `proxvg`: Name of the volume group.

***

#### Step 4: Create a Logical Volume for Storage

Allocate space from the thin pool for storage:

```bash
lvcreate -n proxvz -V 400G proxvg/proxthin
```

* **Command Explanation**:
  * `-n proxvz`: Names the logical volume.
  * `-V 400G`: Creates a virtual volume of 400 GB.
  * `proxvg/proxthin`: Thin pool from which space is allocated.

***

#### Step 5: Format the Logical Volume with EXT4

Prepare the logical volume for use with the EXT4 filesystem:

```bash
mkfs.ext4 /dev/proxvg/proxvz
```

* **Command Explanation**:
  * `mkfs.ext4`: Formats the volume with the EXT4 filesystem.
  * `/dev/proxvg/proxvz`: Logical volume path.

***

#### Step 6: Mount the Logical Volume

Mount the logical volume to a directory for usage.

**Create Mount Directory:**

```bash
mkdir /media/vz
```

**Update `fstab` for Persistent Mounting:**

```bash
echo '/dev/proxvg/proxvz /media/vz ext4 defaults,errors=remount-ro 0 2' >> /etc/fstab
```

* **Command Explanation**:
  * Appends mount details to `/etc/fstab` for automatic mounting at boot.
  * `defaults,errors=remount-ro`: Default mount options with read-only remount in case of errors.

**Apply Mount Configuration:**

```bash
mount -a
```

* **Command Explanation**:
  * Applies all entries in `/etc/fstab`.

***

#### Notes

* Ensure to replace `/media/vz` with `/media/vmdirectory` if that is your preferred mount directory.
* Adjust the volume size (`400G`) as needed based on your requirements.
