• About
  • Disclaimer
  • Privacy Policy
  • Contact Us
Wednesday, February 1, 2023
Tech Fashion Web
  • Home
  • Fashion
    • Footwear
    • Culture
  • Technology
    • Tech Solution
    • Website Design
    • Cyber Security
    • Software
  • Business
  • Digital Marketing
    • SEO
    • Social Media
  • Travel
  • Entertainment
    • Music
    • Celebrity
  • Health
    • Food
  • lifestyle
    • Home
  • More
    • Gaming
    • Gadgets
    • Education
    • Electronics
    • Gadgets
    • Reviews
    • Law
No Result
View All Result
  • Home
  • Fashion
    • Footwear
    • Culture
  • Technology
    • Tech Solution
    • Website Design
    • Cyber Security
    • Software
  • Business
  • Digital Marketing
    • SEO
    • Social Media
  • Travel
  • Entertainment
    • Music
    • Celebrity
  • Health
    • Food
  • lifestyle
    • Home
  • More
    • Gaming
    • Gadgets
    • Education
    • Electronics
    • Gadgets
    • Reviews
    • Law
No Result
View All Result
Tech Fashion web
No Result
View All Result

How to expand and shrink LVM volumes

by Tech Fashion
January 23, 2023
in Technology
0
325
SHARES
2.5k
VIEWS
Share on FacebookShare on Twitter


Image: Andreas Prott/Adobe Stock

The Logical Volume Manager commands in Linux are one of the most useful tools I’ve used for adding new disks or extending existing volumes. It’s easy to do this right away with no downtime or reboots, although I always recommend making a full backup of all data on existing volumes and taking a snapshot of affected virtual machines first.

SEE: 40+ open source and Linux terms you should know (Tech Republic Premium)

In this tutorial, I’ll show you how to extend and shrink a volume using LVM. Read for more information.

Jump to:

Extend a volume using LVM

I’m going to go through a series of steps I recently performed to extend the /home volume on a server that was set up with 1 GB of space. This was on a test server, but I decided to increase the volume by another 10 GB just to be on the safe side.

Since we use VMWare exclusively for our servers, this scenario takes place in a vSphere environment, but the steps involved are the same if you add a disk to a physical server.

First, I logged into the server, became root, and then examined the disks present by running:

lsblk

NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                      8:0    0  200G  0 disk

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

├─sda2                   8:2    0   49G  0 part

│ ├─rhel-root          253:0    0   20G  0 lvm  /

│ ├─rhel-swap          253:1    0    4G  0 lvm  [SWAP]

│ ├─rhel-home          253:3    0    1G  0 lvm  /home

│ ├─rhel-var           253:4    0   10G  0 lvm  /var

│ ├─rhel-var_log       253:5    0   20G  0 lvm  /var/log

│ ├─rhel-var_opt       253:6    0    3G  0 lvm  /var/opt

│ ├─rhel-var_tmp       253:7    0    3G  0 lvm  /var/tmp

│ ├─rhel-usr_local     253:8    0   10G  0 lvm  /usr/local

│ ├─rhel-var_log_audit 253:9    0 1012M  0 lvm  /var/log/audit

│ ├─rhel-opt           253:10   0   65G  0 lvm  /opt

│ ├─rhel-opt_fireeye   253:11   0    2G  0 lvm  /opt/fireeye

│ ├─rhel-besclient     253:12   0    8G  0 lvm  /var/opt/BESClient

│ ├─rhel-opt_encase    253:13   0    2G  0 lvm  /opt/encase

│ └─rhel-tmp           253:14   0   15G  0 lvm  /tmp

sdb                      8:16   0  850G  0 disk

└─appvg-vaplv          253:2    0  850G  0 lvm  /opt/vap

I checked /etc/fstab and found that /dev/mapper/rhel-home is the /home mount point. It resides on the sda2 partition on the physical sda disk and is an XFS file system unlike EXT2/3/4.

I then started running vgs to display the volume groups:

