Thursday, December 28, 2023

LVM Partition.

After installing a new SSD or hard disk, you need to partition it first. A drive requires at least one partition to format it and store files on it. Linux offers several tools for creating partitions, with fdisk being the most popular one. fdisk is a command-line utility that provides a menu-driven interface for creating and modifying partition tables on a hard disk. fdisk is a powerful tool that requires careful usage. Only users with root or sudo privileges can access and alter the partition tables.

List Partitions:
To display the partition table and partitions of a device, use the fdisk command with the -l option and the device name. For example, to show the /dev/sda partition table and partitions, run:
Command: fdisk /dev/sda.
If no device is specified as an argument, fdisk will show the partition tables of all devices in the /proc/partitions file.
Command: fdisk -l.
FIGURE-1
The above output displays the partition tables of all devices connected to your system. SATA device names usually have the format /dev/sd[a-z], while NVMe device names have the format /dev/nvme[1-9]n[1-9].

Creating Partition Table:
To partition the drive, use fdisk with the device name. For example, to work on /dev/sda, run:
Command: fdisk /dev/sda.
The command prompt will change to the fdisk dialogue, where you can enter commands.
fdisk (util-linux 2.34) started.
Changes are not written to disk until confirmed.
Use the write command with caution.
Enter command (m for help):
The partition table changes are not applied until confirmed with the w command. To quit the fdisk dialogue without saving the changes, use the q command.
To see all the command, enter m.
FIGURE:2
I selected 'n' to create a new partition and 'p' to make it a primary partition type. I set the partition number and the first and last sector of the partition. It displayed the new partition type as Linux. If the LVM had a signature, I chose 'y' to remove it.
FIGURE-3
I entered 't' to change the partition type and 'L' to list all the Hex codes. I chose '8e' for Linux LVM.
FIGURE-4
I confirmed the partition type change by entering 'w'. This wrote the table to disk and exited fdisk.
FIGURE-5
I listed the block devices using the 'lsblk' command. It displayed the sda2 partition that I created in the previous steps.
FIGURE-6

Create Physical Volume:
To create physical volumes on the newly partitioned disks, use the 'pvcreate' command.
Command: pvcreate /dev/sdb1.
To view information about the physical volumes on the system, use the 'pvdisplay' command. Optionally, specify a volume to see its details.
Command: pvdisplay.
FIGURE-7
Creating a Virtual Group:
Create a volume group named vol1 that contains the /dev/sda1 partition as a physical volume. Use the following command:
Command: vgcreate vol1 /dev/sda1
To display information about the volume groups, use the vgdisplay command.
Command: vgdisplay.
FIGURE-8

Create Logical Volume:
Now we can move on to create logical volumes. It may help to think of our virtual group as a "big cake," from which we can cut "pieces" (logical volumes) that will get treated as partitions on our Linux system.
The following command will create a logical volume named vol1 with a size of 1Gb.
FIGURE-9
Use the 'lvdisplay' command to display the created logical volumes.
FIGURE-10
The 'vgdisplay' command shows that the volume group vol1 has 1GB of free space left.
FIGURE-11

Create a filesystem on Logical volume:
The logical volume is almost ready to use. All we need to do is to create a filesystem on it with the 'mkfs' command.
Command: mkfs.ext4 /dev/vol1/LV1.
FIGURE-12

Mount Logical volume:
Before using the new volumes, create a mount point and mount the volumes to it. Then, use the 'lsblk' command to verify the mount status of the directory.
Command: mount /dev/vol1/LV1 /path of the directory.
FIGURE-13

Removing a Logical Volume:
To remove logical volumes using the 'lvremove' command, ensure that the volumes are unmounted and deactivated, and that no critical data is stored on them.
Command: lvremove /dev/vol1/LV1.
Now removing the mount point by using 'umount' command as shown below.
Command: umount /dev/vol1/LV1 /home/dir. 
FIGURE-14

Delete Partition:
Before deleting a partition, back up your data. All data is automatically deleted when a partition is deleted.
To delete partition, run the 'd' command in the fdisk command-line utility.
The partition is automatically selected if there are no other partitions on the disk. If the disk contains multiple partitions, select a partition by typing its number.
The terminal prints out a message confirming that the partition is deleted. Then run the 'w' command to write and save changes made to the disk.
Command: fdisk /dev/sda.
FIGURE-15










No comments:

Post a Comment

DC1 VULNHUB.

DC-1 Penetration Testing Lab Overview. Objective: Gain experience in penetration testing through a purposely built vulnerable lab. Target Au...