Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Our corporate standard is CentOS (which seems to prefer LVM), and I come from primarily a non-LVM background, so I'm treading new ground with this issue.

I inherited an older machine as a log-server and had to reinstall CentOS. Due to issues with EHCI on the motherboard (either via the normal install DVD or via LiveCD GUI install), the only thing I could find that would install CentOS 6.3 was a LiveCD text install. The LiveCD text install would not allow me to make my own partition settings on the disk; it only installs LVM and gave me bad space allocation for a log server...

[mpenning@someHost ~]$ cat /etc/issue
CentOS release 6.3 (Final)
Kernel \r on an \m

[mpenning@someHost ~]$ df -m
Filesystem           1M-blocks      Used Available Use% Mounted on
/dev/mapper/vg_livecd-lv_root
                         50397      2167     47719   5% /
tmpfs                     1850         0      1850   0% /dev/shm
/dev/md127p1               485        65       395  15% /boot
/dev/mapper/vg_livecd-lv_home
                        227001       188    215283   1% /home
[mpenning@someHost ~]$ sudo lvm vgdisplay
  --- Volume group ---
  VG Name               vg_livecd
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               278.96 GiB
  PE Size               4.00 MiB
  Total PE              71415
  Alloc PE / Size       71415 / 278.96 GiB
  Free  PE / Size       0 / 0
  VG UUID               8qywNz-cmKv-RxAH-WcsV-2nEA-sGg3-fcZ6BU

[mpenning@someHost ~]$

Two questions:

  1. If I need to increase / to have 257398 1-M blocks and decrease /home to 20000 1-M blocks, can I do this without rebooting and if so, what is the specific command sequence to do this?
  2. If I want to add a dedicated /var partition, how should I do it without a complete reinstall?
share|improve this question

2 Answers 2

up vote 5 down vote accepted

If I need to increase / to have 257398 1-M blocks and decrease /home to 20000 1-M blocks, can I do this without rebooting and if so, what is the specific command sequence to do this?

You have roughly 280GB of space available, of which 50GB has been allocated to '/' and 230GB has been allocated to /home. Because this represents all of your available space, you're going to have to free something up first. Here are a few options:

If you haven't placed any data in /home, it's easiest to just discard the existing filesystem and re-create it with the appropriate size:

  • Unmount /home:

    # umount /home
    
  • Reduce the size of the logical volume (LV) to 20GB:

    # lvreduce -L 20G /dev/vg_livecd/lv_home
    
  • Recreate the /home filesystem:

    # mke2fs -j -m0 /dev/vg_livecd/lv_home
    
  • And remount /home:

    # mount /home
    

It is possible to shrink /home, rather than discarding it, but this can be a little trickier:

  • Unmount /home:

    # umount /home
    
  • fsck the filesystem. This is probably required by the next step:

    # fsck /dev/vg_livecd/lv_home
    
  • Resize the filesystem:

    # resize2fs /dev/vg_livecd/lv_home 19G
    
  • Reduce the LV:

    # lvreduce -L 20G /dev/vg_livecd/lv_home
    

You'll note that I've reduced the filesystem a little bit more than the LV; this is because I can never remember if they count things the same way so I try to leave a little wiggle room.

In either case, at this point you now have roughly 210GB free space in your volume group. Now you can increase the size of your root filesystem:

  • Increase the size of the root LV:

    # lvresize -L260G /dev/vg_livecd/lv_root
    
  • Increase the size of the filesystem:

    # resize2fs /dev/vg_livecd/lv_root
    

Note that you may need to use a number slightly less than 260G due to the rounding I'm using in this answer. Also, recent versions of the LVM utilities include the ability to resize the filesystem for you, but I've been doing this since before those options were available...so I can't claim to know exactly how they operate.

If I want to add a dedicated /var partition, how should I do it without a complete reinstall?

First, you'll probably want to do this in single-user mode, otherwise running processes will probably have files open in /var making it hard to replace. Having said that...

Once again you're going to need free space. If instead of using up all your free space in the previous step you leave a few GB free, you can do this:

  • Create a new logical volume:

    # lvcreate -n lv_var -L 10G /dev/vg0
    
  • Create a new filesystem:

    # mke2fs -j -m0 /dev/vg_livecd/lv_var
    
  • Edit /etc/fstab to mount the new LV on /var by adding the following line:

    /dev/vg_livcde/lv_var /var ext4 defaults 1 2
    

    But don't mount it yet!

  • Mount the new filesystem somewhere convenient:

    # mount /dev/vg_livecd/lv_var /mnt
    
  • Copy over the contents of /var:

    # rsync -a /var/ /mnt/
    
  • Rename /var to /var.old and create a new directory:

    # mv /var /var.old
    # mkdir /var
    
  • Mount your new LV on /var:

    # umount /mnt # mount /var

  • And continue booting. When you are satisfied everything is working correctly you can delete /var.old.

share|improve this answer
    
+1, Thoughtful answer, I will try it out tomorrow –  Mike Pennington Aug 24 '12 at 5:27

Considering:

  • the ugly VG name
  • the bad space allocation for your use case and the burden of the procedure detailed by larsks
  • the fact that you'd have to do all this again in case you had to reinstall

AFAIC, I'd try to netinstall/kickstart to get the desired configuration in one shot and in a reproductible way.

My 2cts

share|improve this answer

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.