VG    #PV #LV #SN Attr   VSize    VFree

appvg   1   1   0 wz--n- <850.00g     0

rhel    2  14   0 wz--n-  198.99g 35.00g

This established that the volume group to work with was rhel.

I then logged into the vSphere console to add a new disk using these steps:

  1. Right-click on the virtual machine.
  2. Choose Edit Settings.
  3. Choose Add New Device.
  4. Choose Hard disk.
  5. Add a 10 GB drive as shown (Picture A).

Image A

Add a 10 GB drive as shown.
Image: Scott Matteson/TechRepublic.

Note that in certain cases it is possible to extend an existing volume — such as when there are no snapshots — and then add the expanded space to a volume group and map it to a logical volume. However, I consider this riskier than adding a new drive entirely, as there is a chance that human error could negatively impact existing data. It is much cleaner to work with a new disk and an empty associated partition. The operating system doesn’t care which disks it uses for logical volumes; that’s the beauty of LVM.

I then started running lsblk again:

NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                      8:0    0  200G  0 disk

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

├─sda2                   8:2    0   49G  0 part

│ ├─rhel-root          253:0    0   20G  0 lvm  /

│ ├─rhel-swap          253:1    0    4G  0 lvm  [SWAP]

│ ├─rhel-home          253:3    0    1G  0 lvm  /home

│ ├─rhel-var           253:4    0   10G  0 lvm  /var

│ ├─rhel-var_log       253:5    0   20G  0 lvm  /var/log

│ ├─rhel-var_opt       253:6    0    3G  0 lvm  /var/opt

│ ├─rhel-var_tmp       253:7    0    3G  0 lvm  /var/tmp

│ ├─rhel-usr_local     253:8    0   10G  0 lvm  /usr/local

│ ├─rhel-var_log_audit 253:9    0 1012M  0 lvm  /var/log/audit

│ ├─rhel-opt           253:10   0   65G  0 lvm  /opt

│ ├─rhel-opt_fireeye   253:11   0    2G  0 lvm  /opt/fireeye

│ ├─rhel-besclient     253:12   0    8G  0 lvm  /var/opt/BESClient

│ ├─rhel-opt_encase    253:13   0    2G  0 lvm  /opt/encase

│ └─rhel-tmp           253:14   0   15G  0 lvm  /tmp

sdb                      8:16   0  850G  0 disk

└─appvg-vaplv          253:2    0  850G  0 lvm  /opt/vap

sdc                      8:32   0   10G  0 disk

Here we see the new 10 GB drive added as sdc.

I then ran fdisk to add a primary partition to the disk so that the file system would recognize it:

fdisk -u -c /dev/sdc

I pressed n for new partition, p for primary, hit enter twice to confirm the defaults, then hit w to write the changes as follows:

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Device does not contain a recognized partition table

Building a new DOS disklabel with disk identifier 0xf9417ab7.

Command (m for help): 

I pressed n to create a new partition.

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p): 

I pressed p to create a primary partition.

Partition number 

(1-4, default 1):

I press enter to accept the default value of 1.

First sector (2048-20971519, default 2048):

I press Enter to accept the default value of 2048.

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):

I press Enter to accept the default value of 20971519.

Using default value 20971519

Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): 

I pressed w to write down the changes.

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

The next step was to create the physical volume of the new partition by running:

pvcreate /dev/sdc1

Physical volume "/dev/sdc1" successfully created.

Then I extended the rhel volume group with the new partition:

vknit rhel /dev/sdc1

Volume group "rhel" successfully extended 

Run vgs reconfirmed the addition of 10 GB:

VG    #PV #LV #SN Attr   VSize    VFree

appvg   1   1   0 wz--n- <850.00g     0

rhel    3  14   0 wz--n- <208.99g 45.00g

Extending the logical volume came next:

lvextend -l 100% FREE /dev/mapper/rhel-home

You can also use lvextend -r -L +10G /dev/mapper/rootvg-rhel_home to just add the 10 GB of space if you don’t want to allocate the full amount to a particular logical volume.

