Menu

1/3/25

LVM in linux

[root@GLPI ~]# lsblk

NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS

sda           8:0    0   20G  0 disk

├─sda1        8:1    0    1G  0 part /boot

└─sda2        8:2    0   19G  0 part

  ├─cs-root 253:0    0   17G  0 lvm  /

  └─cs-swap 253:1    0    2G  0 lvm  [SWAP]

sdb           8:16   0    3G  0 disk

sdc           8:32   0    3G  0 disk

sr0          11:0    1 1024M  0 rom

Step 1: Install LVM2

First, ensure that the LVM2 package is installed on your system. This package provides the necessary tools for managing LVM.

sudo dnf install lvm2

  • sudo: Runs the command with superuser privileges.
  • dnf: The package manager for CentOS Stream.
  • install lvm2: Installs the LVM2 package.

Step 2: Prepare Physical Volumes

You need to have one or more physical disks or partitions to create LVM. You can use existing partitions or create new ones. For this example, let's assume you have two disks: /dev/sdb and /dev/sdc.

Note: Make sure to replace /dev/sdb and /dev/sdc with your actual disk identifiers.

To create physical volumes, use the following command:

sudo pvcreate /dev/sdb /dev/sdc

  • pvcreate: This command initializes the specified disk(s) as physical volumes for LVM.
  • /dev/sdb /dev/sdc: The disks you are initializing.

Step 3: Create a Volume Group

Next, you need to create a volume group (VG) that will contain the physical volumes. You can name your volume group anything you like; in this example, we will call it vg01.

sudo vgcreate vg01 /dev/sdb /dev/sdc

  • vgcreate: This command creates a new volume group.
  • vg01: The name of the volume group you are creating.
  • /dev/sdb /dev/sdc: The physical volumes that will be included in this volume group.

Step 4: Create Logical Volumes

Now that you have a volume group, you can create logical volumes (LVs) within that group. For example, let's create a logical volume named lv01 with a size of 10GB.

sudo lvcreate -n lv01 -L 5G vg01

  • lvcreate: This command creates a new logical volume.
  • -n lv01: Specifies the name of the logical volume.
  • -L 5G: Specifies the size of the logical volume (10 gigabytes).
  • vg01: The volume group in which the logical volume will be created

Step 5: Format the Logical Volume

Before you can use the logical volume, you need to format it with a filesystem. For example, you can use the ext4 filesystem:

sudo mkfs.xfs /dev/vg01/lv01

  • mkfs.ext4: This command formats the logical volume with the ext4 filesystem.
  • /dev/vg01/lv01: The path to the logical volume you just created.

Step 6: Mount the Logical Volume

Now, you can mount the logical volume to a directory. First, create a mount point:

sudo mkdir /mnt/lv01

  • mkdir /mnt/lv01: Creates a directory where the logical volume will be mounted.Next, mount the logical volume:
sudo mount /dev/vg01/lv01 /mnt/lv01

  • mount: This command mounts the specified filesystem.
  • /dev/vg01/lv01: The logical volume you want to mount.
  • /mnt/lv01: The directory where you want to mount the logical volume.

Step 7: Verify the Setup

You can verify that the logical volume is mounted correctly by using the df command:

df -h

  • df -h: Displays the disk space usage in a human-readable format.

You should see an entry for /mnt/lv01 showing the size and usage of the logical volume.

Step 8: Make the Mount Permanent

To ensure that the logical volume is mounted automatically at boot, you need to add an entry to the /etc/fstab file. Open the file in a text editor:

sudo nano /etc/fstab

Add the following line at the end of the file:

/dev/vg01/lv01 /mnt/lv01 xfs defaults 0 0

  • This line specifies the device, mount point, filesystem type, mount options, and dump/pass options.

Save and exit the editor (in nano, press CTRL + X, then Y, and Enter).

Step 9: Reboot and Verify

Finally, reboot your system to verify that the logical volume mounts automatically:

sudo reboot

Step 1: Create Some Files in lv01

First, navigate to the mounted logical volume directory:

cd /mnt/lv01

Now, create some files. You can use the touch command to create empty files or echo to create files with content. Here are a few examples:

touch file1.txt 2echo "This is file 2" > file2.txt 3echo "This is file 3" > file3.txt

  • touch file1.txt: Creates an empty file named file1.txt.
  • echo "This is file 2" > file2.txt: Creates a file named file2.txt with the specified content.
  • echo "This is file 3" > file3.txt: Creates a file named file3.txt with the specified content.

Step 2: Verify the Files

You can list the files in the directory to verify that they were created successfully:

ls -l

You should see file1.txtfile2.txt, and file3.txt listed.

1. List Physical Volumes

To list all physical volumes (PVs) in your LVM setup, use the following command:

sudo pvs

2. List Volume Groups

To list all volume groups (VGs), use the following command:

sudo vgs

  • vgs: This command displays a summary of all volume groups, including their names, number of physical volumes, number of logical volumes, total size, and free space.

3. List Logical Volumes

To list all logical volumes (LVs), use the following command:

sudo lvs

4. Detailed Information

If you want more detailed information about each component, you can use the following commands:

  • For Physical Volumes:
  • sudo pvdisplay

  • For Volume Groups:
sudo vgdisplay

BACKUP

[root@localhost lv01]# yum install xfsdump -y
[root@localhost lv01]# xfsdump -f /tmp/backup.dump /mnt/lv01 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.12 (dump format 3.0) - type ^C for status and control ============================= dump label dialog ============================== please enter label for this dump session (timeout in 300 sec) -> backup session label entered: "backup" --------------------------------- end dialog --------------------------------- xfsdump: level 0 dump of localhost.localdomain:/mnt/lv01 xfsdump: dump date: Sun Jan 5 08:14:14 2025 xfsdump: session id: 27195896-3d59-4e1b-b8a3-03503b8ec022 xfsdump: session label: "backup" xfsdump: ino map phase 1: constructing initial dump list xfsdump: ino map phase 2: skipping (no pruning necessary) xfsdump: ino map phase 3: skipping (only one dump stream) xfsdump: ino map construction complete xfsdump: estimated dump size: 29952 bytes ============================= media label dialog ============================= please enter label for media in drive 0 (timeout in 300 sec) -> backup media label entered: "backup" --------------------------------- end dialog --------------------------------- xfsdump: creating dump session media file 0 (media 0, file 0) xfsdump: dumping ino map xfsdump: dumping directories xfsdump: dumping non-directory files xfsdump: ending media file xfsdump: media file size 24072 bytes xfsdump: dump size (non-dir files) : 1088 bytes xfsdump: dump complete: 51 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /tmp/backup.dump OK (success) xfsdump: Dump Status: SUCCESS

[root@localhost ~]# sudo umount /mnt/lv01

Remove:
[root@localhost ~]# lvremove /dev/vg01/lv01

Recreate:
[root@localhost ~]# lvcreate -L 4GB -n lv01 vg01

[root@localhost ~]# lvdisplay /dev/vg01/lv01 --- Logical volume --- LV Path /dev/vg01/lv01 LV Name lv01 VG Name vg01 LV UUID bYNzyI-RTSO-56md-hehM-68bp-dUP0-pDTU1m LV Write Access read/write LV Creation host, time localhost.localdomain, 2025-01-05 08:30:49 -0500 LV Status available # open 0 LV Size 4.00 GiB Current LE 1024 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:3

[root@localhost ~]# mkfs.xfs -f /dev/vg01/lv01 meta-data=/dev/vg01/lv01 isize=512 agcount=4, agsize=262144 blks = sectsz=4096 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 bigtime=1 inobtcount=1 nrext64=0 data = bsize=4096 blocks=1048576, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=16384, version=2 = sectsz=4096 sunit=1 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 Discarding blocks...Done.

REMOUNT:

[root@localhost ~]# mount /dev/vg01/lv01 /mnt/lv01

RESTORE & TEST

[root@localhost lv01]# xfsrestore -f /tmp/backup.dump /mnt/lv01 xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.12 (dump format 3.0) - type ^C for status and control xfsrestore: searching media for dump xfsrestore: examining media file 0 xfsrestore: dump description: xfsrestore: hostname: localhost.localdomain xfsrestore: mount point: /mnt/lv01 xfsrestore: volume: /dev/mapper/vg01-lv01 xfsrestore: session time: Sun Jan 5 08:14:14 2025 xfsrestore: level: 0 xfsrestore: session label: "backup" xfsrestore: media label: "backup" xfsrestore: file system id: 1ebf44a1-6d13-45f7-bbb5-1d9ae4e750ff xfsrestore: session id: 27195896-3d59-4e1b-b8a3-03503b8ec022 xfsrestore: media id: 962d280f-8c7f-433f-b11b-d4f2d41567c2 xfsrestore: using online session inventory xfsrestore: searching media for directory dump xfsrestore: reading directories xfsrestore: 1 directories and 3 entries processed xfsrestore: directory post-processing xfsrestore: restoring non-directory files xfsrestore: restore complete: 0 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /tmp/backup.dump OK (success) xfsrestore: Restore Status: SUCCESS [root@localhost lv01]# cd /mnt/lv01 [root@localhost lv01]# ls -l total 8 -rw-r--r--. 1 root root 0 Jan 5 07:44 file1.txt -rw-r--r--. 1 root root 15 Jan 5 07:44 file2.txt -rw-r--r--. 1 root root 15 Jan 5 07:45 file3.txt
























No comments:

Post a Comment