Creation of a new VM

From Newroco Tech Docs
Revision as of 11:35, 28 November 2019 by Cristian.todosi (talk | contribs) (→‎virt-install)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

--DEPRECATED--

Use virt-install

NB being tidied, is out of date. Currently stresses vmbuilder as solution, which we are modifying to suit at https://github.com/newroco/vmbuilder

Related pages

Introduction

VM creation is a relatively straightforward process, but one that can be resource heavy and so disruptive to existing virtual machines. As such builds should normally be created on a dev server or local machine and deployed to target host(s) once ready.

Ubuntu VM

vm-builder (deprecated)

Install:

apt-get install python-vm-builder
Mods for 16.04 (awaiting fork)

If you run into an error from dpkg, related to the script giving the wrong response to a "should I overwrite this conf file" question, edit the related Python lib and rerun:

vi /usr/lib/python2.7/dist-packages/VMBuilder/plugins/ubuntu/dapper.py

Find the line containing "dist-upgrade" and change "-y" to "-s" (changing the command to test mode only)

self.run_in_target('apt-get', '-s', '--force-yes', 'dist-upgrade',

or

self.install_from_template('/etc/sudoers', 'sudoers')


If building 16.04 VMs as normal, you should modify the template file to use the new style Ethernet device naming:

vi /etc/vmbuilder/ubuntu/interfaces.tmpl

and change every instance of eth0 to ens3 (assumes no other changes - as the interfaces now named in order of enumeration, so an additional PCI device could change which is the one associated with a network device. Simpler, in general, to build the basic machine, gain access to it via SSH and modify it and its hardware definition from that point on.

Use the following line to create a new 16.04 VM on a 16.04 host, modifying as necessary to suit your network environment etc.:

vmbuilder kvm ubuntu --libvirt qemu:///system --debug -v --tmpfs=1280 --bridge=br0 --arch=amd64 --suite=xenial --flavour=virtual --mirror='http://gb.archive.ubuntu.com/ubuntu/' --components=main --rootpass=<yourinitialrootpass> -d /var/lib/libvirt/images/<machinename> -m 1024 --cpus=2 --rootsize=9182 --swapsize=4096 --hostname=<machinename> --ip=ip.add.re.ss --mask=255.255.255.0 --gw=gateway.ip.add.ress --dns=internal.dns.ser.ver --addpkg=vim --addpkg=openssh-server --addpkg=curl --addpkg=linux-image-generic

Note you cannot create a newer VM on an older host i.e. to create a 16.04 you must build on a 16.04 host. You can build older releases on a later host, and in general an older host will work with any compatible VM regardless of installed OS.

virt-builder

The virt-builder tool uses pre-built images which the command can then customise, depending on options added.

At time of writing the 16.04 LTS released version 1.32.2 has an issue caused by a bug in dhclient-script 06:13, 18 December 2016 (UTC)

Install:

apt-get install libguestfs-tools

! After creating the VM run on host

virsh autostart new_vm_name

virt-install

Virt-install is a command line tool for creating new KVM , Xen, or Linux container guests using the "libvirt" hypervisor management library

Using virt install to create VM, and a preseed.cfg file to automate the installer

Example:

virt-install usage example

Preseed_file

! After creating the VM run on host

virsh autostart new_vm_name

General notes

If you add additional packages beyond those in the example you may encounter critical errors referencing a device being out of space. This may mean the --tmpfs option isn't sufficient to install all the packages you've requested. The packages listed above are sufficient to get salt installed and so additional packages can be deployed with that or via SSH post boot.

Windows VM

Start with an iso file for your install. If you don't have one, create one:

dd if=/dev/dvd of=dvd.iso

Move this file to a location accessible to your KVM host, for example: ~


To install onto a single partition (eg for Windows 7/8 deploy)

  • instructions to be completed but long story short:
  1. Create a new blank disk: Creation_of_a_new_VM#Adding_a_second_hard_Disk
  2. Temporarily add that disk as a second disk on an existing VM
  3. Boot the VM, and format the second disk with a new single partition filling whole drive
  4. shut down temporary VM and remove second disk
  5. Store new disk in a new sub-folder of your images folder - eg /var/lib/kvm/newvm/newvm.qcow2
  6. Copy and modify an xml file for new VM:
  7. add option to boot from cdrom in xml
  8. add installation iso file as cd-rom in xml
  9. boot from cd-rom and install as normal


To create a new disk during installation

  • this avoids all the faff above, but if you use windows 7/8 it will create the annoying 350mb system partition, which is awkward for Kace system imaging and deployment

Install the necessary packages if not there already:

sudo apt-get install virtinst

Run a version of the following command. Note, this is for a w2k12r2 guest; adapt as necessary:

sudo mkdir /var/lib/kvm/w2k12r2-vanilla/ && sudo virt-install --connect qemu:///system -n w2k12r2-vanilla -r 4096 -f /var/lib/kvm/w2k12r2-vanilla/w2k12r2-vanilla.qcow2 -s 50 -c ~/win2k12r2.ISO --vnc --noautoconsole --os-type windows --os-variant win2k8

[nb win2k8 is the latest acceptable os-variant in June 2015]

Options include:

  • -n Domain name of VM
  • -r Amount of RAM in MB
    • 1 GB is probably the minimum required, although the machine will install and boot with half that
  • -f The intended location of the VM file
  • -s The size of the VM's disk in GB
    • Windows Server 2008 is listed as requiring a minimum of 10GB, however 40gb is more realistic once updates have started to swell the plaguey WINSXS folder (Google it!). Note that resizing a VM's NTFS disk is relatively painless if necessary, but best go for enough space to start with
  • -c The location of the original iso file.
    • Note, in the above example we use a file accessible via http. Please move any useful isos you find to the mirror server so that they may be used by others
  • --os-variant Change as appropriate:
  1. for windows 7 or 8 use win7
  2. for server 2008 or 2012 use win2k8

KVM will create a storage file and boot the iso; to complete the installation you need to connect to the VM from your PC:

  • From Windows: use SSVNC [check link to wiki page]
  • From linux:
virt-viewer -c qemu+ssh://[user]@[kvm host]/system [vmdomain]

Run through the installation process. You might get kicked out a couple of times, but reconnect with virt-viewer and all will continue as expected. The KVM server will look something like the following during this process:

jreeves@leibniz:~$ sudo ./create-windows.sh 
Starting install...
Creating storage file win 100% |=========================|  12 GB    00:00     
Creating domain...                                                 0 B 00:00 
Domain installation still in progress. Waiting for installation to complete.
Domain has shutdown. Continuing.
Starting domain...                                                 0 B 00:00 
Domain installation still in progress. Waiting for installation to complete.
Domain has shutdown. Continuing.
Guest installation complete... restarting guest.
jreeves@leibniz:~$


It may be necessary to "sysprep" your Windows VM after creation to make it suitable for cloning or moving about. Once connected to the vm run:

c:\Windows\System32\sysprep\sysprep.exe


Select "Enter System Out-of-Box Experience", check the “Generalize” checkbox (regenerates system SID), change the Shutdown Options to “Shutdown”, and click OK.

PXE boot VM (Deploy from Kace 2000)

This process is not finalised yet, please see KVMPXEBoot and Kace, Windows & KVM for more information. The below instructions work, but they involve a number of steps that could probably be removed:

  1. Create a Ubuntu VM
  2. PXE boot VM and format the disk to NTFS
  3. PXE boot VM and deploy image

In effect, we're creating software and hardware, then replacing the software. A neater method would be to create unused hardware and deploy to it:

  1. Create NTFS file image of desired size
  2. Define brief PXE booting VM around this
  3. PXE boot VM and deploy image

Note, the above sequence is used in the instructions given here Kace, Windows & KVM, but I was unable to make them work on the dev server. Specifically qemu-img create failed.

Perhaps a current NTFS filesystem could be copied and used for the above. Just copy the filesystem and xml definition of a current Windows VM and edit all necessary parts to make them unique. For the current method, however we:

Start by creating a Ubuntu VM as above. Give this machine the hardware settings that you intend to use on the final deployed example. Don't worry about whether you're using a 32 or 64 bit processor; 32 bit images will deploy to a 64 bit machine, so it's probably easiest to simply leave the defaults as they are. Once completed, do:

sudo virsh edit <your-vm>

And make sure that PXE booting is enabled:

 <os>
   <type arch='x86_64' machine='pc-0.12'>hvm</type>
   <boot dev='network'/>
   <boot dev='hd'/>
 </os>

Boot the VM and connect via VNC (note, xtightvncviewer on my laptop kept exiting whenever the screen size on the VM increased. This happens a lot. Keep restarting your VNC client and you will be returned to where you left off). At the bios prompt, choose network boot. You should be presented with the Kace screen; choose the deployment menu.

The VM created above will have a raw filesystem that we need to format to NTFS before deploying an image to it. The easiest way (I've found so far) is to run a scripted installation of XP. Choose scripted installations and run any of the options available. The disk will be formatted to NTFS, then the usual XP installation screen will be shown. Cancel the installation. You will be returned to a Kace prompt that asks whether a specific location is a file or a directory - answer with any answer and the VM will reboot.

Re-enter network booting and the Kace deployment system. From here you can now choose to deploy your image.

Once complete, shutdown your VM and edit it to boot straight from the local disk:

 <os>
   <type arch='x86_64' machine='pc-0.12'>hvm</type>
   <boot dev='hd'/>
 </os>

Post-creation tasks

Start the vm with virsh:

yhamon@paris:/var/lib/kvm$ virsh start vmname
Domain vmname started

Set the vm to automatically start when the host is powered:

 sudo virsh autostart [vmname]

Depending on the situation you may need to add a management agent like Salt configure monitoring like Zabbix, configure a DNS entry (see Dns-master), etc..

Adding a second hard Disk

raw file

  1. Stopped the VM
  2. Used the qemu-img command create a disk:
 sudo qemu-img create -f raw /var/lib/kvm/oanorth-itadmin2/disk2.raw 100G
  1. Added these lines to the .xml file, below the existing disk definition:
   <disk type='file' device='disk'>
     <driver name='qemu' type='raw'/>
     <target dev='hdc' bus='ide'/>
   </disk>
  1. Re-defined the VM
  2. Started the VM
  3. In Windows, right-click My Computer | Manage | Disk Management
  4. All being well, it will announce "new disk found": Accept invitation to write a disk signature
  5. Right-click new disk - Create Partition - use the whole disk
  6. Right-click the partiton - Format - QUICK format
  7. Exit and re-boot to be sure the system sees the disk cleanly

qcow2 format

  • this does not always work - creates a 193kb file which is not formattable as a disk [at least in Windows VM]
  • in which case - just use raw

qcow2 can be used to create device files that use only as much space as the files within them contain. For example, a 100GB device will take up almost no disk space if it has no files in it. The difference between creating raw and qcow2 files is just a couple of options. Create the disk:

sudo qemu-img create -f qcow2 /var/lib/kvm/oanorth-itadmin/disk2.qcow2 100G

Add the relevant details to the xml:

<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <target dev='hdb' bus='ide'/>
    </disk>

Note that the only relevant change is to swap "raw" for "qcow2" in the creation command and xml.

The host sees the file as big as the contents of that disk (initially this file was measured in KBs, but has since been used), but the VM sees the entire 100 GB:

jreeves@leibniz:~$ ls -lh /var/lib/kvm/downloads/download.img 
-rw-r--r-- 1 root root 1.6G 2012-07-06 13:32 /var/lib/kvm/downloads/download.img
jreeves@downloads:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             7.4G  799M  6.2G  12% /
none                  245M  168K  244M   1% /dev
none                  247M     0  247M   0% /dev/shm
none                  247M   60K  247M   1% /var/run
none                  247M     0  247M   0% /var/lock
none                  247M     0  247M   0% /lib/init/rw
none                  7.4G  799M  6.2G  12% /var/lib/ureadahead/debugfs
/dev/sdb1              92G  188M   87G   1% /srv/downloads

If your /etc/fstab devices do not agree with df

  • You may find that /dev/sdba in /ect/fstab is listed as /dev/sdb in df
  • Discover the UUID with the command:
sudo blkid

Kace, Windows & KVM