kvm virtual machine creation and cloning methods

  • 2020-05-27 07:41:53
  • OfStack

preface

This article introduces the creation and cloning of kvm virtual machine

1. Create a virtual machine:


qemu-img create -f qcow2 -o preallocation=metadata /data/kvm_centos/centos6.7_base.qcow2 10G

virt-install --name=centos6.7_base --ram 512 --vcpus=1 -f /data/kvm_centos/centos6.7_base.qcow2 --location /data/soft/CentOS-6.7-x86_64-bin-DVD1.iso --graphics vnc,listen=0.0.0.0,port=6900, --extra-args='console=tty0 console=ttyS0,115200n8 serial' --network network=default, --force --autostart

2. Clone virtual machine:

The clone of KVM virtual machine can be divided into the following steps:

1. Clone using the virt-clone command

2. Modify the VNC connection port of the cloned virtual machine through the virsh edit command

3. Configure the host name, ip address and other contents of the cloned virtual machine

1. Use the virt-clone command to clone, and keep the cloned machine in a shutdown state before cloning


[root@puppetnode01 yum.repos.d]# virsh list --all
 Id  Name              State
----------------------------------------------------
 -   centos6.7_01          shut off

[root@puppetnode01 yum.repos.d]# cd /data/kvm_centos/
[root@puppetnode01 kvm_centos]# ll
total 2774424
-rw-r--r-- 1 root root 10739318784 Oct 29 15:54 centos6.7_01.qcow2
[root@puppetnode01 kvm_centos]# virt-clone -o centos6.7_01 -n centos_ansible_01 -f /data/kvm_centos/centos_ansible_01.qcow2
Cloning centos6.7_01.qcow2                                                                   | 10 GB   01:16   

Clone 'centos_ansible_01' created successfully.
[root@puppetnode01 kvm_centos]# virsh list --all
 Id  Name              State
----------------------------------------------------
 -   centos6.7_01          shut off
 -   centos_ansible_01       shut off

[root@puppetnode01 kvm_centos]# 

[root@puppetnode01 kvm_centos]# virsh start centos_ansible_01
Domain centos_ansible_01 started

[root@puppetnode01 kvm_centos]# virsh list --all       
 Id  Name              State
----------------------------------------------------
 3   centos_ansible_01       running
 -   centos6.7_01          shut off

[root@puppetnode01 kvm_centos]# 

After cloning, you need to do the following to use the new machine.

1. Modify the vnc port address, because the cloned machine was set up with the vnc port content


[root@puppetnode01 kvm_centos]# virsh edit centos_ansible_01
  <graphics type='vnc' port='5900' autoport='no' listen='0.0.0.0'>
   <listen type='address' address='0.0.0.0'/>
  </graphics>

2. Log in the machine with virsh console, modify the host name, ip address and other contents


[root@centos_init ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=centos_ansible_01
[root@centos_init ~]# cat /etc/hosts
127.0.0.1  centos_ansible_01 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1     localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@centos_init ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth1
HWADDR=52:54:00:20:d6:f6
TYPE=Ethernet
UUID=21114847-3d8c-4e4b-86be-04c76d848c92
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
GATEWAY=192.168.122.1
IPADDR=192.168.122.11
dns=192.168.122.1

It can be restarted and used normally.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: