1&1 Cloud Server to LVM on a live server

1&1 Cloud Servers are a good balance between a dedicated server and shared hosting. One of the best attractions to such a service is the ability to grow the spec of your server over time as your needs grow.

This is the situation I was in when I upgraded my server for an extra 1GB of Ram and 100GB disk. The CentOS images provided by 1&1 is not built on top of LVM and therefore cannot take advantage of being able to grow your partition without data loss and while the system is live - something that LVM provides.


I needed to grow my full /var partition without data lose and convert /var to LVM to handle growth later, these are my notes to achieve this task...

Note: I am not a Linux expert, these are my notes and may not be complete. Please use this as guidance.

 

Preparing the Partition

Following steps 1,2,3, 5 and 6 (not 4!) from the 1&1 support document for assigning additional disk space you just purchased

We now have a new empty partition waiting to be used.

 

Setting up LVM

Convert the new partition to LVM
{codecitation style="brush: bash;"}#fdisk /dev/hda
Command (m for help): t
Partition number (1-4): 7 (whatever your partition number is)
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Unknown)
Command (m for help): w{/codecitation}

 

Initialize LVM

{codecitation style="brush: bash;"}vgscan{/codecitation}
Make the new partition into a Physical Volume.
{codecitation style="brush: bash;"}pvcreate /dev/hda7{/codecitation}
Create a new volume group.
{codecitation style="brush: bash;"}vgcreate vg0 /dev/hda7{/codecitation}
Create a logical volume to hold the new /var and setting its size, for example below 90GB
{codecitation style="brush: bash;"}lvcreate -L 90G vg0{/codecitation}
Make a filesystem in the logical volume
{codecitation style="brush: bash;"}mke2fs -j /dev/vg0/lvol0{/codecitation}
You now have a new partition formatted in ext3 waiting for data

 

Moving /var to your new partition

There are two ways to do this, request 1&1 to put your server into rescue mode so that you may mount the two partitions without them being in use, or kill as many processes on your server as possible and mount your new LVM partition as follows...
{codecitation style="brush: bash;"}mount /dev/vg0/lvol0 /mnt{/codecitation}
Once you have both partitions mounted you want to sync them (in this example im syncing the already mounted /var to my new LVM partition that I just mounted above to /mnt...
{codecitation style="brush: bash;"}rsync -r -a -v /var/ /mnt/{/codecitation}
You now have two partitions with the same /var data, one is your original /var the second the new partition you built on top of LVM

 

Setting your new /var at boot

Now we need to tell your server that /var has now moved to the new LVM partition.
Edit your fstab file
{codecitation style="brush: bash;"}vi /mnt/etc/fstab{/codecitation}
And change the value of the current /var to something like the following

From:
{codecitation style="brush: bash;"}/dev/hda6    /var       xfs     defaults,usrquota        0 2{/codecitation}
To:
{codecitation style="brush: bash;"}/dev/vg0/lvol0    /var        ext3    defaults        0 2{/codecitation}
Note the change in file system, from xfs to ext3! This caught me the first time, but was easily fixed in rescue mode

 

Rebuild ramdisk

Run mkinitrd to recrete your initial ramdisk so that LVM is loaded at boot
{codecitation style="brush: bash;"}/sbin/mkinitrd -f /boot/initrd-2.6.18-164.11.1.el5.img 2.6.18-164.11.1.el5{/codecitation}
You now have your server ready to mount the new /var on boot, cross your fingers, pray to the penguin gods and reboot!

 

Convert the old partition to LVM

Now your booted into your system using the new /var on LVM you need to convert the old /var partition to LVM
{codecitation style="brush: bash;"}#fdisk /dev/hda
Command (m for help): t
Partition number (1-4): 4 (whatever your old /var partition number is)
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Unknown)
Command (m for help): w{/codecitation}

And finally extend your LVM partition to use the old /var partition you just converted to LVM above by following this CentOS document...
Expanding a LVM logical volume

Tagged under tech