KVM管理

https://blog.csdn.net/wh211212/article/details/74908390

管理kvm虚拟机常用的虚拟机管理命令
列出所有的虚拟机virsh list --all1显示虚拟机信息virsh dominfo kvm-11显示虚拟机内存和cpu的使用情况yum install virt-top -yvirt-top12显示虚拟机分区信息virt-df kvm-11关闭虚拟机(shutodwn)virsh shutdown kvm-11启动虚拟机virsh start kvm-11设置虚拟机(kvm-1)跟随系统自启virsh autostart kvm-11关闭虚拟及自启virsh autostart --disable kvm-11删除虚拟机virsh undefine kvm-11通过控制窗口登录虚拟机virsh console kvm-11给虚拟机添加硬盘添加硬盘(lvm卷)或者USB到虚拟机上
virsh attach-disk kvm-1 /dev/sdb vbd --driver qemu --mode shareable1使用完成之后可以卸载usbvirsh detach-disk kvm vdb1添加lvm卷,并挂载
[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 shareableDisk attached successfully# 登录到kvm-1上查看lvm是否已经被挂载[root@sh-kvm-1 ~]# virsh console kvm-1 # 输入kvm-1的用户和密码[root@sh-kvm-1-1 ~]# fdisk -l # 查看硬盘挂载情况
Disk /dev/vda: 21.5 GB, 21474836480 bytes16 heads, 63 sectors/track, 41610 cylindersUnits = cylinders of 1008 * 512 = 516096 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00058197
   Device Boot      Start         End      Blocks   Id  System/dev/vda1   *           3        1018      512000   83  LinuxPartition 1 does not end on cylinder boundary./dev/vda2            1018       41611    20458496   8e  Linux LVMPartition 2 does not end on cylinder boundary.
Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes255 heads, 63 sectors/track, 2285 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000

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

Disk /dev/vdb: 53.7 GB, 53687091200 bytes  # 新添加的硬盘16 heads, 63 sectors/track, 104025 cylindersUnits = cylinders of 1008 * 512 = 516096 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000123456789101112131415161718192021222324252627282930313233343536373839404142格式化新添加的vdb,并添加到lvm组中# 对新添加的硬盘分区[root@sh-kvm-1-1 ~]# fdisk /dev/vdbDevice contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding 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): 1First cylinder (1-104025, default 1):Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-104025, default 104025):Using default value 104025
Command (m for help): t  #改变分区的格式Selected partition 1Hex code (type L to list codes): 8e  #改成lvmChanged system type of partition 1 to 8e (Linux 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: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks3276800 inodes, 13107142 blocks655357 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4294967296400 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,    4096000, 7962624, 11239424
Writing inode tables: doneCreating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.[root@sh-kvm-1-1 ~]# pvc reate /dev/vdb1   # 创建pvvdb   vdb1[root@sh-kvm-1-1 ~]# vgextend VolGroup /dev/vdb  # 扩展lvm vgvdb   vdb1[root@sh-kvm-1-1 ~]# vgs  VG       #PV #LV #SN Attr   VSize  VFree  VolGroup   2   2   0 wz--n- 69.50g 50.00g# 从上面能看出,新添加的 已经加到lvm组中12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182改变虚拟机的参数通过命令行更改创建之后虚拟机的内存,cpu等信息
更改内存# 1. 查看虚拟机当前内存[root@sh-kvm-1 ~]# virsh dominfo kvm-1 | grep memoryMax memory:     4194304 KiBUsed memory:    4194304 KiB
# 2、动态设置内存为512MB,内存减少virsh setmem kvm-1 524288# 注意单位必须是KB
# 3、查看内存变化# virsh dominfo kvm-1 | grep memoryMax memory: 14194304 KiBUsed memory: 524288 kiB
# 4、内存增加virsh shutdown kvm-1virsh edit kvm-1  # 直接更改memoryvirsh create /etc/libvirt/demu/kvm-1/xml# 之后操作1,2,3步骤增加内存12345678910111213141516171819更改CPU需要修改配置文件,因此需要停止虚拟机
virsh shutdown kvm-1virsh edit kvm-1#  <vcpu>2</vcpu>  # 4 > 2virsh create /etc/libvirt/demu/kvm-1/xml1234硬盘扩容1. Create a 10-GB non-sparse file:# dd if=/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. You can look copy & pastethe entry for your mail storage device and just change the target and address tags. For example: # virsh edit vm1 <disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none' io='threads'/> <source file='/vm-images/vm1.img'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04'function='0x0'/> </disk> Add: <disk type='file' device='disk'> <driver name='qemu' type='raw' cache='none' io='threads'/> <source file='/vm-images/vm1-add.img'/> <target dev='vdb' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06'function='0x0'/> </disk> # 这里建议使用上面的添加硬盘的方式添加1234567891011121314151617181920212223删除虚拟机第一步,停掉虚拟机virsh shutdown kvm-11第二步virsh destroy kvm-11第三步virsh undefine kvm-11第四部 rm /dev/vg_shkvm1/kvm-1  # 不建议删除硬盘--------------------- 版权声明:本文为CSDN博主「shaonbean」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/wh211212/article/details/74908390

猜你喜欢

转载自www.cnblogs.com/chenjiye/p/11358367.html