How do I combine two logical volumes that reside on a single physical volume in LVM?

I have two logical volumes, one is mounted as /, the other is mounted as /home.

I want to combine them, because the / volume is constantly running out of space. (Yes, I know what’s using all that space; no, I can’t remove it.)

I’m willing to take the machine down for a day or two to do it.

OS: Linux xxxxx 2.6.32-279.22.1.el6.x86_64 #1 SMP Wed Feb 6 03:10:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux (CentOS)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’m done, and the re-partition worked. However, since there are missing areas in both guides, I am going to summarize what I did. Please note that I have replaced my username with zzzz and the name of my computer with xxxxxxx. Also, all commands should be run as root unless otherwise noted.

First, I backed up:

tar -zcvf /media/e14be279-8c11-450a-b52c-1493cff3c632/xxxxxxx-home.tar.gz --exclude=/home/zzzz/Downloads /home

This creates a compressed backup of the /home directory, and puts it on my external drive. (Note that your external drive will have a different path.)

Now, you need to reboot into single-user mode.

reboot

When your computer turns on again, you need to look for a screen like this:
Booting Centos 2.6.32-279.22.1.el6.x86_64 in 3 seconds...

Press any key to interrupt that. You should see a screen like this:

How do I combine two logical volumes that reside on a single physical volume in LVM?
Press a, then Space, then type single. Hit enter.

You should see a prompt like this:

 [<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a4855554e7a42424242424242">[email protected]</a> /] #

Enter the following commands:
df -h

It should show you Size, Used, and Avail. We’re interested in Used.
Write down how much space is in use by your home volume. We’ll need that later.
umount /home
e2fsck -f /dev/mapper/vg_xxxxxxx-lv_home

Remember how much space your home volume used? Double that, and give it to this command. For example, I had 15 gigabytes used by my home directory, so I sized it to 30 gigabytes.
resize2fs /dev/mapper/vg_xxxxxxx-lv_home 30G

Warning: This next step is the one that can lose data. Make sure that you put in a size larger than the previous command. Add 5 Gigabytes to be sure.
lvresize -L 35G vg_xxxxxxx/lv_home

Tell the root partition to use all that space we just freed up.
lvresize -l +100%FREE vg_xxxxxxx/lv_root
resize2fs /dev/mapper/vg_xxxxxxx-lv_root

Copy your data to the root partition
mount -t ext4 /dev/mapper/vg_xxxxxxx-lv_home /mnt/home
ls /mnt/home
cp -a /mnt/home/. /home/

(By the way, that -a is important, because otherwise everything will be owned by root, and you don’t want that.)
Do a quick check that the data copied correctly:
ls -lR /home

Now we remove the home partition, and tell root to take the new space again.
umount /mnt/home
lvremove /dev/mapper/vg_xxxxxxx-lv_home
lvresize -l +100%FREE vg_xxxxxxx/lv_root
resize2fs /dev/mapper/vg_xxxxxxx-lv_root

At this point, I made a big mistake, one which you can learn from. I rebooted, and when the system came back up, it refused to mount my root partition as read-write because it couldn’t mount my /home partition. So, before we reboot, we type
nano /etc/fstab

One of those lines will tell the system to mount the /home partition. Put a # before it. Press Ctrl-O, Enter, Ctrl-X. Now you can reboot.

(Hypothetically, if you were to forget to edit fstab like I did, the command to mount your root partition is mount -n -o remount -t ext2 /dev/vg_xxxxxxxx/lv_root /)

Method 2

WARNING: Backup all critical data before doing anything!

You would need to move all the data from the /home partition to another partition, local partition or external drive or wherever other place you have enough space.

Remove the /home lv and then expand the / (root) lv

Copy the data back to /home directory

Edit 1

The answer above is assuming you don’t have enough space in the root partition to place the data in the home partition.

If this is not the case, just mount the home partition somewhere else and copy the data, for example:

umount /home
mount /dev/mapper/lv-home /mnt/home
cp -a /mnt/home/. /home/

Then just remove the home partition and expand the root partition

Method 3

You can do it without any external storage, running from /, but you will need to unmount /home.

This isn’t a particularly risky procedure if you’re careful, but nonetheless, the standard advice applies: make backups first, and test them.

I’ll use foo for the volume group name, and root and home as the logical volume names. This makes the logical volume paths foo/root and foo/home, and the device paths /dev/mapper/foo-root and /dev/mapper/foo-home. I assume that they are in the same volume group, otherwise this gets more complicated and is dependent on the physical volume layout of the two volume groups.

First, unmount /home and run e2fsck -f /dev/mapper/foo-home. Then shrink the filesystem first, and the logical volume accordingly. Run resize2fs /dev/mapper/foo-home NNNN where NNNN is the number of blocks you want to retain inside the volume (you can use kB or MB or GB instead with the suffix K, M or G). After shrinking the filesystem, shrink the logical volume with lvresize -L MMMM foo/home where MMMM is a number of megabytes (note that the default unit is different from resize2fs, but you can use the same suffixes K, M, G). Be very careful not to shrink the logical volume further than the filesystem — this is the one dangerous step in this procedure.

There is now unused room in the volume group. Run lvresize -l +100%FREE foo/root to make the root volume grab all the free space in the volume group. Then run resize2fs /dev/mapper/foo-root (with no size parameter) to enlarge the filesystem to span the whole logical volume; enlarging can be done while the volume is mounted.

You now have room to copy some files. If you don’t have enough room for everything, move as much as you can, then repeat the procedure.

Once you’ve copied all the files out of /home, you’re ready to destroy it. Verify that /home is empty (if you moved the files) or that all the files in /home have been copied to /root (if you copied the files). To destroy the now-unneeded logical volume, run lvremove foo/home. Finally, enlarge the root volume with lvresize -l 0%FREE foo/root and its filesystem with resize2fs /dev/mapper/foo-home.

Method 4

OK, I found myself in exactly the same position. It seems CentOS creates a small root volume by default and fills the remainder of your space with /home. I even tested my setup first in a VM but with the smaller disk CentOS didn’t create the home volume. I picked up on this problem before becoming space constrained tho’ and also figured how not to have to enter single user mode. As follows:

Get access to the machine, ssh is fine (assuming non-root user).

  1. cd / so you’re not in the home directory
  2. sudo -s we’ll be root for all of this
  3. lsof /home check that no one is using the home volume, shouldn’t be
  4. mv, tar, or similar the contents of home to somewhere else
  5. lvremove centos/home
  6. Edit /etc/fstab and comment out or remove the line for /home
  7. Move the contents of home that you backed up before back into /home – you are now at a safe position, a reboot here should be OK
  8. lvresize -l +100%FREE centos/root
  9. xfs_growfs /dev/centos/root on CentOS 7, others might use resize2fs
  10. df -h have you succeeded?

Hope this makes it easier for someone. It really is quite amazing what you can do live on filesystems these days.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments