Basic management and common commands for KVM virtual machines under Linux

Source: Internet
Author: User
Tags cpu usage hex code

Description: There may be duplicates

First, the basic management of KVM

1. View the KVM virtual machine configuration file

#KVM Virtual Machine default profile location [[email protected] qemu]# pwd/etc/libvirt/qemu[[email protected] qemu]#  1 root root 3863 Nov 6 17:26  CentOS6.5.  1 root root 3771 Nov  7 00:57 centos6u7.  1 root root 3628 Nov  7 02:04 centos.  3 root root  7 00:46 Networks

2. Start and close

#显示正在运行的虚拟机 [[email protected] ~]# Virsh list Id    Name                           State----------------------------------------------------
#显示所有虚拟机 [[email protected] ~]# Virsh list--all Id Name State----------------------------------------------------- CentOS shut off- CentOS6.5 shut off- centos6u7 shut off# start a virtual machine named CentOS6.5 [[email protected] ~]# Virsh start centos6.5# close the virtual machine named CentOS6.5 [[ Email protected] ~]# Virsh shutdown centos6.5# Force off virtual machines named Centos6.5 [[email protected] ~]# Virsh destroy centos6.5# remove name CE ntOS6.5 virtual machine [[email protected] ~]# virsh undefine centos6.5# settings CentOS6.5 virtual machine boot up [[email protected] ~]# Virsh autostart Cent os6.5# by default, the Virsh tool cannot shut down the Linux virtual machine #linux the operating system needs to turn on and start the Acpid service. This service must be configured on the installation of a KVM Linux virtual machine. # yum-y Install acpid#/etc/init.d/acpid start

3. Start the virtual machine from the configuration file

[Email protected] ~]# Virsh create/etc/libvirt/qemu/centos6.5.xml

4. Other (hang, resume) Virsh command

#挂起服务器 [[email protected] qemu]# Virsh suspend CentOS6.5
#恢复服务器 [[email protected] qemu]# Virsh resume CentOS6.5

5. Renaming a virtual machine

#停止虚拟机 [[email protected] qemu]# Virsh shutdown centos6u5# Export the virtual machine's configuration file [[email protected] qemu]# pwd/etc/libvirt/qemu[[ Email protected] qemu-img]# Virsh dumpxml centos6u5 > Test-centos6u5.xml
#更改配置文件 [[email protected] qemu]# sed-i ' s/centos6u5/test-centos6u5/g ' test-centos6u5.xml# Note You can not change the image name
#移除原有的虚拟机 [[email protected] qemu]# Virsh undefine centos6u5# load new virtual machine [[email protected] qemu]# Virsh define Test-centos6u5.xml
#启动虚拟机 [[email protected] qemu]# Virsh start Test-centos6u5

6. Virtual Machine Snapshot creation

#转换磁盘镜像文件格式为qcow2 [[email protected] ~]# Virsh shutdown test-centos6u5[[email protected] qemu-img]# qemu-img convert-f RA W centos6u5.raw-o qcow2 centos6u5.raw.qcow2# snapshots must be in QCOW2 format #我这边从新建立一个虚拟机以qcow2
#创建快照 [[email protected] qemu-img]# Virsh snapshot-create centos6u5# View snapshot [[email protected] qemu-img]# Virsh Snapshot-list centos6u5 Name Creation time State------------------------------------------------------------1479043349 2016-11-13 08:22:29-0500 shutoff #恢复快照 [[email protected] qemu-img]# Virsh snapshot-revert centos6u5 1479043349# Delete snapshot [[email protected] qemu-img]# Virsh Snapshot-delete Centos6u5 1479043349

Ii. Common Commands

List all virtual machines

Virsh List--all

displaying virtual machine information

Virsh Dominfo kvm-1

Show virtual machine memory and CPU usage

Yum Install Virt-top-yvirt-top

Display virtual machine partition information

VIRT-DF kvm-1

Shutting down a virtual machine (SHUTODWN)

Virsh shutdown kvm-1

Start a virtual machine

Start kvm-1

Set up virtual machine (KVM-1) following system self-boot

Virsh autostart kvm-1

Turn off virtual and self-boot

Virsh autostart--disable kvm-1

Deleting a virtual machine

Virsh undefine kvm-1

Log in to a virtual machine via the control window

Virsh Console Kvm-1

Other

Virsh shutdown CentOS shutdown CentOS virtual machine        Virsh destroy CentOS            Force off CentOS virtual machine Virsh suspend CentOS        pause CentOS Virtual machine vrish Resume CentOS Recovery CentOS virtual machine            virsh reboot CentOS            restart CentOS virtual Machine Virsh edit CentOS                Edition CentOS virtual machine Virsh Vncdisplay  CentOS    

Add a hard disk to a virtual machine

Add a hard disk (LVM volume) or USB to a virtual machine

Virsh attach-disk kvm-1/dev/sdb vbd--driver qemu-mode shareable

You can uninstall USB when you are finished using

Detach-disk KVM VDB

Add LVM volumes and mount

[Email protected] ~]# lvcreate-n kvm-1-data-l 50G vg_shkvm1[[email protected]1 ~]# virsh attach-disk kvm-1/dev/vg_shkvm1/kvm-1-data vdb--driver qemu--ModeShareabledisk attached successfully#Log on to Kvm-1 to see if the LVM is already mounted [[email protected]1 ~]# virsh Console kvm-1 #Enter kvm-1 user and password [[email protected]1-1 ~]#fdisk-L #view hard disk mount conditions disk/dev/vda:21.5 GB, 21474836480bytesHeads, Sectors/track, 41610cylindersunits= Cylinders of 1008 * 512 = 516096bytessector Size(logical/physical): bytes/512Bytesi/o Size (minimum/optimal): bytes/512bytesdisk Identifier:0x00058197Device Boot      Start         EndBlocks Id System/DEV/VDA1 * 3 1018 512000 83linuxpartition1 does not End  onCylinder BOUNDARY./DEV/VDA2 1018 41611 204584968e Linux lvmpartition2 does not End  onCylinder boundary.Disk/dev/mapper/volgroup-lv_root:18.8 GB, 18798870528bytes255 heads, Sectors/track, 2285cylindersunits= Cylinders of 16065 * 512 = 8225280bytessector Size(logical/physical): bytes/512Bytesi/o Size (minimum/optimal): bytes/512bytesdisk Identifier:0x00000000Disk/dev/mapper/volgroup-lv_swap:2147 MB, 2147483648bytes255 heads, Sectors/track, 261cylindersunits= Cylinders of 16065 * 512 = 8225280bytessector Size(logical/physical): bytes/512Bytesi/o Size (minimum/optimal): bytes/512bytesdisk Identifier:0x00000000Disk/dev/vdb:53.7 GB, 53687091200 bytes #newly added hard diskHeads, Sectors/track, 104025cylindersunits= Cylinders of 1008 * 512 = 516096bytessector Size(logical/physical): bytes/512Bytesi/o Size (minimum/optimal): bytes/512bytesdisk Identifier:0x00000000

Format the newly added VDB and add it to the LVM group

#partition The newly added hard disk [[email protected]1-1 ~]#fdisk/dev/VDBDeviceContains neither a valid DOS partition table, nor Sun,SGI or OSF disklabelbuilding a new DOS disklabel with disk identifier0xf04b6807.changes'll remain in memory only, until you decide to write them. after that, of course, the previous content won ' t be recoverable.warning:invalid Flag0x0000 of partition Table 4 would be corrected by W (rite)warning:dos-compatibleModeis deprecated.It ' s strongly recommended to switchoffTheMode(Command' C ')and change display units to sectors(Command' U ').Command(M forHelp): M #View HelpCommandaction A toggle a bootable flag B edit BSD Disklabel c toggle the DOS compatibility flag D delete a P Artition l list known partition types MPrintThis menu n add a new partition o create a new empty DOS partition table PPrintThe partition table Q quit without saving changes s create a new empty Sun Disklabel t change a partition ' s system ID u change display/Entry Units vVerifyThe partition Table W write table to disk andExitx Extra Functionality(Experts only)Command(M forHelp): N #Add a partitionCommandAction E Extended p primary partition(1-4)P#Select Add an extended partition partition number(1-4): Value out of range.Partition Number(1-4): 1First cylinder(1-104025, default 1): Using Default Value1Last cylinder, +cylinders or +size{k,m,g} (1-104025, default 104025): Using Default Value104025Command(M forHelp): t #change the format of the partition selected partition1Hex Code(typeL to list codes): 8e #change to lvmchanged systemtypeof partition 1 to 8e (Linux LVM)Command(M forHelp): W #save changes [email protected]1-1 ~]# MKFS.EXT4/DEV/VDB1 #format Partition MKE2FS1.41.12 (17-may-2010)Filesystemlabel=OStype: Linuxblock size=4096 (log=2)Fragment Size=4096 (log=2)Stride=0 blocks, Stripe width=0blocks3276800 Inodes, 13107142blocks655357 blocks (5.00%) reserved forThe Super Userfirst data Block=0Maximum filesystem blocks=4294967296400block Groups32768 blocks per group, 32768fragments per group8192inodes per groupsuperblock backups stored onBlocks:32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424Writing inode tables:donecreating Journal(32768 blocks): donewriting superblocks and filesystem accounting information:donethis filesystem would be automatically checked Eve Ry28Mounts orWhichever comes first. UseTune2fs-c or-i to override.[[Email protected]1-1 ~]# PVC REATE/DEV/VDB1 #create Pvvdb vdb1[[email protected]1-1 ~]# vgextend Volgroup/dev/vdb #Extended LVM vgvdb vdb1[[email protected]1-1 ~]#VGs VG#PV #LV #SN Attr vsize vfree VolGroup2 2 0 wz--n-69.50g 50.00g# as can be seen from the above, the new additions have been added to the LVM group

Changing the parameters of a virtual machine

Change the memory, CPU, and other information of the virtual machine after creation through the command line

Change memory

# 1. View virtual machine current memory [[email protected]1 ~]# virsh dominfo kvm-1 | grep memorymax Memory:     4194304 kibused m Emory:    4194304 KiB# 2, dynamically set memory to 512MB, memory reduced Virsh setmem kvm-1 524288# Note Unit must be KB# 3 , viewing memory changes # Virsh dominfo kvm-114194304524288 kiB# 4, memory increase Virsh shutdown kvm-1virsh edit kvm-1  #/etc/libvirt/demu/kvm-1/XML# Follow the steps to add more memory

Change CPU

The configuration file needs to be modified so the virtual machine needs to be stopped

Virsh shutdown kvm-1virsh edit kvm-  # <vcpu>2</vcpu>  # 4 > 2/etc/libvirt/ Demu/kvm-1/xml

HDD expansion

1. Create a 10-gb non-sparse File:# DDif=/dev/zero of=/vm-images/vm1-add.img bs=1m count=102402.Shutdown the VM:#Virsh shutdown VM13. Add an extra entry for' Disk ' in the VM ' s XML file In/etc/libvirt/qemu. can lookCopy& Paste the entry forYour mail storageDeviceAnd just change the target and address tags. forExample:#Virsh Edit Vm1<disktype= ' file 'Device= ' disk ' > <driver name= ' qemu 'type= ' raw 'Cache= ' None ' io= 'Threads'/> <source file= '/vm-images/vm1.img '/> <target dev= ' vda ' bus= ' virtio '/> <addresstype= ' PCI ' domain= ' 0x0000 ' bus= ' 0x00 ' slot= ' 0x04' function= ' 0x0 '/> </disk>ADD:<disktype= ' file 'Device= ' disk ' > <driver name= ' qemu 'type= ' raw 'Cache= ' None ' io= 'Threads'/> <source file= '/vm-images/vm1-add.img '/> <target dev= ' vdb ' bus= ' virtio '/> <addresstype= ' PCI ' domain= ' 0x0000 ' bus= ' 0x00 ' slot= ' 0x06 ' function= ' 0x0 '/> </disk> # Here is a recommended way to add a hard drive using the above

Deleting a virtual machine

The first step is to stop the virtual machine

Virsh shutdown kvm-1

Step Two

Virsh Destroy Kvm-1

Step Three

Virsh undefine kvm-1

Fourth Step

Rm/dev/vg_shkvm1/kvm-1  # Not recommended to delete hard drives

Reference:

Https://www.cnblogs.com/yexiaochong/p/6059956.html (The above sections are transferred from this article)

http://blog.csdn.net/wh211212/article/details/74908390 (The above sections are transferred from this article)

Basic management of KVM virtual machine under Linux and Common commands (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.