In my example, the following was returned:
Size of logical volume rhel/home changed from 1.00 GiB (256 extents) to 11.00 GiB (2816 extents).

Logical volume rhel/home successfully resized.
The last step was to grow the file system using this command:

xfs_growfs /dev/mapper/rhel-home

meta-data=/dev/mapper/rhel-home  isize=512    agcount=4, agsize=65536 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0 spinodes=0

data     =                       bsize=4096   blocks=262144, imaxpct=25

         =                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0 ftype=1

log      =internal               bsize=4096   blocks=2560, version=2

         =                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

data blocks changed from 262144 to 2883584

Run lsblk reconfirmed that the /home volume is now 11 GB:

NAME                   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                      8:0    0  200G  0 disk

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

├─sda2                   8:2    0   49G  0 part

│ ├─rhel-root          253:0    0   20G  0 lvm  /

│ ├─rhel-swap          253:1    0    4G  0 lvm  [SWAP]

│ ├─rhel-home          253:3    0   11G  0 lvm  /home

│ ├─rhel-var           253:4    0   10G  0 lvm  /var

│ ├─rhel-var_log       253:5    0   20G  0 lvm  /var/log

│ ├─rhel-var_opt       253:6    0    3G  0 lvm  /var/opt

│ ├─rhel-var_tmp       253:7    0    3G  0 lvm  /var/tmp

│ ├─rhel-usr_local     253:8    0   10G  0 lvm  /usr/local

│ ├─rhel-var_log_audit 253:9    0 1012M  0 lvm  /var/log/audit

│ ├─rhel-opt           253:10   0   65G  0 lvm  /opt

│ ├─rhel-opt_fireeye   253:11   0    2G  0 lvm  /opt/fireeye

│ ├─rhel-besclient     253:12   0    8G  0 lvm  /var/opt/BESClient

│ ├─rhel-opt_encase    253:13   0    2G  0 lvm  /opt/encase

│ └─rhel-tmp           253:14   0   15G  0 lvm  /tmp

sdb                      8:16   0  850G  0 disk

└─appvg-vaplv          253:2    0  850G  0 lvm  /opt/vap

sdc                      8:32   0   10G  0 disk

└─sdc1                   8:33   0   10G  0 part

  └─rhel-home          253:3    0   10G  0 lvm  /home

Shrink a volume with LVM

Decreasing a volume is very different from increasing a volume. There are a few considerations at stake.

XFS file systems cannot be minified; the only solution is to back up the data, delete the volume, create and configure a smaller volume and restore the data to it.

Conversely, EXT2/EXT3/EXT4 file systems can be shrunk, but there are also VMWare entanglements when it comes to shrinking disks. Snapshots can interfere with the process, requiring them to be deleted, and thick provisioned disks can be tricky to shrink. VMWare provides a good KB article here what Vmware Converter and/or the vmkfstools command entails.

For the purposes of this LVM article, I’ll focus on how to shrink an EXT2/3/4 volume – assuming the VMWare disk has been successfully shrunk, or assuming you want to keep the VMWare disk size as she is and divide the space over another logical volume.

Shrinking a root volume becomes the trickiest operation. That is the logical volume associated with /, and since this process requires a volume to be unmounted to shrink it, that is not possible. You must boot from a live CD where the operating system is not running. Maybe you should run vgchange -ay so that the Live CD recognizes the logical volumes so you can proceed.

Shrinking a non-root volume is much cleaner; in this case I would run to match the above examples umount /home to proceed.

The following steps should now apply to both root and non-root volumes.

Run a file system check on the LVM volume to reduce the risk of data corruption:

e2fsck -fy /dev/mapper/rhel-home

The -f flag forces the check to continue even if the file system doesn’t appear to be in trouble, and the -y answers yes to all questions.

The command below shrinks the /home logical volume from 11 GB to 5 GB. Note that you must have enough free space to run this:

resize2fs /dev/mapper/rhel-home 5G

