User Tools

Site Tools


virtualisation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
virtualisation [2016/07/05 10:57]
mstraub [Resizing a (system) disk]
virtualisation [2020/02/13 11:47]
mantis [vmbuilder]
Line 1: Line 1:
 +====== Installing ======
 +Check cpu for virtualization support
 +
 +<code bash>
 +egrep '​vmx|svm'​ /​proc/​cpuinfo
 +</​code>​
 +
 +Install (on Ubuntu 9.10)
 +
 +<code bash>
 +sudo apt-get install kvm libvirt-bin virt-manager virt-viewer
 +sudo adduser $USER libvirtd ​
 +# make sure current user is in libvirtd group. log out and back in if necessary
 +</​code>​
 +
 +
 +====== Creating a VM ======
 +
 +===== With Ubuntu already installed =====
 +
 +==== virt-install ====
 +
 +https://​www.server-world.info/​en/​note?​os=Ubuntu_18.04&​p=kvm&​f=2
 +
 +
 +==== vmbuilder ====
 +
 +If you want to create an **Ubuntu** VM you can use a shortcut (found here: https://​help.ubuntu.com/​8.04/​serverguide/​C/​ubuntu-vm-builder.html). Install
 +
 +<code bash>
 +sudo apt-get install ubuntu-vm-builder
 +</​code>​
 +and create, for example, a Lucid VM by typing:
 +
 +<code bash>
 +sudo ubuntu-vm-builder kvm lucid --addpkg vim --addpkg openssh-server --addpkg man --mem 1024
 +</​code>​
 +
 +Available hypervisors:​ esxi xen kvm vmw6 vmserver
 +
 +Available OS suites: dapper gutsy hardy intrepid jaunty karmic
 +
 +You can specify disk, swap and memory size, network settings (use the --help flag for more details).
 +
 +
 +Oh, and a tiny but important detail: default user and password on the new KVM: **ubuntu/​ubuntu** ​
 +You might want to add additional users, then remove the default user.
 +
 +
 +=== Lucid KVM, 64bit ===
 +<code bash>
 +sudo vmbuilder kvm ubuntu --arch=amd64 --suite=lucid --addpkg vim --addpkg openssh-server --addpkg man --mem 1024 --rootsize=2048
 +</​code>​
 +
 +=== Trusty KVM, 64bit ===
 +There is currently a bug which causes KVM building to fail under trusty (14.04). It is easily fixed by explicitly adding the parameter "​--addpkg linux-image-generic"​ to the command: ​
 +
 +<code bash>
 +ubuntu-vm-builder kvm trusty --rootsize=5120 --addpkg vim --addpkg openssh-server --addpkg man --addpkg ntp --addpkg linux-image-generic --arch=amd64
 +</​code>​
 +
 +
 +=== Bionic ===
 +
 +<code bash>
 +sudo virt-builder ubuntu-18.04 --format qcow2  -o ubuntu1804.qcow2 --root-password password:​xxx --install ​ net-tools,​ntp,​openssh-server
 +
 +</​code>​
 +
 +===== Empty disk Images=====
 +
 +<code bash>
 +qemu-img create -f raw disk.img 2G  # file format can be raw,​qcow2,​vmdk ​ etc.. 
 +</​code>​
 +
 +
 +====== Snapshots ======
 +
 +===== The obvious way =====
 +
 + * Create a tar.gz from your vmware image
 +
 +<code bash>
 +tar xfvzS my_image.tar.gz ​ my_image.img ​
 +</​code>​
 +
 +===== Real Snapshots with rebasing =====
 +
 +Note: For some reason this does not work with the virt-manager in Ubuntu 9.10 . Although it works fine when starting the image from the commandline using "kvm -hda my_image.qcow2"​
 +
 + * Create a base image. Raw images dont work, you need a **qcow2** image for that. To convert raw to qcow2 use 
 +
 +<code bash>
 +kvm-img convert -c -f raw -O qcow2 my_image.img my_converted_image_base.qcow2 ​
 +</​code>​
 +
 + * Create a new sliver ​
 +
 +<code bash>
 +kvm-img create -f qcow2 -b current_image.qcow2 new_image.qcow2 ​
 +</​code>​
 +
 + * Make the Virtual Machine start with new_image.qcow2 as disk 
 +
 +
 +
 +====== Howtos ======
 +
 +===== Mounting a KVM Image on the host =====
 +
 +<code bash>
 +sudo modprobe nbd # load kernel module
 +sudo qemu-nbd -c /dev/nbd0 windows2003.img # connect image to device nbd0
 +sudo cfdisk -P s /dev/nbd0 # show partitions
 +sudo mount /dev/nbd0p1 /​mnt/​my_mountpoint # mount partition 1
 +
 +#disconnect image
 +sudo umount /​mnt/​my_moutnpoint
 +sudo qemu-nbd -d /dev/nbd0
 +</​code>​
 +
 +
 +===== Passing an USB Device to the Guest =====
 +
 +Written for Ubuntu 10.04 .
 +
 + * First you need to reconfigure apparmor, so it allows read-write access to the hosts USB - Devices. This can be done by
 +
 +
 +<code bash>
 +vim /​etc/​apparmor.d/​abstractions/​libvirt-qemu
 +
 +# Now find the  line: /​sys/​devices/​*/​*/​usb[0-9]*/​** r
 +# and replace it by: /​sys/​devices/​*/​*/​usb[0-9]*/​** rw   
 +# but be aware that this grants read/write access to the hosts usb devices. so be careful (or add a more specific rule if you want to)
 +
 +
 +sudo service apparmor reload ​ # reload apparmor
 +</​code>​
 +
 + * Now open virt-manager --> Add Hardware --> Physical Host Device --> USB --> Select Device . Done. 
 +
 +
 +
 +===== Resizing a (system) disk =====
 +First turn off the VM and resize the disk image on the VM host
 +
 +<code bash>
 +sudo qemu-img resize image.qcow2 +10G
 +</​code>​
 +
 +Make sure virt-manager knows about the changed disk size: go to "​Storage"​ and Refresh (not sure if really necessary, but cant hurt..)
 +
 +Next boot the VM,  log in and increase the size of the partition. This is done by deleting the old partition and re-creating it with the same start, but a larger size: 
 +
 +<code bash>
 +sudo fdisk /dev/vda
 +#sudo fdisk --compatibility=dos /dev/vda # in case you want to start at sector 63
 +# or on newer fdisk versions: ​
 +#sudo fdisk -c=dos /dev/vda # in case you want to start at sector 63
 +
 +p # print partition table and memorize start sector etc.
 +d # delete partition 1
 +n # new partition, primary, 1, start sector = same as sector from print , end sector = default -> maximum size)
 +w # write new partition table
 +
 +sudo reboot # reboot immediately to make sure nothing bad happens..
 +
 +</​code>​
 +
 +Now you can resize the file system:
 +
 +<code bash >
 +sudo resize2fs /​dev/​vda1 ​
 +</​code>​
 +
 +===== Reclaim unused space =====
 +Try out the approach mentioned here: 
 +
 +https://​www.jamescoyle.net/​how-to/​323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk
 +
 +
 +===== Convert qcow2 to VMWare =====
 +<code bash>
 +qemu-img convert -f qcow2 file.qcow2 -O vmdk file.vmdk
 +</​code>​
 +
 +
 +====== Troubleshooting ======
 +
 +===== Windows install hangs at "​Windows is starting up" =====
 +
 +When the Windows Install asks you to hit F6 for additional drivers, you hit F5 instead. The install will give you the option to select Standard PC or other. Pick Standard PC and the install will continue. ​
 +
 +===== Improving Disk Performance =====
 +
 +**WARNING:​** This seems to be no longer true for Ubuntu 10.04. At least I am not seeing any performance gain (quite the contrary).
 +
 +One effective (but '''​unsafe'''​) way to improve disk-performance is to enable the writeback cache in the disk-driver: ​
 +
 +<code bash>
 +sudo virsh stop guest_name
 +sudo virsh edit guest_name ​  # <-- add cache='​writeback'​ in the driver section
 +sudo virsh start guest_name
 +</​code>​
 +
 +The relevant part in the xml (in /​etc/​libvirt/​qemu) should look like this: 
 +
 +<code bash>
 +<disk type='​file'​ device='​disk'>​
 +    <driver name='​qemu'​ type='​qcow2'​ cache='​writeback'/>​
 +    <source file='/​var/​vms/​vm2/​ubuntu-kvm/​tmphAUcOB.qcow2'/>​
 +    <target dev='​hda'​ bus='​ide'/>​
 +</​disk>​
 +</​code>​
 +
 +For details see  http://​www.ducea.com/​2011/​07/​06/​howto-improve-io-performance-for-kvm-guests/​
  
virtualisation.txt ยท Last modified: 2020/02/13 11:47 by mantis