Sunday, 13 April 2014

Adding and mounting a new disk in Linux to expand storage and house the contents of /home

So you want to add another disk to your Linux install. There could be several reasons for this, for instance running out of space on your existing setup, or wanting to split your system across multiple drives.

In my case I'd setup a new cPanel instance and wanted more capacity for the content in /home using a second disk. As such this post also explains how to move the existing /home content onto the newly added disk, such that everything continues to work like it did before.

I'll start by assuming you've added the new drive to your server, whether physically or as a new virtual disk, in your chosen hyper-visor.

Check that the system has detected the new disk by running :

    ls /dev/sd*

You'll see a list of all the physical disks detected, and the partitions on them. The first drive is /dev/sda, and if that has two partitions on it currently you'll see /dev/sda1 and /dev/sda2 listed. This increments for each drive, so a newly added second drive will be /dev/sdb.

You can check and ensure /dev/sdb has nothing on it already by running :

    fdisk -l

which will display the disk and partition details, and indicate that /dev/sdb doesn't contain a valid partition table. So lets get a partition added. Run :

    fdisk /dev/sdb

then at the prompt press n for New. You'll be asked what type of partition you want, so press p for primary. Now you'll be asked to select how much of the disk to use, these default to the first and last cylinders, so if you're using the entire disk you can simply press return on both and use the defaults. Finally at the last prompt press w to write the table to disk and exit. Note, if you quit without pressing w then nothing will be changed.

Now if you check in /dev you'll see a newly added /dev/sdb1 listed, but before we can use it we need to format it. To format it as ext3 run :

    /sbin/mkfs.ext3 -L /home /dev/sdb1

which will format the disk. The "-L /home" part of the command specifies the volume label, so if you're not using this disk for your /home folder then you might want to adjust it accordingly.

Now we want to move the existing contents of /home onto the newly formatted disk, but obviously new and old can't be /home at the same time so...

    mkdir /mnt/home
    mount /dev/sdb1 /mnt/home


to create a temporary mount point for the new drive and then mount it. Now copy the data with :

    cp -Ra /home/* /mnt/home

and then check in /mnt/home that the data is all there. Now we're finished with the temporary mount point so we can remove it with :

    umount /mnt/home
    rmdir /mnt/home


Before we can mount the new disk to its proper home we need to get rid of the existing /home folder, so let's rename it rather than delete it :

    mv /home /home-backup

which also means that if things go wrong you can switch back to the original data. Then, because we still need a folder to mount the new drive to :

    mkdir /home

To permanently mount the new disk we need to edit the fstab file, otherwise if we simply mount it then it won't reload next time the server is booted. Edit /etc/fstab and then add the following line :

    /dev/sdb1    /home    ext3    defaults    0 2

This then mounts the drive partition /dev/sdb1 to the folder /home using the ext3 file system and using the default options. The 0 relates to the archiving frequency (generally set to 0) and 2 controls the order that fsck checks for errors on the drive at boot time. You'd set that to 1 for the boot device (so not relevant here), or 2 for a subsequent drive. Many people blogging about this set it to 0 which disables checking, which I wouldn't recommend for anything other than a drive you were using short term.

Once that's saved we just need to test that it's working. You could reboot, but personally I prefer to run :

    mount -a

which will execute all of the entries listed in fstab (but do nothing where something's already mounted). If your new entry is OK then it should mount the new disk and make the contents accessible via /home, or if there's a problem then it will indicate where the issue is so you can edit /etc/fstab again to correct the issue.

So now you have a newly mounted drive, containing the same folder structure as before, but with the /home content stored on a separate drive. If you run :

    df

you'll see the newly added mount point and drive, and the disk usage / availability.

The above steps were tested on a CentOS 6.4 Hyper-V virtual machine, but should obviously work on most Linux installs.

Sunday, 30 March 2014

Migrating CentOS / cPanel from VMWare to Hyper-V

To migrate a CentOS / cPanel virtual machine from VMWare to Hyper-V there are a few steps you need to remember if you want to keep things easy.

The following steps assume you're attempting to migrate an existing CentOS installation, between host servers in the same network, eg you don't need or want to change any settings on the guest server like IP addresses etc. Simply convert, copy, import and power up.

Before shutting down the guest, it's advisable to uninstall the VMWare Tools from the server if they have been installed. While not always an issue, there are some reports of people having difficulties uninstalling it once the server has been migrated. Additionally, I'd suggest installing the Hyper-V Linux Integration Services prior to migration (current version at time of writing is 3.5 available here http://www.microsoft.com/en-gb/download/details.aspx?id=41554). This ensures when you power up the guest in Hyper-V that you don't have any issues with it recognising the network adaptor.

Linux installations hard code the MAC address of the NIC alongside the IP address details, which means if the MAC address changes (as it will by default when moving to another host), you'll find your network settings no longer work. Make a note of the current MAC address, either from the network settings in VMWare, or by looking in /etc/sysconfig/networking/devices/ifcfg-eth0.

Shutdown the server being migrated, and copy the files to the new Hyper-V server to convert them. You could convert them on the old server, but it's probably older and slower (hence you migrating) so moving them now saves time.

To convert the files I recommend using WinImage. Simply point it at the target vmdk file, tell it where to put the resulting .vhd file and its name, and it gets on with it. Note, some people report issues converting single large vmdk files (I don't know if this applies to WinImage), in which case the advise is to use VMWare Converter to split them into smaller files before the conversion. Once finished, copy the now converted .vhd file into the folder you'll create as part of the setup for the VM in the next step.

While the conversion is taking place create the new Hyper-V VM. At time of writing it will need to be a generation 1 virtual machine if you're running 2012 R2, as CentOS doesn't support generation 2 machines. Create the VM as normal, but when prompted to configure the HDD choose the option to Attach a virtual hard disk later.

Once configured, go into the Settings for the new VM. Newer versions of CentOS may be fine, but for 5.10 I found the NIC must be configured as a Legacy Network Adaptor, so remove the Network Adaptor that's there and then add a Legacy Network Adaptor instead. Within the Adaptor settings, go into the Advanced Features and statically assign the MAC address to the one you previously recorded from VMWare. Finally, select IDE Controller 0, select Hard Drive and click Add, then click Browse and find the .vhd file you converted earlier. Obviously if you have multiple drives then repeat this for each of them.

Once all this is done you can start the VM. You should find it starts successfully and works exactly the same as previously without the need to adjust anything on the server.