Now you can reduce the size of the logical volume with the lvreduce command.

To shrink a logical volume to a desired size, use the -L flag and then specify the desired size:

lvreduce -L 5G /dev/mapper/rhel-home

To shrink a logical volume by a specific size, use the -L flag and a dash for the desired size:

lvreduce -L -5G /dev/mapper/rhel-home

Once you run this command, you will be prompted to confirm by answering ‘y’ and pressing Enter. You should now run resize2fs on the volume as shown:

resize2fs /dev/mapper/rhel-home

Next, you need to mount the volume. If this involves a root volume, simply reboot the system to load the primary Linux operating system. If it’s a non-root volume, run mount -a assuming you’ve configured it in /etc/fstab — otherwise specify the logical volume and where to mount to as shown:

mount /dev/mapper/rhel-home /home

Finally, check the space of the volume with the df command to confirm that it has shrunk as expected. I also recommend running another file system health check with this command:

e2fsck -fy /dev/mapper/rhel-home

A useful framework for configuring disks

Working with LVM to perform disk and volume operations is fast, easy, and reliable. I’ve performed on-the-fly disk management actions and have never encountered a technical issue let alone an OS crash or data loss.

For more information about LVM, visit the Complete beginner’s guide to LVM by linuxhandbook.com.



Source link

Share130Tweet81Share33
Previous Post

How to create OpenProject Forums

Next Post

How to create a GitHub Issue

Tech Fashion

Tech Fashion

Related Posts

Technology

How to install the Neo4j graph database on Ubuntu Server 22.04

by Tech Fashion
January 31, 2023
Technology

Top 5 IT Trends You Should Remember From 2022

by Tech Fashion
January 31, 2023
Technology

How to cap the number of users allowed to log into your Linux servers

by Tech Fashion
January 31, 2023
Technology

How to use measures to calculate profit margin in Power BI

by Tech Fashion
January 31, 2023
Next Post

How to create a GitHub Issue

Sam Smith dismisses 'crazy' fan theory that they are the same person as Adele

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Categories

  • Business (2)
  • Celebrity (10)
  • Culture (8)
  • Education (8)
  • Entertainment (1,773)
  • Fashion (12)
  • Food (7)
  • Footwear (7)
  • Health (6)
  • Lifestyle (14)
  • Music (6)
  • News (1,896)
  • Social Media (2)
  • Software (4)
  • Tech Solution (1)
  • Technology (1,676)
  • Travel (12)
  • Website Design (2)

Recent.

Ozzy Osbourne frankly admits that the touring days are over

February 1, 2023

How to install the Neo4j graph database on Ubuntu Server 22.04

January 31, 2023

Priscilla Presley is going to court to request changes to her daughter’s will

January 31, 2023
Tech Fashion Web

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Category

  • Business
  • Celebrity
  • Culture
  • Education
  • Entertainment
  • Fashion
  • Food
  • Footwear
  • Health
  • Lifestyle
  • Music
  • News
  • Social Media
  • Software
  • Tech Solution
  • Technology
  • Travel
  • Website Design

Recent Posts

  • Ozzy Osbourne frankly admits that the touring days are over February 1, 2023
  • How to install the Neo4j graph database on Ubuntu Server 22.04 January 31, 2023
  • Priscilla Presley is going to court to request changes to her daughter’s will January 31, 2023

Contact Us

    © 2021 techfashionweb.com . All rights reserved.

    No Result
    View All Result
    • Home
    • Fashion
      • Footwear
      • Culture
    • Technology
      • Tech Solution
      • Website Design
      • Cyber Security
      • Software
    • Business
    • Digital Marketing
      • SEO
      • Social Media
    • Travel
    • Entertainment
      • Music
      • Celebrity
    • Health
      • Food
    • lifestyle
      • Home
    • More
      • Gaming
      • Gadgets
      • Education
      • Electronics
      • Gadgets
      • Reviews
      • Law

    © 2021 techfashionweb.com . All rights reserved.