KVM creates a batch of KVM virtual machines by Virsh making image
Create a virtual machine from the KVM virtual machine XML template file (make an image in raw format and install Ubuntu OS) ******************
First, the preparatory work
1. Prepare a KVM virtual machine profile Template
KVM Virtual Machine profile template file uploaded to attachment: Kvm_vm_raw_template.xml
Copying a copy of a virtual machine profile template: Kvm_vm_raw_template.xml
2. Determine the storage path of image
3. Create an image file in RAW format
4. Prepare ubuntu14.4 ISO file
Second, define the Kvm_vm_raw_template.xml virtual machine configuration file
Note: All "percent" in the XML file is a variable, for example:%vm_name%
Replace the variable with SED, when the virtual machine name is: ubuntu14.04_temp
1, virtual machine overall information configuration
<name>%VM_NAME%</name> Virtual machine Name
<uuid>%UUID%</uuid> UUID Values
<memory>1048576</memory> total memory size, in kilobytes
<currentMemory>1048576</currentMemory> Current Memory size
<vcpu>1</vcpu> Number of CPUs
The configuration is as follows:
Sed-i "S,%vm_name%,ubuntu14.04_temp,g" Ubuntu14.04.xml
Generating UUID values from the Uuidgen command
Uuid= ' Uuidgen '
Sed-i "s,%uuid%, $uuid, G" ubuntu14.04.xml
2, System Information configuration
<os>
<type arch= ' x86_64 ' machine= ' pc-0.14 ' >hvm</type> system architecture and machine type, HVM representative is based on hardware virtualization, such as to view supported machine types: qemu-system-x86_ 64-m?
<boot dev= ' HD '/> Boot media, first use hard disk as the preferred boot media, such as Cdrom,floppy
<bootmenu enable= ' yes '/> whether the boot Options menu is turned on, the boot menu can be accessed by F12 when the virtual machine starts
</os>
3. Create image file and configuration
3.1. Create a RAW format image
Qemu-img create-f Raw Ubuntu14.04.raw 20G
Sed-i "S,%image_path%,/datapool/vm/ubuntu14.04.raw,g" Ubuntu14.04.xml
<disk type= ' file ' device= ' disk ' >
<driver name= ' qemu ' type= ' raw ' cache= ' None '/>
<source file= '/datapool/vm/ubuntu14.04.raw '/>
<target dev= ' Vda ' bus= ' Virtio '/>
<address type= ' PCI ' domain= ' 0x0000 ' bus= ' 0x00 ' slot= ' 0x08 ' function= ' 0x0 '/>
</disk>
4. Mount ISO
<disk type= ' file ' device= ' cdrom ' >
<driver name= ' qemu ' type= ' raw ' cache= ' None '/>
<source file= '/datapool/iso/ubuntu-14.04.3-server-amd64.iso '/> #指定ISO文件路径
<target dev= ' hda ' bus= ' ide '/>
<readonly/>
<address type= ' drive ' controller= ' 0 ' bus= ' 1 ' unit= ' 0 '/>
</disk>
5. Virtual network card
Randomly generated MAC address command: OpenSSL Rand-hex 6 | Sed ' s/(..) /1:/g; s/.$//'
40:d9:44:0c:55:ac
<interface type= ' bridge ' >
<mac address= ' 40:d9:44:0c:55:ac '/>
<source bridge= ' br100 '/>
<target dev= ' vnet0 '/>
<alias name= ' net0 '/>
<address type= ' PCI ' domain= ' 0x0000 ' bus= ' 0x00 ' slot= ' 0x03 ' function= ' 0x0 '/>
</interface>
6. Define Virtual machines
Virsh define Ubuntu14.04.xml defines a virtual machine, define tells Libvirt to be managed by Libvirt
#查看虚拟机列表
[Email protected]:/datapool/vm# virsh list--all
Id Name Status
----------------------------------------------------
-Ubuntu14.04_temp off
#启动虚拟机
Virsh Start Ubuntu14.04_temp
7. VNC view virtual machine, #查看VNC端口
[Email protected]:/datapool/vm# virsh Vncdisplay ubuntu14.04_temp
: 0
Installing the Vncviewer tool
Apt-get-y Install Vncviewer
Vncviewer 192.168.89.3:0 Connection, note the IP address here is br100 IP
8, then start to install the operating system, when the operating system and required software installed, the virtual machine shut down, and the virtual machine RAW format image is saved
At this point, a raw format image file has been created, and the operating system is installed, and so on, using the raw format image as Backing_file, bulk create Qcow2 disk (as a disk for virtual machines)
Bulk Create Qcow2 disks (as disks of virtual machines) using the RAW format image as Backing_file ********************************
First, batch create Ubuntu virtual machine solution:
Scenario 1, manual installation, cumbersome, egg pain
Scenario 2, direct replication, copy already installed system RAW format image file, need to create how many VMs to copy how many copies, respectively, the use of backup files to create a virtual machine, but the main time will be spent on the copy image
Scenario 3, Qcow2 quickly create, take advantage of the system already installed RAW format image file as Backing_file file, how many VMs need to be created to copy how many copies of the QCOW2 format image, and then use Qcow2 file to create virtual machine, time is very little
* Choose Scenario 3 here
Before making a good raw format image (the operating system is installed), you need to note the place:
1, try not to modify the image
2. Do not use this image directly to create a virtual machine
3, avoid the human read and write process to the image operation, such as Mount
Ii. Start writing batch creation scripts
1, the script has been finished
Script file can be consulted: Create_qcow2_vm.bash script file has been uploaded to attachment
2, using this method to build up the virtual machine, there are the following problems:
2.1, the host name is exactly the same
2.2, the virtual machine memory saved MAC address is still backing_file MAC address (tested found that the newer version of the KVM does not appear the same MAC address situation)
Solution: After you mount the qcow2 image, modify the hostname and MAC address
1, mount the Qcow2 file, first need to install the dependency package Kpartx, use the Kpartx tool to mount the virtual machine disk image file
2. Mount the Qcow2 file using the NBD module (the modprobe command is used to intelligently load modules into the kernel or remove modules from the kernel.) The modprobe can load the specified individual modules, or load a set of dependent modules. Modprobe will determine which modules to load based on the dependencies that the depmod generates. If an error occurs during onboarding, the entire set of modules is unloaded at modprobe. )
The #******************** command is as follows: ***********************
Apt-get-y Install Kpartx
#激活nbd模块
Modprobe NBD max_part=63
#将qcow2文件挂载到/dev/nbd0 Equipment (Note: If/DEV/NBD0 is occupied, you can use the/DEV/NBD1 device)
Qemu-nbd-c/dev/nbd0 Ubuntu14.04_vm5.qcow2
#处理 partition under the/DEV/NBD0
Kpartx-a/dev/nbd0
#这里需要暂停一下
Sleep 1
#查看qcow2文件中的各种分区 (see here The NBD is the Mount partition, which partition is the root partition that needs to be tried before it can be determined)
Ls/dev/mapper
#确定根分区
[Email protected]:/datapool/vm/ubuntu14.04_vm5# temp_file= ' mktemp '
[Email protected]:/datapool/vm/ubuntu14.04_vm5# rm-rf $temp _file; Mkdir-p $temp _file
[Email protected]:/datapool/vm/ubuntu14.04_vm5# mount/dev/mapper/nbd0p1 $temp _file
[email protected]:/datapool/vm/ubuntu14.04_vm5# ls $temp _file/usr #没有挂载的这个分区/usr This directory, stating that it is not a root partition
LS: Unable to access/tmp/tmp.nmt6d15vlk/usr: No file or directory
[email protected]:/datapool/vm/ubuntu14.04_vm5# ls $temp _file #查看了一下, this estimate is/boot partition
Abi-3.19.0-25-generic config-3.19.0-25-generic Grub initrd.img-3.19.0-25-generic lost+found memtest86+.bin memtest86 +.elf Memtest86+_multiboot.bin system.map-3.19.0-25-generic Vmlinuz-3.19.0-25-generic
[Email protected]:/datapool/vm/ubuntu14.04_vm5# umount $temp _file #取消这个分区的挂载
[Email protected]:/datapool/vm/ubuntu14.04_vm5# mount/dev/mapper/nbd0p2 $temp _file #挂载nbd0p2分区
[email protected]:/datapool/vm/ubuntu14.04_vm5# ls $temp _file #挂载成功后, view the contents of the partition, this is the root partition.
Bin Boot Dev etc home initrd.img Lib lib64 lost+found media mnt opt proc root run sbin SRV sys tmp usr var vmlinuz
[email protected]:/datapool/vm/ubuntu14.04_vm5# ls $temp _file/usr #这个分区有/usr directory
Bin games include Lib local sbin share src
#确保无误后, start modifying host names below
Host_name= "VM5"
Sed-i "s,127.0.0.1.*,127.0.0.1 $HOST _name,g" $temp _file/etc/hosts
echo $HOST _name > $temp _file/etc/hostname
#修改MAC地址
It has been tested that newer versions of KVM will not have the same MAC address, my environment is ubuntu14.04
Here are the 4 VMS I created in bulk, and I can see that the MAC address is really not duplicated, maybe it's really related to the version.
VM4 fa:92:28:bd:1a:79
VM2 FA:92:15:28:D7:D5
VM3 fa:92:ad:51:87:54
VM1 FA:92:38:68:52:E0
#卸载挂载的分区 (Be sure to note the Unload order)
[Email protected]:/datapool/vm/ubuntu14.04_vm5# umount $temp _file
[Email protected]:/datapool/vm/ubuntu14.04_vm5# qemu-nbd-d/DEV/NBD
Since this is all done, you can start the virtual machine for the Qcow2 disk.
This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1878898
KVM enables batch creation of KVM virtual machines