April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

LVM

LVM

First LVM Setup

1) fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054):
Using default value 13054

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

fdisk /dev/sdc

fdisk /dev/sdd

fdisk /dev/sde

2) Now we prepare our new partitions for LVM:

[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
Writing physical volume data to disk “/dev/sdb1”
Physical volume “/dev/sdb1” successfully created
Writing physical volume data to disk “/dev/sdc1”
Physical volume “/dev/sdc1” successfully created
Writing physical volume data to disk “/dev/sdd1”
Physical volume “/dev/sdd1” successfully created

Revert this last action

[root@localhost ~]# pvremove /dev/sdb1 /dev/sdc1 /dev/sdd1
Labels on physical volume “/dev/sdb1” successfully wiped
Labels on physical volume “/dev/sdc1” successfully wiped
Labels on physical volume “/dev/sdd1” successfully wiped

To create again

pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1

[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1
Writing physical volume data to disk “/dev/sdb1”
Physical volume “/dev/sdb1” successfully created
Writing physical volume data to disk “/dev/sdc1”
Physical volume “/dev/sdc1” successfully created
Writing physical volume data to disk “/dev/sdd1”
Physical volume “/dev/sdd1” successfully created

Let dispay the current physical volumes

[root@localhost ~]# pvdisplay

3 ) create our volume group datashare

vgcreate datashare /dev/sdb1 /dev/sdc1 /dev/sdd1

[root@localhost ~]# vgcreate datashare /dev/sdb1 /dev/sdc1 /dev/sdd1
Volume group “datashare” successfully created

Dispaly the volume groups
volume groups: [root@localhost ~]# vgdisplay

Dispaly the volume groups

[root@localhost ~]# vgscan
Reading all physical volumes. This may take a while…
Found volume group “datashare” using metadata type lvm2
Found volume group “VolGroup” using metadata type lvm2

let’s rename our volumegroup datashare into fileshare:

vgrename datashare fileshare

[root@localhost ~]# vgrename datashare fileshare
Volume group “datashare” successfully renamed to “fileshare”

[root@localhost ~]# vgscan
Reading all physical volumes. This may take a while…
Found volume group “fileshare” using metadata type lvm2
Found volume group “VolGroup” using metadata type lvm2

let’s delete our volume group fileshare

vgremove fileshare

[root@localhost ~]# vgremove fileshare
Volume group “fileshare” successfully removed

[root@localhost ~]# vgcreate datashare /dev/sdb1 /dev/sdc1 /dev/sdd1
Volume group “datashare” successfully created

4) Next we create our logical volumes storage (40GB), backup (5GB), and data (1GB) in the volume group datashare

lvcreate –name storage –size 30G datashare

lvcreate –name backup –size 20G datashare

lvcreate –name data –size 10G datashare

[root@localhost ~]# lvcreate –name storage –size 30G datashare
Logical volume “storage” created
[root@localhost ~]# lvcreate –name backup –size 20G datashare
Logical volume “backup” created
[root@localhost ~]# lvcreate –name data –size 10G datashare
Logical volume “data” created

check using command : lvdisplay

[root@localhost ~]# lvdisplay
/dev/datashare/storage
/dev/datashare/backup
/dev/datashare/data

[root@localhost ~]# lvscan
ACTIVE ‘/dev/datashare/storage’ [30.00 GiB] inherit
ACTIVE ‘/dev/datashare/backup’ [20.00 GiB] inherit
ACTIVE ‘/dev/datashare/data’ [10.00 GiB] inherit
ACTIVE ‘/dev/VolGroup/lv_root’ [48.62 GiB] inherit
ACTIVE ‘/dev/VolGroup/lv_home’ [50.63 GiB] inherit
ACTIVE ‘/dev/VolGroup/lv_swap’ [1.97 GiB] inherit

5) Next let’s delete the logical volume data

lvremove /dev/datashare/data

[root@localhost ~]# lvremove /dev/datashare/data
Do you really want to remove active logical volume data? [y/n]: y
Logical volume “data” successfully removed

Now let create logical volume servershare

lvcreate –name servershare –size 5G datashare

[root@localhost ~]# lvcreate –name servershare –size 5G datashare
Logical volume “servershare” created

Now let’s enlarge servershare to 10 G

lvextend -L10G /dev/datashare/servershare

root@localhost ~]# lvextend -L10G /dev/datashare/servershare
Extending logical volume servershare to 10.00 GiB
Logical volume servershare successfully resized

