Linux KVM virtual machines under management and basic common commands (rpm)

Reprinted connection: https: //www.cnblogs.com/EasonJim/p/8367022.html

First, the basic management of KVM

1, see the KVM virtual machine configuration file

Copy the code

Kvm virtual machine default configuration file location

[KVM QEMU the root @] # pwd
/ etc / the libvirt / QEMU
[the root @ KVM QEMU] # LL
Total 12 is
-rw ------- 3863-Nov. 1 the root the root. 6 17:26 CentOS6.5.xml
-RW- 3771-Nov. 1 the root the root ------. 7 00:57 CentOS6u7.xml
-rw ------- 3628-Nov. 1 the root the root. 7 02:04 centos.xml
drwx the root the root. 3 ------ 40 Nov 7 00:46 networks
copy the code
2, to start off with

Copy the code

Display the virtual machine is running

[root@kvm ~]# virsh list
Id Name State
----------------------------------------------------

Displays all virtual machines

[root@kvm ~]# virsh list --all
Id Name State
----------------------------------------------------

  • centos                         shut off
  • CentOS6.5                      shut off
  • CentOS6u7                      shut off

Start the virtual machine named CentOS6.5

[root@kvm ~]# virsh start CentOS6.5

Close the virtual machine named CentOS6.5

[root@kvm ~]# virsh shutdown CentOS6.5

Forced to shut down the name of the virtual machine Centos6.5

[root@kvm ~]# virsh destroy CentOS6.5

Removing the name for the virtual machine CentOS6.5

[root@kvm ~]# virsh undefine CentOS6.5

Set CentOS6.5 virtual machine boot

[root@kvm ~]# virsh autostart CentOS6.5

virsh tool can not linux virtual machine shutdown by default

linux operating system needs to open and start acpid service. This service must be configured to install KVM linux virtual machine.

yum -y install acpid

/etc/init.d/acpid start

Copy the code
3. Start the virtual machine configuration file

[KVM the root @ ~] # virsh Create /etc/libvirt/qemu/CentOS6.5.xml
. 4, the other (suspend, resume) command virsh

Hangs server

[root@kvm qemu]# virsh suspend CentOS6.5

Recovery Server

[root @ KVM QEMU] # virsh Resume CentOS6.5
5, rename a virtual machine

Copy the code

Stopping Virtual Machines

[root@kvm qemu]# virsh shutdown CentOS6u5

Export virtual machine's configuration file

[root@kvm qemu]# pwd
/etc/libvirt/qemu
[root@kvm qemu-img]# virsh dumpxml CentOS6u5 > test-CentOS6u5.xml

Change profile

[root@kvm qemu]# sed -i 's/CentOS6u5/test-CentOS6u5/g' test-CentOS6u5.xml

Note you can not change the image name

Remove the original virtual machine

[root@kvm qemu]# virsh undefine CentOS6u5

Load new virtual machine

[root@kvm qemu]# virsh define test-CentOS6u5.xml

Start the virtual machine

[root @ kvm qemu] # virsh start test-CentOS6u5
copy the code
6, create a snapshot of a virtual machine

Copy the code

Convert disk image file format qcow2

[root@kvm ~]# virsh shutdown test-CentOS6u5
[root@kvm qemu-img]# qemu-img convert -f raw CentOS6u5.raw -O qcow2 CentOS6u5.raw.qcow2

Snapshot necessarily need to format the job qcow2

I am here to create a new virtual machine qcow2

Create a snapshot

[root@kvm qemu-img]# virsh snapshot-create CentOS6u5

View snapshot

[root@kvm qemu-img]# virsh snapshot-list CentOS6u5
Name Creation Time State
------------------------------------------------------------
1479043349 2016-11-13 08:22:29 -0500 shutoff

Snapshot recovery

[root@kvm qemu-img]# virsh snapshot-revert CentOS6u5 1479043349

Deleting a snapshot

[root @ kvm qemu-img] # virsh snapshot-delete CentOS6u5 1479043349
duplicated code
two common commands,

List all virtual machines

virsh list --all
Displaying Virtual Machine Information

virsh dominfo kvm-1
shows the usage of the virtual machine memory and cpu

Top the install the virt -Y-yum
the virt-Top
display of the virtual machine partition information

virt-df kvm-1
off the virtual machine (shutodwn)

virsh shutdown kvm-1
to start the virtual machine

virsh start kvm-1
set up a virtual machine (kvm-1) following the system from the start

virsh autostart kvm-1
off the virtual and from the start

virsh autostart --disable kvm-1
to delete the virtual machine

virsh undefine kvm-1
Log virtual machine through the control window

virsh console kvm-1
other

Copy the code
virsh shutdown centos close centos virtual machine
virsh destroy centos forced shutdown 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 edit centos virtual machine
virsh vncdisplay centos vnc display
copy the code
to add the virtual machine hard disk

Adding hard disk (lvm volume) or USB to a virtual machine

virsh attach-disk kvm-1 / dev / sdb vbd --driver qemu --mode shareable
after completion of use can be unloaded usb

virsh detach-disk kvm vdb
added lvm volume, and mount

复制代码
[root@sh-kvm-1 ~]# lvcreate -n kvm-1-data -L 50G vg_shkvm1
[root@sh-kvm-1 ~]# virsh attach-disk kvm-1 /dev/vg_shkvm1/kvm-1-data vdb --driver qemu --mode shareable
Disk attached successfully

Log on to see if kvm-1 has been mounted lvm

[root @ sh-kvm-1 ~] # virsh console kvm-1 # input kvm-1 user and password
[root @ sh-kvm-1-1 ~] # fdisk -l # Mount View hard case

Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00058197

Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 41611 20458496 8e Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk / dev / vdb: 53.7 GB , 53687091200 bytes # newly added hard disk
16 Heads, 63 is sectors / Track, 104 025 Cylinders
Units = Cylinders of 1008 * 512 = 516 096 bytes
Sector size (Logical / PHYSICAL): 512 bytes / 512 bytes
the I / O size (Minimum / Optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
copy the code
format newly added vdb, and added to the group lvm

Copy the code

Newly added to the hard disk partition

[root@sh-kvm-1-1 ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf04b6807.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): m # 查看帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the 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
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n #添加一个分区
Command action
e extended
p primary partition (1-4)
p #选择添加一个扩展分区
Partition number (1-4):
Value out of range.
Partition number (1-4): 1
First cylinder (1-104025, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-104025, default 104025):
Using default value 104025

Command (m for help): t # change the format partitions
the Selected Partition. 1
Hex code (type L to List Codes): # 8E into LVM
the Changed type of System Partition. 1 to 8E (the Linux the LVM)

Command (m for help): w # 保存更改
root@sh-kvm-1-1 ~]# mkfs.ext4 /dev/vdb1 # 格式化分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13107142 blocks
655357 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@sh-kvm-1-1 ~]# pvc reate /dev/vdb1 # 创建pv
vdb vdb1
[root@sh-kvm-1-1 ~]# vgextend VolGroup /dev/vdb # 扩展lvm vg
vdb vdb1
[root@sh-kvm-1-1 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 2 2 0 wz--n- 69.50g 50.00g

From the above it can be seen, it has been added to the newly added group lvm

Copy the code
parameter change virtual machine

Virtual machine's memory, cpu and other information after the change created by the command line

Change Memory

Copy the code

1. Review the current virtual machine memory

[root@sh-kvm-1 ~]# virsh dominfo kvm-1 | grep memory
Max memory: 4194304 KiB
Used memory: 4194304 KiB

2, the dynamic memory is provided to 512MB, reduce memory

virsh setmem kvm-1 524288

Note that the unit must be KB

3, view memory changes

virsh dominfo sqm-1 | grep memory

Max memory: 14194304 KiB
Used memory: 524288 kiB

4, memory increases

virsh shutdown kvm-1
virsh edit kvm-1 # 直接更改memory
virsh create /etc/libvirt/demu/kvm-1/xml

After the step of increasing the memory operation 1,2,3

Copy the code
change CPU

Need to modify the configuration file, it is necessary to stop the virtual machine

virsh shutdown kvm-1
virsh edit kvm-1

2 # 4 > 2

virsh create / etc / libvirt / demu / kvm-1 / xml
hard disk expansion

Copy the code

  1. Create a 10-GB non-sparse file:

    dd if=/dev/zero of=/vm-images/vm1-add.img bs=1M count=10240

  2. Shutdown the VM:
    # virsh shutdown vm1
  3. Add an extra entry for ‘disk’ in the VM's XML file in /etc/libvirt/qemu. You can look copy & paste the entry for your mail storage device and just change the target and address tags. For example:
    # virsh edit vm1






    Add:






    # It is recommended to use the above way to add a hard disk to add
    copy the code
    to delete a virtual machine

The first step, stopped virtual machine

virsh shutdown kvm-1
Step

virsh destroy kvm-1
Step

virsh undefine kvm-1
fourth step

rm / dev / vg_shkvm1 / kvm-1 # is not recommended to remove the hard disk

reference:

https://www.cnblogs.com/yexiaochong/p/6059956.html (above partially switched this article)

http://blog.csdn.net/wh211212/article/details/74908390 (above partially switched this article)

http://www.mamicode.com/info-detail-1715145.html

Guess you like

Origin www.cnblogs.com/yspworld/p/11343937.html