Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)

share|improve this question
1  
Why not just grow the / volume, isn't that the point of using LVM? –  jasonwryan Mar 13 '13 at 19:53
1  
@jasonwryan Why should I need to manually adjust how disk space is allocated? Isn't that the whole point of a filesystem? –  Nick ODell Mar 13 '13 at 19:56
    
@NickODell No. If one volume is too small to hold the data, then it's just too small. Overall capacity is a factor of volume size, not filesystem size. Logical volumes do make it easier to resize the mounted volumes and to merge disparate storage resources into a single volume, though. With LVM you should be able to do what jasonwryan is suggesting without even rebooting: make sure regular users are logged off (to free /home) umount /home lvreduce it (don't forget -r) re-mount it then do an lvextend on the root filesystem and you're done. If you want to merge you have to do an rsync. –  Bratchley Mar 21 '13 at 13:10
    
@JoelDavis If I require that all users to log out, is it relevant whether need to I reboot? –  Nick ODell Mar 21 '13 at 20:44
    
@NickODell Um...yeah? Most linux systems are servers, you should realize that will be people's assumption unless you say otherwise. –  Bratchley Mar 21 '13 at 23:57

3 Answers 3

up vote 5 down vote accepted

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:

grub boot centos Press A, then Space, then type "single" without the quotes. Hit enter.

You should see a prompt like this:

 [root@xxxxxxx /] # 

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 /)

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
Note: /home refused to unmount until I rebooted into single-user mode. –  Nick ODell Mar 14 '13 at 1:59
    
Note: It's referred to as vg_(machine name) and lv_home –  Nick ODell Mar 14 '13 at 2:19
    
Ok, I got it working. There were a few details missing from this guide though, so I'll post my full command history soon. –  Nick ODell Mar 14 '13 at 3:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.