“Genvm” is a virtual machine (VM) generated script Custom Debian. The generated drive images can be used with Qemu / KVM, VirtualBox and VMWare (Windows, Mac OS or Linux) depending on the selected image format. This script accepts different options to customize all the generated parameters of KVM.
In this article we will simply use the script “genvm”.
Although the development version “genvm” tool is already widely used for many platforms of continuous integration (TeamCity Jenkins) for the dissemination of research tools (eg CosyVerif).
Installation
No special installation is required, there is a script file to download and to place where ever you want. We will use the following command:
aaricia ~ # wget -q https://downloads.sourceforge.net/project/genvm/genvm.tar.bz2 -O - | tar xjvf - -C /sbin/ genvm aaricia ~ # chown root.root /sbin/genvm
From the prerequisites needed while launching for the first time the tool, we find the following ones:
- debootstrap Debian installer available on most distributions (see AUR for Arch Linux, EPEL for RedHat like, etc);
- kpartx installed by default on a lot of distros;
- qemu-img and qemu-nbd available on all linux.
Finally the script requires the rights “root” to run.
As first virtual machine
To generate a minimal virtual machine, the script just needs to have the name of the disk image that will be generated (here “firstvm.raw” will be created in “~ fhh / VM VirtualBox /”):
aaricia ~ # time genvm ~fhh/VirtualBox\ VMs/firstvm.raw Set password to root > Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully real 2m15.012s user 0m47.003s sys 0m5.410s aaricia ~ # ls -l ~fhh/VirtualBox\ VMs/ total 557652 -rw------- 1 root root 5368709120 Mai 12 14:52 firstvm.raw
In this case, the script stops during the construction of the disk image in order to demand the administrator password for future VM.
The invocation of the argument without script displays, among other things, the default values used for the generation of our first disk image:
aaricia ~ # genvm | grep default -a : architecture : amd64, i386 (default : amd64) -f : format of image : raw, qcow2, qcow, vmdk (default : raw) -k : specify kernel version (default : linux-image-amd64) -n : virtual machine name (default : aaricia) -o : change default tmp dir location (path with no space ; default "/tmp") -p : root password (default : ask to user) -s : image size (default : 5G) -S : server to download debian (default : http://ftp.debian.org/debian) -V : debian version : jessie, wheezy, ... (default : wheezy)
The image Debian Wheezy (“-V”) 64-bit (“-a”) format raw (“-f”) to 5G (“-s”) is generated from the server “ftp.debian.org” (“-S “) and has the same name as the host machine (” -n “).
The kernel installed in the VM is the standard kernel distribution (“k”).
The directory used for the construction is created in “/ tmp” (“-o”) and the administrator password is required for the user during the construction of the VM (“-p”).
Before using the image, the root has to “give” it to the user (via “chown”):
aaricia ~ # chown fhh.users ~fhh/VirtualBox\ VMs/firstvm.raw
The image is then used directly via QEMU / KVM:
fhh@aaricia ~ $ qemu-system-x86_64 VirtualBox\ VMs/firstvm.raw & [1] 32766
Note:
- The generated virtual machine is, by default, not connected to the internet and don’t have DHCP client.
- The keyboard is QWERTY.
- This is really a minimum machine, you have all the choices concerning the existant components (no default text editor installed, no logs manager, no MTA, etc).
Advanced VM
In this second example, we will install a list of additional packages on our machine. We will also use the “-t” option to run the scripts directory “~ / scripts.genvm” during and after the generation of the image. The generated virtual machine will be used under VirtualBox.
-
List of additional packages
The packages to be added to the basic installation can be notified in a text file at a rate of one packet per line. The file is then passed to the script via the “-l” option (as list)
aaricia ~ # cat > ~/scripts.genvm/lst.txt openssh-server vim ifupdown console-setup ^d
Another solution is to use the “-A” option followed by the list of packages to add a comma-separated (if there is not too many packages to be added).
-
VM customization scripts
To customize the virtual machine, it is possible to execute scripts contained in the directory passed to “genvm” via the “-t” option.The names of the format scripts “. Chroot {DD | host | post} .nom_du_script” provide information about their orders and their execution contexts:
3rd/ |- 01.chroot.norecommends |- 01.host.java ... |- 01.post.chown `- 02.host.rc.local
Scripts “XX.chroot. *” And “YY.host. *” Will be executed first and in order. The scripts prefixed with “XX.chroot. *” Will be executed in the VM. The scripts prefixed with “XX.host. *” Will be executed on the machine generating the disk image.
Scripts marked by “post” will be executed in order after the generation of the VM (when the disk image is removed).
Note: The files in the directory passed as argument to genvm but not respecting the name of the format described above will not be applied.
A number of variables and functions can be used for customization scripts such as:
- ${MOUNT_POINT}: mount point of the root of the generated image;
- ${HD_IMG}: The name of the generated disk image file;
- set_passwd_to pwd foo: function assigning the password “Password” to “foo” user;
-
Start network during the boot VM
The first script applied to the virtual machine intended to start the network and configure DHCP. The file “/ etc / network / interfaces” of the virtual machine will be suitable for the construction of the disk image:
aaricia ~ # mkdir ~/scripts.genvm aaricia ~ # cat > ~/scripts.genvm/01.host.networkcat > "${MOUNT_POINT}/etc/network/interfaces <<_eof_ auto lo eth0 iface lo inet loopback iface eth0 inet dhcp _eof_ ^d
The script is executed from the host machine and written in “mountpoint / etc / network / interfaces”.
-
Desactivate the installation of the recommended packages
To maintain a minimal system, it is required to disable the automatic installation of the recommended packages.
aaricia ~ # cat > ~/scripts.genvm/01.chroot.norecommends cat > /etc/apt/apt.conf.d/02norecommends <<_eof_ APT::Install-Recommends False; _eof_ ^d
-
Creating a user “FHH” password “TRUC”
We will create the user in the virtual machine:
aaricia ~ # cat > ~/scripts.genvm/02.chroot.createuser useradd -c FHH -g users -m -s /bin/bash fhh
To initialize the password for the user we will do as fellow:
aaricia ~ # cat > ~/scripts.genvm/02.host.setpasswd2user set_passwd_to fhh truc ^d
Chown applied after the generation of the image assumes that party. Now we will change the rights of the disc image:
aaricia ~ # cat > ~/scripts.genvm/01.post.chown chown fhh.users "${HD_IMG}" ^d
Be carefull: chroot contains very little default actions; so if you want to make a “wget” for the virtual machine, install wget and if you want to extract a “tar.bz2” install bzip2, etc.
Generation of the virtual machine
Once complete customization scripts and list of packages are setted up, the script is invoked:
aaricia ~ # genvm -a amd64 -A isc-dhcp-client -f vmdk -l ~/scripts.genvm/lst.txt \ > -n vm -p toor -s 10G -S http://ftp.fr.debian.org/debian -t ~/scripts.genvm/ -v \ > -V jessie ~fhh/vm.vmdk checking for "qemu-img"... Ok ... delete temporary mount point (if needed) /home/root/scripts.genvm/01.post.chown Running /home/root/scripts.genvm/01.post.chown after all
Adding to the packages of the file “~ / scripts.genvm / lst.txt” the “-A” option is used to add the dhcp client. The image Debian Jessie (“-V”) 10G is generated on “vmdk” format to be compatible with VirtualBox. The machine generated is called “vm” (“-n” option). The root password is set to “toor” (“-p” option).
Launch of the Virtual Machine
QEMU/KVM
Under Qemu / KVM, the VM can, as seen previously be launched directly:
fhh@aaricia ~ $ qemu-system-x86_64 ~/vm.vmdk & [1] 6556
VIRTUALBOX
Under VirtualBox, you have to create a new virtual machine and associate the generated VMDK disk format. Then open VirtualBox and click “New” button. Select parameters adapted to the new image and select the newly created disk image before creating the VM. Finaly, start the virtual machine as any VM VirtualBox.
Advanced features
Adding to the features already presented, the script offers other possibilities in order to help the customization or debugging installation scripts.
Interactive mode
The “-i” option allows you to control after the installation it to perform additional actions:
aaricia ~ # genvm -A isc-dhcp-client -p toor -f vmdk -i ~fhh/vm.vmdk *** ENTERING IN INTERACTIVE SHELL *** You are here : /home/root Virtual machine is monted here : /tmp/tmp.qxiJkmmgvC Press "Ctrl+d" or "exit" to exit bash-4.2# # all the wanted commands... bash-4.2# # Possibility of chrooting in the VM bash-4.2# exit exit *** END OF INTERACTIVE SEQUENCE ***
Launch additional scripts
The longest in the construction of a virtual machine of this type is the download and installation of packages. To debug custom scripts, reconstruction of a full VM quickly becomes tedious. The “-T” overcomes this problem.
The option is used on the same line as which is generated the VM. For example, for a disk image generated by the following command:
aaricia ~ # genvm -p toor -l scripts.genvm/lst.txt -t scripts.genvm/ -f vmdk ~fhh/vm.vmdk
The execution of customization scripts only is obtained by adding a “-T” to the command:
aaricia ~ # genvm -T -p toor -l scripts.genvm/lst.txt -t scripts.genvm/ -f vmdk ~fhh/vm.vmdk
For debugging add “-v” will see the execution of scripts:
aaricia ~ # genvm -T -v -p toor -l scripts.genvm/lst.txt -t scripts.genvm/ -f vmdk ~fhh/vm.vmdk ...
In reality, only the file of the image generated on the image size and location of the scripts are necessary:
aaricia ~ # genvm -T -v -t scripts.genvm/ -f vmdk ~fhh/vm.vmdk ...
Mounting a VM generated
By combining the “-i” option for interactivity and “-T” for application customization scripts, it is possible to automatically mount and access the VM via genvm.
The trick is to use the “-i” and “-T” and replace the customization scripts directory with “/ dev / null” (“-f” option).
aaricia ~ # genvm -T -i -t /dev/null -f vmdk ~fhh/vm.vmdk *** ENTERING IN INTERACTIVE SHELL *** You are here : /home/root Virtual machine is monted here : /tmp/tmp.T9mWjcLTNs Press "Ctrl+d" or "exit" to exit bash-4.2# exit *** END OF INTERACTIVE SEQUENCE ***
Once again, only the format of the image (“-f”) and the image file is needed.
Conclusion
The script allows you to generate rapidly and/or automatically many virtual machines for the realization of bench tests or the distribution of some projects.The management of partitioning disk images should allow the script to generate server-based virtual machines that can be putted into production directly.For other developments don’t hesitate to ask and/or to contribute.
Source Translated : http://www.admin-linux.fr/?p=9109