Wednesday, July 10, 2013

Find KVM Vm Ip address

Let assume we have a Virtual Machine running (started).
The VM is connected to the network thru a bridge ethernet.
We need to know its IP address, how?

  • Ping all the ip address of the network.
    • If IP exist ARP will add a line to the ARP table with IP and MAC address.
  • Compare the MAC address saved in the KVM XML with all the MAC-IP saved inside ARP table.
    • When Match print the IP address
How to call: script_name <number\name of running VM>

example: Windows



for end in {1..254}

  ping $network$end -c2 >/dev/null 2>/dev/null &

# Wait all ping to end

for kvm_addr in `virsh dumpxml $1 | \

                 awk 'BEGIN {FS="\47"} /mac address/ {print $2}'`

  for arp_addr in `arp | awk '/ether/ {print $3"-"$1}'`
    if [ "$kvm_addr" == ${arp_addr:0:17} ];
      echo "Match: " $arp_addr

Sunday, December 16, 2012

Migrate physical PC to Oracle Virtualbox

Some day ago we had to return back a old desktop computer. In this computer we had software and configuration we rarely use. We decide to don’t clone the PC in another physical computer because didn’t use this desktop very often. We decide to create a virtual machine to start when needed. We could creare a new VM with a fresh Windows XP installation, but install and configure all the software we had was too long. So we decide to clone the physical machine.

There are two easy way to clone a physical machine in a virtual machine for free. Those two method create a copy of your hard-drive to use with you favorite VM software: Microsoft Virtual PC, VMware, Oracle Virtualbox (with some edit with KVM, XEN too)

The first one is Disk2vhd from Microsoft. This sofware works only on Microsoft OS, and need a running Microsoft OS to work. You can use the computer you want to clone to run the program an save the images on another hard-drive (network, usb). I think you can connect the hard drive you want to clone to another pc and clone it, but I never tried.
I tried to use this method, but the program wasn't able to start on the Desktop. It worked fine with my laptop.

The second one is WmWare Converter
I don’t know much about this software. I know it’s free but I had to give them my email and accept something to use it so I quit before start.

I decided to use a third way. It (should) works with all kind of OS.

What we need:
Second computer
Big hard-drive.
usb-key or cd-rom with a live version of Linux

Step 1 Clone the hard drive

We need to clone the hard drive, to do that I used Linux command DD. It’s a very powerful and  dangerous command. It can clone hard drive, cd-rom or just file byte to byte.
Can be dangerous because if you set wrong the source and the target you might overwrite the data you want to clone.

the command is: dd if=(source) of=(target)

DD will clone every single byte of your hard drive, even the free space, so it will create a image as big as your hard drive. So be sure you are going to have a usb or network hardrive big enough to contain this image.

I started a live distro on the pc i wanted to clone.
Opened a terminal
and used DD.

dd if=/dev/sda of=/<path>/clone_sda.img

I didn’t had a usb hardrive so I decided to copy over SSH:


dd if=/dev/sda | ssh username@backupserver "dd of=/<path>/clone_sda.img"

Step 2 Create VDI virtual hard drive

This and next step need Virtualbox.

Pretty straightforward:

VBoxManage convertfromraw clone_sda.img clone_sda.vdi --format VDI

* Step 1 and 2 can be pipelined, bu you need some extrasteps check

Step 3 Compress VDI virtual hard drive

To compress the VDI image you have to “wipe” all free space.
When you delete a file in your harddrive you don’t overwrite the data of your file with ZEROES but you just “forgot” you you put the data. 
The compression just trows away all the zeroes we have in our hardrive. So we must wipe the free space before launch the program.


After the wipe the harddrive: 

VBoxManage modifyvdi <hd_path> compact



Tuesday, March 13, 2012

Migrate from Virtualbox to KVM

Some days ago I decided to migrate some of my Oracle Virtualbox test VM on a KVM server.

Why? easy, KVM\QEMU is better Oracle Virtualbox for a lot of reason. Probably this link will be enough to convince you.

Have a computer with virtualbox make everything easier but I wanted to do everything without virtuabox. (Link with virtuabox)

So we have to converto VDI images into somethink KVM can read. I used a simple ".RAW" images. This step is easy, and you can find tons of post online:

Edit 16/12/12: Anyway this will make a .RAW as big as the dimension assigned to your virtual hard drive.

I started with my VirtualBox VM Debian6 test server, where I usually try new stuff before put it on a real PC or anyway in the main VM.
Convert the hard drive from VDI to RAW, I used the RAW image inside a KVM VM and everything worked fine.

Windows XP:
After conver the VDI image and put the RAW inside a KVM VM i had this issue:

This blue screen arrived at the first second third and ALL boot after the conversion....
I searched and I found something.
Talking on IRC I realized the problem was the hard drive controller.
What we have to do? touch a bit WinXp registry... how to do it easily?
I found 2 solutions:
  1. With virtualbox: This "patch". Run it before your turn off the last time your virtualbox machine before the migration. I will say to XP to accept all HD chipset (or something similar)
  2. Without virtualbox: HIREN's boot cd or as a lot of SysAdmin know it... Ass saver! Simply boot micro XP goes on the tools and find "Fix Hard disk controller" under Registry. And DONE!

I found another problem, something I really though I wasn't to solve it fast, read a lot of stuff, like reinstall windows over the existing one or make a brand new VM.
I didn't want! so I pass more time searching than the time I needed to reinstall windows! But I Found it

Fist the problem (link):

So what happened? I haven't only moved my VM from Virtualbox to KVM on the same machine but I moved it from an AMD Athlon x2 to two different PC (to test) an AMD phenon x6 and Intel SB i5. On the AMD I haven't had this problem, but on the Intel I had! So I had problem because the processor type isn't the same.

Thanks to this AWESOME GUY we have a solution for this problem too....

I just start my VM XP in safe mode and edit the register like he wrote

And here we are! true you won't have reconized the CPU on hardware manager on Windows, but who care it's Windows! :D

Hope this post will help somebody.
Have a nice day!

Monday, July 18, 2011

Linux Kiosk

Some days ago I saw this video on youtube (Linux Kiosk Intro) I thought it was a great idea! Sometime we would like to give to an user the possibility to surf the net but nothing else, or anyway you need a computer ONLY to use the same program and nothing else! (Can be a program to sell tickets, or to see what table are busy in your restaurant and what they order at this table, or whatever)
Usually you buy a computer, then you usually pay for a Windows OS and you put the program on this system.
What's wrong with that?
  • You pay for a computer probably too powerfull for your needs.
  • You pay for an OS you probably you will not use for real
  • Maybe you must pay for some software to lock windows options to have a better security.
How can we solve that?
We can use Linux! Linux is free, well supported, and you can control everything you want! And with this method you don't need a powerful computer. A 10year old computer could be powerful enough.

How can we use Linux?
That's my solution! (The solution on the youtube video is good, my way is only different!)


I used ubuntu x64 11.04
Like you I used only basic system then i installed (with all dependencies):
  • Xorg is the graphics environment you need to start every GUI programs, there are other programs but I barely know that one so I used this.
  • Chromium-browser is the program I want to run, it's a version of Chrome browser, but you can use Firefox, or a special program like a python program you made.
  • Slim is a desktop-independent graphical login manager for X11
Then I set slim like that:
PS: my username is "media"

#Path, X server and arguments (if needed)
# Note: -xauth $authfile is automatically appended
default_path /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
default_xserver /usr/bin/X11/X
xserver_arguments -nolisten tcp

# Commands for halt, login, etc.
halt_cmd /sbin/shutdown -h now
reboot_cmd /sbin/shutdown -r now
console_cmd /usr/bin/xterm -C -fg white -bg black +sb -T "Console login" -e /bin/sh -c "/bin/cat /etc/; exec /bin/login"
#suspend_cmd /usr/sbin/suspend

# Full path to the xauth binary
xauth_path /usr/bin/X11/xauth

# Xauth file for server
authfile /var/run/slim.auth

# Activate numlock when slim starts. Valid values: on|off
# numlock on

# Hide the mouse cursor (note: does not work with some WMs).
# Valid values: true|false
# hidecursor false

# This command is executed after a succesful login.
# you can place the %session and %theme variables
# to handle launching of specific commands in .xinitrc
# depending of chosen session and slim theme
# NOTE: if your system does not have bash you need
# to adjust the command according to your preferred shell,
# i.e. for freebsd use:
# login_cmd exec /bin/sh - ~/.xinitrc %session
[b]login_cmd exec /bin/bash -login /etc/X11/Xsession chromium-browser[/b]

# Commands executed when starting and exiting a session.
# They can be used for registering a X11 session with
# sessreg. You can use the %user variable
# sessionstart_cmd    some command
# sessionstop_cmd    some command

# Start in daemon mode. Valid values: yes | no
# Note that this can be overriden by the command line
# options "-d" and "-nodaemon"
# daemon    yes

# Available sessions (first one is the default).
# The current chosen session name is replaced in the login_cmd
# above, so your login command can handle different sessions.
# see the xinitrc.sample file shipped with slim sources
sessions default,startxfce4,openbox,ion3,icewm,wmaker,blackbox,awesome

# Executed when pressing F11 (requires imagemagick)
screenshot_cmd scrot /root/slim.png

# welcome message. Available variables: %host, %domain
welcome_msg Welcome to %host

# Session message. Prepended to the session name when pressing F1
# session_msg Session:

# shutdown / reboot messages
shutdown_msg The system is halting...
reboot_msg The system is rebooting...

# default user, leave blank or remove this line
# for avoid pre-loading the username.
[b]default_user media[/b]

# Focus the password field on start when default_user is set
# Set to "yes" to enable this feature
#focus_password no

# Automatically login the default user (without entering
# the password. Set to "yes" to enable this feature
[b]auto_login yes[/b]

# current theme, use comma separated list to specify a set to
# randomly choose from
current_theme debian-spacefun

# Lock file
lockfile /var/run/slim.lock

# Log file
logfile /var/log/slim.log


# NOTE: if your system does not have bash you need
# to adjust the command according to your preferred shell,
# i.e. for freebsd use:
# login_cmd exec /bin/sh - ~/.xinitrc %session
login_cmd exec /bin/bash -login /etc/X11/Xsession chromium-browser

# default user, leave blank or remove this line
# for avoid pre-loading the username.
default_user media

# Automatically login the default user (without entering
# the password. Set to "yes" to enable this feature
auto_login yes

You can leave %session on login_cmd if you change the possible sessions in sessions.

And it works not bad at all, maybe i have to fix some stuff with mouse and size of the windows, and last if I close the last tab it goes back to login page, but i don't need openbox!