📜 ⬆️ ⬇️

We stamp windows: automated deployment of Windows virtual machines to Hyper-V using Vagrant (part 2)

In the previous part, we looked at what automation of deploying VM Windows is needed for and how to prepare the host system. Today I would like to focus on the technical implementation of the most massive part of the process, namely the preparation of the basic boxing for Vagrant. After all the necessary software is installed, it's time to prepare an exemplary box - a template from which the machines will unfold. The box is a regular tar-archive containing a VHD disk file and metadata: virtual settings and service information. Thus, it is necessary to start preparing the image with creating a new VM in it, updating it, adjusting and compressing in order to save space and speed up the deployment. I will not dwell on how to create a new virtual machine in Hyper-V Manager, how to install the system and roll updates. I will focus only on the nuances:After installing all the updates, Windows usually “swells” up to 12-15 gigabytes, which is obviously a bit too much for quick cloning. This is due to the fact that the system caches package installers and distributions of some of its components. All this, fortunately, is easily cleaned. So, how to reduce the volume occupied by the VHD file?After that, you can turn off the virtual machine and run Compact operation on its VHD. The total size may vary, but usually it is at the level of 5-7 GB. The final size of the box will be even smaller, because we are going to further compress it with tar and GZip. However, if you plan to use this machine in a domain environment - you need to prepare it so that there is no situation with duplication of SID. To do this, use the Sysprep utility.

Sysprep walking

In the most general case, preparing virtual machines for cloning using sysprep is a rather banal procedure. To do this, you can use as a GUI.
C: \ Windows \ System32 \ Sysprep \ sysprep

as well as the command line, which is closer to our liking and also allows you to specify additional parameters:
C: \ Windows \ System32 \ Sysprep \ sysprep / generalize / oobe / mode: vm / shutdown
After that, the program will perform all the necessary operations and extinguish the car. The next time the system is turned on, it will be in the aforementioned OOBE (Out-of-Box Experience) - the state in which the newly purchased laptop is located when it is first turned on: hardware inventory will be taken, local administrator password, license key, computer name and so on will be requested. All anything, but you have to enter all this manually, from the keyboard. Fortunately, all this is easily automated. To do this, you need to prepare an answer file, unattend.xml and put it next to the sysprep.exe file launched (in the C: \ Windows \ System32 \ Sysprep folder), or by specifying the path to it as a command line parameter - this will allow you to place the file on the external disk so that it does not appear on the final box. I will not dwell on this in detail; I just want to say that this will require a Windows installation image (more precisely, an install.wim file from it) and a Windows Assessment and Deployment Kit (ADK) (you need only install the Deployment Tools item, which includes the Windows System Image Manager). Links to the distribution, instructions and examples of use will be in the appropriate section in the next, final part of the article.
Important nuances:

Boxing packaging

After Sysprep put out the car, it's time to pack the virtual machine in a boxing file. In general, Vagrant has built-in functionality for this (command provision), however, as of now, it does not work in Hyper-V. So we’ll pack up the box with our hands - there’s nothing to be afraid of, it’s done quite simply:
Now, in order for Vagrant to copy it to itself and see — you need to add it using the add command directly from our directory with the file:
vagrant box add --provider hyperv --name Win2012R2x64 Win2012R2.box
If everything went well, the box will become available (the vagranate will copy it to himself in% VAGRANT_HOME% and unpack it) and you can see it in the output of the vagrant box list command. To make it available for colleagues - you can put it on a shared network drive (then the command for adding a box will be vagrant box add --name Win2012R2x64 D: \ Path \ To \ Win2012R2x64 \\ fileserver.domain.local \ boxes \ win2012r2. box) or web server: local (vagrant box add - name Win2012R2x64 http: //devops.local/boxes/win2012r2.box) or external. For example, at http://www.vagrantbox.es/ or Hashicorp Atlas .
In the next part, I will show you how to lift a VM from boxing and perform its automatic tuning with the help of provisioning.

')

Source: https://habr.com/ru/post/251873/


All Articles