Let’s shrink it to 5GB Again

lvreduce -L5G /dev/datashare/servershare

root@localhost ~]# lvreduce -L5G /dev/datashare/servershare
WARNING: Reducing active logical volume to 5.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce servershare? [y/n]: Y
Reducing logical volume servershare to 5.00 GiB
Logical volume servershare successfully resized

6) Lets format and mount the partition

[root@localhost ~]# mkfs.ext4 /dev/datashare/storage
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1966080 inodes, 7864320 blocks
393216 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
240 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

same case

mkfs.ext4 /dev/datashare/backup
mkfs.ext4 /dev/datashare/servershare

7) mkdir storage backup servershare in /

mount the logical volumes

mount /dev/datashare/backup /backup
mount /dev/datashare/servershare /servershare
mount /dev/datashare/storage /storage

[root@localhost /]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 52G 2.6G 47G 6% /
tmpfs tmpfs 528M 0 528M 0% /dev/shm
/dev/sda1 ext4 508M 32M 450M 7% /boot
/dev/mapper/VolGroup-lv_home
ext4 54G 189M 51G 1% /home
/dev/mapper/datashare-backup
ext4 22G 181M 20G 1% /backup
/dev/mapper/datashare-servershare
ext4 5.3G 145M 4.9G 3% /servershare
/dev/mapper/datashare-storage
ext4 32G 181M 30G 1% /storage

Add in the fstab to in order mount after the reboot

8) Resize Logical Volumes And Their Filesystems

Now let’s enlarge share from 30GB to 40GB:

[root@localhost /]# lvextend -L50G /dev/datashare/storage
Extending logical volume storage to 50.00 GiB
Logical volume storage successfully resized

we have enlarged only storage, but not the ext4 filesystem on share.

we can do it by

[root@localhost /]# e2fsck -f /dev/datashare/storage
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/datashare/storage: 11/1966080 files (0.0% non-contiguous), 167409/7864320 blocks

Make a note of the total amount of blocks (7864320) we need it when we shrink storage later on.
[root@localhost /]# resize2fs /dev/datashare/storage
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/datashare/storage to 13107200 (4k) blocks.
The filesystem on /dev/datashare/storage is now 13107200 blocks long.

Let is mount we will have 50 GB

[root@localhost /]# mount /dev/datashare/storage /storage/

[root@localhost /]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 52G 2.6G 47G 6% /
tmpfs tmpfs 528M 0 528M 0% /dev/shm
/dev/sda1 ext4 508M 32M 450M 7% /boot
/dev/mapper/VolGroup-lv_home
ext4 54G 189M 51G 1% /home
/dev/mapper/datashare-backup
ext4 22G 181M 20G 1% /backup
/dev/mapper/datashare-servershare
ext4 5.3G 145M 4.9G 3% /servershare
/dev/mapper/datashare-storage
ext4 53G 189M 50G 1% /storage

Lets is Umount the backup and reduce the disk space 14G

umount /backup/

/dev/mapper/datashare-backup

e2fsck -f /dev/datashare/backup

root@localhost /]# e2fsck -f /dev/datashare/backup
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/datashare/backup: 11/1310720 files (0.0% non-contiguous), 126289/5242880 blocks

[root@localhost /]# resize2fs /dev/datashare/backup 3242880
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/datashare/backup to 3242880 (4k) blocks.
The filesystem on /dev/datashare/backup is now 3242880 blocks long.

[root@localhost /]# lvreduce -L14G /dev/datashare/backup
WARNING: Reducing active logical volume to 14.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce backup? [y/n]: y
Reducing logical volume backup to 15.00 GiB
Logical volume backup successfully resized
[root@localhost /]# mount /dev/datashare/backup /backup/
[root@localhost /]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 52G 2.6G 47G 6% /
tmpfs tmpfs 528M 0 528M 0% /dev/shm
/dev/sda1 ext4 508M 32M 450M 7% /boot
/dev/mapper/VolGroup-lv_home
ext4 54G 189M 51G 1% /home
/dev/mapper/datashare-servershare
ext4 5.3G 145M 4.9G 3% /servershare
/dev/mapper/datashare-storage
ext4 53G 189M 50G 1% /storage
/dev/mapper/datashare-backup
ext4 14G 177M 13G 2% /backup

2 comments to LVM

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>