How to setup a private network for virtual guests in KVM

Posted by on August 20, 2008 under Uncategorized | Be the First to Comment

There is sparse documentation on how to setup a virtual network that is private (as in just between the virtual guests) on a single VM Host.  I tried a myriad of methods and none worked for me.  So I tested a few theories of my own, and found something… a method that is ridiculously obvious and easy to implement.  The trick is to simply create a fake interface on your host to bind your VM’s NIC.  I put these steps together to save you from wasting the same amount of time I did.

(note - One of the alternatives would be to use VDE (Virtual Distributed Ethernet), but that is still evolving (KVM-72 just added it native!).  VDE is slow performance wise, but has some interesting uses, such as extending your virtual switch to remote locations)

Ubuntu 8.04.1 64bit Server updated to 2008.08.01 w/ KVM-72

Step 1 - add a fake interface to bind your virtual machine’s network card, by editing the interfaces config file.  I open it to edit in nano:

nano /etc/network/interfaces

and add the following 2 lines at the top:

auto pb1
iface pb1 inet manual

I named it pb1 for ‘private bridge’.  Call it whatever you want.  And restart your network stack (/etc/init.d/networking restart)

Step 2
- create the startup script for this interface:

nano /etc/qemu-pb1-ifup

with the following in your statup script:

#!/bin/sh
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
sudo /usr/sbin/brctl addif pb1 $1
sleep2

Your done with the private bridge config.

Step 3 - To connect two (or more) VMs to that private interface, just start each VM with similar network settings… reference the same script file and use the same vLAN reference.  They are highlighted in my example KVM startup command below:

qemu-system-x86_64 \
-name SQL09 \
-vnc :32 \
-m 1024 \
-smp 1 \
-net nic,vlan=21,macaddr=aa:00:50:50:50:32,model=e1000 \
-net tap,vlan=21,ifname=tapSQL09,script=/etc/qemu-pb1-ifup \
-drive file=sql09root.qcow2,if=ide,index=0,media=disk,boot=on \
-std-vga \
-usb -usbdevice tablet \
-localtime \
-daemonize

Of course, use whatever vLAN you wish.

You are done!  I am going to update my KVM installation articles to include this, always adding a private interface, it is so easy. You could add multiple private interfaces if you wanted.

Uses:
This is great for setting up a completely isolated network of virtual guests.  However, if you need to connect out for Internet access, it is easy enough to multi-home one of your systems, and connect to a bridged interface on your Host.  For me, I like to run my favorite little firewall PFSense in a VM, multi-homed to both the private bridge and a normal bridge (shared with the Host).  For my laptop, I also include a third interface for the built in QEMU user networking (since I can’t bridge the wireless).

Works like a champ.

I didn’t find anyone else who documented this method.  If you have another working solution, please share.  This method is working great in our production and test environments.

 

Read more of this article »

How to setup a private network for virtual guests in KVM

Posted by on August 19, 2008 under Uncategorized | Be the First to Comment

There are quite a few ways to setup a private, virtual network that is just between virtual guests on a single VM Host.  We’ll explore one very simple way here.  One of the alternatives would be to use VDE (Virtual Distributed Ethernet), but I’ll wait until KVM-73, which will make VDE setup even easier.  For now, let look at creating a private bridge interface:

Read more of this article »

How to image a disk over the network

Posted by on August 12, 2008 under Uncategorized | Be the First to Comment

http://www.cyberciti.biz/tips/howto-copy-compressed-drive-image-over-network.html

 

 

 

 

Copy hard disk or partition image to another system using a network and netcat (nc)

Posted By vivek On August 12, 2007 @ 10:35 am In Backup, CentOS, Data recovery, Debian Linux, File system, FreeBSD, Gentoo Linux, Howto, RedHat/Fedora Linux, Suse Linux, Sys admin, Tips, Ubuntu Linux | 12 Comments

[1]

netcat utility (nc command) considered as TCP/IP swiss army knife. It reads and writes data across network connections, using TCP or UDP protocol. It is designed to be a reliable “back-end” tool that can be used directly or easily driven by other programs and scripts. At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.

I also install the netcat package for administering a network and you’d like to use its debugging and network exploration capabilities.

One my favorite usage is to migrating data between two server hard drives using netcat over a network. It is very easy to copy complete drive image from one server to another.

You can also use ssh for the same purpose, but encryption adds its own overheads. This is tried and trusted method (hat tip to [2] karl) .

Make sure you have [3] backup of all important data.

Install netcat

It is possible that nc may not be installed by default under Redhat / CentOS / Debian Linux.

Insall nc under Redhat / CentOS / Fedora Linux

Use yum command as follows:
# yum install nc
Output:

Loading "installonlyn" pluginLoading "rhnplugin" pluginSetting up Install ProcessSetting up repositoriesrhel-x86_64-server-vt-5   100% |=========================| 1.2 kB    00:00rhel-x86_64-server-5      100% |=========================| 1.2 kB    00:00Reading repository metadata in from local filesParsing package install argumentsResolving Dependencies--> Populating transaction set with selected packages. Please wait.---> Downloading header for nc to pack into transaction set.nc-1.84-10.fc6.x86_64.rpm 100% |=========================| 6.9 kB    00:00---> Package nc.x86_64 0:1.84-10.fc6 set to be updated--> Running transaction check

Dependencies Resolved

============================================================================= Package                 Arch       Version          Repository        Size=============================================================================Installing: nc                      x86_64     1.84-10.fc6      rhel-x86_64-server-5   56 k

Transaction Summary=============================================================================Install      1 Package(s)Update       0 Package(s)Remove       0 Package(s)         

Total download size: 56 kIs this ok [y/N]: yDownloading Packages:(1/1): nc-1.84-10.fc6.x86 100% |=========================|  56 kB    00:00Running Transaction TestFinished Transaction TestTransaction Test SucceededRunning Transaction  Installing: nc                           ######################### [1/1] 

Installed: nc.x86_64 0:1.84-10.fc6Complete!

Debian / Ubuntu Linux netcat installation

Simply use [4] apt-get command:
$ sudo apt-get install netcat

WARNING! These examples may result into data loss, ensure there are good backups before doing this, as using command wrong way can be dangerous.

How do I use netcat to copy hard disk image?

Our sample setup

-----------------------HostA // 192.168.1.1------------------------           sda        NETWORK           sdb------------------------HostB // 192.168.1.2-------------------------

Your task is copy HostA /dev/sda to HostB’s /dev/sdb using netcat command. First login as root user

Command to type on hostB (receiving end ~ write image mode)

You need to open port on hostB using netcat, enter :
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb
Where,

  • -p 2222 : Specifies the source port nc should use, subject to privilege restrictions and availability. Make sure port 2222 is not used by another process.
  • -l : Used to specify that nc should listen for an incoming connection rather than initiate a connection to a remote host.
  • bzip2 -d : Compresses image using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. This will speed up network transfer ( -d : force decompression mode)
  • dd of=/dev/sda : /dev/sda is your hard disk. You can also specify partition such as /dev/sda1

Command to type on hostA (send data over a network ~ read image mode)

Now all you have to do is start copying image. Again login as root and enter:
# bzip2 -c /dev/sda | netcat hostA 2222
OR use IP address:
# bzip2 -c /dev/sda | netcat 192.168.1.1 2222

This process takes its own time.

A note about latest netcat version 1.84-10 and above

If you are using latest nc / netcat version above syntax will generate an error. It is an error to use -l option in conjunction with the -p, -s, or -z options. Additionally, any timeouts specified with the -w option are ignored. So use nc command as follows.

On hostA, enter:
# nc -l 2222 > /dev/sdb
On hostB, enter:
# nc hostA 2222< /dev/sda
OR
# nc 192.168.1.1 2222< /dev/sda

Using a second machine (hostB), connect to the listening nc process at 2222 (hostA), feeding it the file (/dev/sda)which is to be transferred. You can use bzip2 as follows.
On hostA, enter:
# nc -l 2222 | bzip2 -d > /dev/sdb
On hostB, enter:
# bzip2 -c /dev/sda | nc 192.168.1.1 2222

Further readings

How do I improve performance?

As suggested by anonymous user:

You should definitely use bs=16M or something like that. Otherwise, the copy will take forever. Copying a 300 GB hard drive over a 1 Gbps cross-over cable took about 1 1/2 hours or so using bs=16M Without this option, the same thing would have taken about 7 hours.

In short use command as follows:
# netcat -p 2222 -l |bzip2 -d | dd of=/dev/sdb bs=16M

Updated for accuracy.


Article printed from nixCraft: http://www.cyberciti.biz/tips

URL to article: http://www.cyberciti.biz/tips/howto-copy-compressed-drive-image-over-network.html

URLs in this post:
[1] Image: http://www.cyberciti.biz/tips/category/networking/
[2] karl: http://www.meme.com/
[3] backup of all important data: http://www.cyberciti.biz/tips/perform-backups-for-the-linux-operating-system.html
[4] apt-get command: http://www.cyberciti.biz/tips/linux-debian-package-management-cheat-sheet.html
[5] Linux port scanning with netcat (nc) command: http://www.cyberciti.biz/faq/linux-port-scanning/
[6] nmap command for port scanning: http://www.cyberciti.biz/tips/linux-scanning-network-for-open-ports.html
[7] A good collection of netcat command usage: http://www.terminally-incoherent.com/blog/2007/08/07/few-useful-netcat-tricks/
[8] Another good netcat disk image howto: http://www.novell.com/coolsolutions/feature/19486.html

 

Read more of this article »

USB device pass through to Virtual Machines

Posted by on under Uncategorized | Be the First to Comment

Like any good hypervisor, KVM will support USB pass-through. There are just a few simple steps to make it happen. My example will also addresses an issue with Ubuntu/ Debian, and you may need to make a similar adjustment on other distributions ( I think most require the change).

To use USB:

Start your Virtual Machine, and then connect to the VM Monitor
In the VM Monitor, issue the following command:

info usbhost

Now you will see a listing of all the available USB devices, very similar to the command ‘lsusb’ you can run from the host

Device 5.2, speed 1.5 Mb/s
    Class 00: USB device 045e:0040, Microsoft 3-Button Mouse with IntelliEye(TM)
Device 3.2, speed 12 Mb/s
    Class 00: USB device 0bda:8150, Linksys USB LAN Adapter
Device 1.3, speed 480 Mb/s
    Class 00: USB device 0457:0151, USB Mass Storage Device

Find the one in the list that you want to add to your running guest.  In my example above, I want to add the Mass Storage Device.  Note the hexadecimal code for the device, and type the following:

usb_add host:0457:0151

The 0457:0151 matches the code for the device I want to add.

You’re Done!
Your guest OS will now recognize the USB device in it’s normal fashion, as though you had just plugged it in.

Troubleshooting:
Did you receive the following error?

Could not open /proc/bus/usb/devices

There is an easy fix. But some background…At some point in time, the location for the USB mount point changed from /proc/bus/usb to /dev/bus/usb. However, many application still reference the older location (such as usbview, and in this case QEMU). There is an easy work around though:
NOTE: This is reported as a problem for VMWare Server and Virtual Box as well - the fix is the same regardless.

You need to simply re-enable the old mount point. Using your favorite editor, open the file at: ‘/etc/init.d/mountdevsubfs.sh’

Look for the code after line 38 that is similar to this:

#
# Magic to make /proc/bus/usb work
#
#mkdir -p /dev/bus/usb/.usbfs
#domount usbfs “” /dev/bus/usb/.usbfs -obusmode=0700,devmode=0600,listm$
#ln -s .usbfs/devices /dev/bus/usb/devices
#mount –rbind /dev/bus/usb /proc/bus/usb

and those last four lines so that it looks like this:

#
# Magic to make /proc/bus/usb work
#
mkdir -p /dev/bus/usb/.usbfs
domount usbfs “” /dev/bus/usb/.usbfs -obusmode=0700,devmode=0600,listm$
ln -s .usbfs/devices /dev/bus/usb/devices
mount –rbind /dev/bus/usb /proc/bus/usb

Then run this command:

sudo /etc/init.d/mountdevsubfs.sh start

(Note again: The above fix is useful for other applications as well, including VMWare Server and Virtual Box)

Other info:
At you Host’s terminal prompt, type:

lsusb

This will provide a listing of all USB devices on your host system.

In the VMMonitor, type:

info usbhost

This will provide a listing of USB devices available to your VM Guest
Read more of this article »

The Americans - We’re going to smash them

Posted by on August 11, 2008 under Uncategorized | Be the First to Comment

Au Contraire!

What an exciting 3 minutes and 8 seconds… Jason Lezak bested the French anchor in the final leg of the Men’s 4 x 100m relay to set a new world record, and return the Gold medal to America in this event, of which the US had won 9 consecutive times since 1964, with a Silver in 2000, and a Bronze in 2004.

The Frenchman was quoted as saying, “The Americans? We’re going to smash them [ in the 4x100]. That’s what we came here for.”  Even the NBC announcers were saying that the French were going to win, and beat the world record (of which the other US relay team had set just the day before).

Ahh, the drama of the Olympics, when 32-year-old Lezak, the oldest member of the USA Men’s swim team had the fastest EVER split time to win by a fingertip, 8 one-hundreths of a second. 3:08.24 set a new world record.

My wife and I joke in the weeks before the Olympics about our own ‘training’, to stay up late to watch it on TV.  Well all that ‘training’ payed off last night, as we watched in amazement as Lezak came from behind in the final leg to win the Gold!  We were jumping up and down, cheering and high-fiving!  Now we are going to be up late every night for the next two weeks…

Congratulations to Team USA, and to Jason Lezak, Cullen Jones, Garrett Weber-Gale, and Michael Phelps for a well deserved victory!

Did you watch the race?  What did you think?

 

I try to keep my posts on IT issues, but the excitement of the Olympics last night has me briefly diverting.  I found this uncredited photo of the celebration:

Victory for the USA Men's Relay Team
Read more of this article »

USB device passthrough under KVM

Posted by on August 9, 2008 under Uncategorized | Be the First to Comment

Like any good hypervisor, KVM will support USB pass-through.  There are just a few simple steps to make it happen.  My example will also addresses an issue with Ubuntu/ Debian, and you may need to make a similar adjustment on other distributions.

First, make sure you enable USB when you start your VM.  An very basic example startup script might be:

qemu-system-x86_64 \
-usb \
-m 1024 \
-hda /home/dave/vm/myubuntu/mynewroot.qcow2

 

The key part being the -usb command.

Plug the USB device into your VM Host system.

At a terminal prompt, type:

lsusb

This will provide a listing of all USB devices on your host system.  It is necessary to run the command here, but I thought it would be a useful viewpoint.

Now, start your VM, and switch to the VM Monitor (Press Ctrl-Alt-2).  If the VM is already running, just switch to the VM Monitor.

In the VMMonitor, type:

info usbhost

You will likely receive the following error:

Could not open /proc/bus/usb/devices

 

There is an easy fix.  But some background…At some point in time, the location for the USB mount point changed from /proc/bus/usb  to /dev/bus/usb (probably because USB should be used out of userspace).  However, many application still reference the older location (such as usbview, and in this case QEMU).  There is an easy work around though:
NOTE: This is reported as a problem for VMWare Server and Virtual Box as well - the fix is the same reguardless.

You need to simply re-enable the old mount point.  Using your favorite editor, open the file at: ‘/etc/init.d/mountdevsubfs.sh’

Look for the code after line 38 that is similar to this:

#
# Magic to make /proc/bus/usb work
#
#mkdir -p /dev/bus/usb/.usbfs
#domount usbfs “” /dev/bus/usb/.usbfs -obusmode=0700,devmode=0600,listm$
#ln -s .usbfs/devices /dev/bus/usb/devices
#mount –rbind /dev/bus/usb /proc/bus/usb

and those last four lines so that it looks like this:

#
# Magic to make /proc/bus/usb work
#
mkdir -p /dev/bus/usb/.usbfs
domount usbfs “” /dev/bus/usb/.usbfs -obusmode=0700,devmode=0600,listm$
ln -s .usbfs/devices /dev/bus/usb/devices
mount –rbind /dev/bus/usb /proc/bus/usb

Then run this command:

sudo /etc/init.d/mountdevsubfs.sh start

(Note again: The above fix is useful for other applications as well, including VMWare Server and Virtual Box)

Now you can return to the VM Monitor:

info usbhost

Now you will see a listing of all the available USB devices.  Find the one in the list that you want to add to your running guest, and type the following:

usb_add host:04f9:0028

 

Read more of this article »

Secure File Sharing with SSHfs

Posted by on August 5, 2008 under Uncategorized | Be the First to Comment

SSHFS

OpenSSH is probably the most important IT administration tool - ever.  It allows for secure remote access to a system, with many tools built on top of the underlying encryption.  SSHfs is one of those tools.  SSHfs allows you to mount a remote file system, over the secure SSH protocol.  That is just plain cool.  Let me show you how you can be cool too:

My example is using Ubuntu 8.04.1 x64 Server w/ OpenSSH installed:

On the host, install the sshfs package.  On Ubuntu it is easy:

  sudo apt-get install sshfs

And load the kernel module:
 
  sudo modprobe fuse

Now from a remote system you can mount the host with:
  sudo sshfs <yoursshuser>@<ipaddressofHostServer>:/path/on/remote/system /local/mount/point

You now have secure access to the remote file system that you mounted in /local/mount/point.  I use this regularly.  I find it indispensable.  Do you?

Virtualization Note:

This is very useful in making it easy to copy ‘Raw’ virtual disks using the ‘cp’ command’s ’sparse’ options.  I’ll link to that upcoming article shortly.

Read more of this article »

VM Raw Files revisted, using SSHfs and Sparse options

Posted by on August 1, 2008 under Uncategorized | Be the First to Comment

What an ugly title…

I recommended using Raw files for your Virtual Machine’s virtual disks (see other post for the reasons), but it was brought to my attention, how do I copy these huge files?  Let’s look at that:

A Raw file is in a format that mimics a hard disk (block level).  It is supported in “sparse” format on file systems that support “holes” (NTFS, Ext3 included).  What does that mean?  When I create a Raw format file, let’s say of 100 GB in size, and then do a directory listing, it reports it as using 100GB of space:

dave@itpc11:~$ qemu-img create -f raw myvirtualfile.raw 100GB
dave@itpc11:~$ ls -l
-rw-r–r– 1 dave dave 107374182400 2008-08-01 15:26 myfile.raw

But wait?  My entire hard drive is only 40GB, how is this possible?  That is the sparse part of the file format, and the fact that Ext3 supports holes.  Run this command instead:

dave@itpc11:~$ ls -ls
0 -rw-r–r– 1 dave dave 107374182400 2008-08-01 15:26 myfile.raw

Note the first value of 0.  The file is actually taking up no space (currently) on the hard disk.

Here is the problem that was posed to me:

When you try to copy the file, either to another computer, or even on the same computer, the system creates a copy that is 100GB in size!

That is true.  You need to tell the application you are using to copy the file with “sparse” file recognition enabled.  Only then will it copy just the data in use.

There are two ways that I do this.  I either use “cp” or “rsync”.  They both have support for sparse files.  However, I find that cp is a bit more efficient for simple copies.  Rsync must be used when you only want to copy changed bits (or any of the many other reasons you would use Rsync).

Some examples:

To simple copy the file on the same computer from location A to location B:

  cp –sparse=always /mydir/myfile.raw /mydir/mycopy.raw

This will be a fast copy

What if I need to copy to another computer, and I want to connect to that computer over SSH?

Typically, you would simple use Rsync, as it supports sparse and SSH.  However, I found Rsync’s sparse copy to be very slow.  I prefer to mount the remote location by a cool technique using SSHFS.

SSHFS

OpenSSH is probably the most imporatant IT administration tool - ever.  SSHfs allows you to mount a remote file system, over the secure SSH protocol.  That is just plain cool.  We’ll use it here.

First, install the sshfs package.  On Ubuntu it is easy:

  sudo apt-get install sshfs

Now load the kernel module:
 
  sudo modprobe fuse

Now simply mount the remote file system to a local folder:

  sudo sshfs <yoursshuser>@<ipaddressofserver>:/path/on/remote/system /local/mount/point

Now you can use the cp command to copy the file to the remote server:
  sudo cp –sparse=always /mydir/myfile.raw /local/mount/point/mycopy.raw

 

Other methods:
As you begin to deal with more virtual disks, and they are ever increasing in size, you may want to convert them over to QCOW2 format.  Much like the VMDK format, they report there size based on the actual space they are using.  In the above example, if it was converted to a QCOW2 format, the file size would not longer be listed as 100GB:

dave@itpc11:~$
Read more of this article »