Thursday, 30 January 2014

Importing a VirtualBox VM into RHEV

After several trials and errors I finally found a "working" method of importing VirtualBox (and probably VMWare and other Virtualizations solutions) VM into a RHEV system.

What you need:

1 - Lots of disk space! At least twice the size of your VM's disks.
2 - A VirtualBox installation and the qemu-img program (available in your distro or from www.qemu.org).
3 - A libvirtxml template file (you can get the one I use from here).
4 - The virt-v2v program installed in one the RHEV machines (or the RHEV manager machine).

So how do you do it?

Convert VirtualBox disks to QCOW format (if someone knows how to easily convert them directly to raw please let know!).
You could be tempted to convert the disks directly from VMDK format to RAW with qemu-img, unfortunately I found this is not possible since qemu gives an "unsupported format error".

Anyway, just follow these steps:


Go to the VirtualBox File Menu and select: "Virtual Media Manager".


Then select the first disk and right-click it and select "Copy...", or press the Copy Button:



Check if the right file was selected and press Next:


Select the QCOW (QEMU Copy-On-Write) format and press Next:


Select the output filename and path, finally press Copy:


Wait for it to finish... 


Once it is done you still need to convert them to the RAW format. This is where qemu-img comes in. Issue the following command to convert a disk to the RAW format:

qemu-img convert disk1.qcow -O raw disk1.img

Repeat for every disk you need converted.


Now, for the real fun part! 

Open the libvirtxml file you got from above and edit the following fields:

<name>VMNAME</name>
<uuid>b8d7388a-bbf2-db3a-e962-12346789abcd</uuid>
<memory>2097152</memory>
 <type arch='x86' machine='pc'>hvm</type>

The vmname should be changed to whatever you want, this will be the name that appears on the imported VM.
The uuid is not important at this point since it will be changed once the import is completed.
You can also change the memory size (in KB).
The arch property should be set to x86 or x86_64 if it has a 32bit or a 64bit guest OS.


In the disk section you need to change a few things.
<disk type='file' device='disk'>
    <driver name='qemu' type='raw' cache='none' io='threads'/>
    <source file='disk1.img'/>
    <source dev='/dev/sda'/>
    <target dev='sda' bus='scsi'/>

<shareable/>      
</disk>



Change the source file  and target dev properties to what you need.  In this example the disk1.img is the first disk (sda). 
The example XML file I provide has two disks, you can remove the section for the second one or you can add more disk sections if you need to.


Once you have the file properly set up it's time to import it into RHEV.

Use the following command:

virt-v2v -i libvirtxml -o rhev -osd IP:/DIR --network rhevm VM_libvirt.xml

The IP and DIR should be the IP, or hostname, of the export domain and DIR the NFS dir path (I'm using an NFS export domain, you can change this to reflect you export domain).
Check the documentation if you have any doubts.


After a couple of minutes or hours, depending on the size of the disks and your network speed, you should have the machine in your export domain. From there you can import it to RHEV.

I have used this method on both windows and Linux machines and it seems to work fine!

Wednesday, 15 January 2014

Windows - Batch file to clean old Log Files

After dealing with several hundred GB of log files from Tomcat, I created a simple cleanup script for old log files. This script is meant to be run by a Schedule Task in Windows so its configuration is very simple. It only needs to know three things: Path to search, Type of Files and the number of days to keep the files.

You can download the batch file here, or copy paste the following text:


echo off
REM ********************************
REM *       USER VARIABLES         *

REM CLEAN FILES OLDER THAN NUM_DAYS
set NUM_DAYS=7

REM PATH WHERE TO RECURSIVELY SEARCH FOR FILES
set SRCDIR=C:\LOGS

REM FILE EXTENSION TO LOOK FOR (*.* FOR ALL FILES)
set EXT="*.log"
REM ********************************


echo "Cleaning Files older than %NUM_DAYS% on %SRCDIR%"

forfiles /p "%SRCDIR%" /s /m %EXT% /D -%NUM_DAYS% /C "cmd /c del @path"

echo "DONE!"
echo on


Super simple! It requires the forfiles command to be installed (it already comes Windows Server 2003 and onwards, Windows Vista and onwards. Windows XP users have to install it manually)

You can used this in conjunction with a schedule task to make the script run automatically at any given day/time. 

You can also modified it to move the files, zip them or event rename them.
I suppose this can also be done with Windows Powershell, but since I'm more into Linux I don't have a clue on how to do it. If you have a similar Powershell script please share it, or let me know where I can download it from!