常用的虚拟机管理命令

列出所有的虚拟机

virsh list --all

显示虚拟机信息

virsh dominfo ubuntu_1

显示虚拟机内存和cpu的使用情况

apt install -y virt-top
virt-top

显示虚拟机分区信息

apt install -y guestfs-tools
virt-df ubuntu_1

关闭虚拟机

virsh shutdown ubuntu_1

启动虚拟机

virsh start ubuntu_1

设置虚拟机跟随系统自启

virsh autostart ubuntu_1

关闭虚拟及自启

virsh autostart --disable ubuntu_1

删除虚拟机

virsh undefine ubuntu_1

通过控制窗口登录虚拟机

# 登录虚拟机执行
systemctl enable getty@ttyS0
# 重启
reboot
# 在宿主机上执行
virsh console ubuntu_1

虚拟机添加硬盘(未验证)

添加硬盘(lvm卷)或者USB到虚拟机上

virsh attach-disk ubuntu_1 /dev/sdb vbd --driver qemu --mode shareable

使用完成之后可以卸载usb

virsh detach-disk ubuntu_1 vbd

添加lvm卷,并挂载

[root@ubuntu ~]# lvcreate -n ubuntu-data -L 50G vg_shkvm1
[root@ubuntu ~]# virsh attach-disk ubuntu_1 /dev/vg_shkvm1/ubuntu-data vdb --driver qemu --mode shareable
Disk attached successfully
# 登录到ubuntu_1上查看lvm是否已经被挂载
[root@ubuntu ~]# virsh console ubuntu_1 # 输入ubuntu_1的用户和密码
[root@ubuntu_1 ~]# fdisk -l # 查看硬盘挂载情况

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  # 新添加的硬盘
16 heads, 63 sectors/track, 104025 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: 0x00000000

格式化新添加的vdb,并添加到lvm组中

对新添加的硬盘分区

[root@ubuntu_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  #改变分区的格式
Selected partition 1
Hex code (type L to list codes): 8e  #改成lvm
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w  # 保存更改
root@ubuntu_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@ubuntu_1 ~]# pvc reate /dev/vdb1   # 创建pv
vdb   vdb1
[root@ubuntu_1 ~]# vgextend VolGroup /dev/vdb  # 扩展lvm vg
vdb   vdb1
[root@ubuntu_1 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  VolGroup   2   2   0 wz--n- 69.50g 50.00g
# 从上面能看出,新添加的 已经加到lvm组中

改变虚拟机的参数

通过命令行更改创建之后虚拟机的内存,cpu等信息

更改内存

# 1. 查看虚拟机当前内存
# virsh dominfo ubuntu_1 | grep memory
Max memory:     4194304 KiB
Used memory:    4194304 KiB

# 2、动态设置内存为512MB,内存减少
virsh setmem ubuntu_1 524288
# 注意单位必须是KB

# 3、查看内存变化
# virsh dominfo ubuntu_1 | grep memory
Max memory: 14194304 KiB
Used memory: 524288 kiB

# 4、内存增加
virsh shutdown ubuntu_1
virsh edit ubuntu_1  # 直接更改memory
virsh create /etc/libvirt/qemu/ubuntu_1/xml
# 之后操作1,2,3步骤增加内存

更改CPU

需要修改配置文件,因此需要停止虚拟机

virsh shutdown ubuntu_1
virsh edit ubuntu_1
#  <vcpu placement='static'>2</vcpu>  # 4 > 2
virsh create /etc/libvirt/qemu/ubuntu_1/xml

硬盘扩容

在宿主机操作

virsh blockresize  ubuntu_1 /data/kvm/ubuntu_1.img 150G

在虚拟机操作

获取文件系统信息
lsblk

需要调整的逻辑卷:ubuntu--vg-lv--data

获取逻辑卷信息
lvdisplay

获取卷组信息
vgdisplay

扩容卷组
fdisk /dev/vda

红色字体报错修复

# apt-get install parted
# parted -l
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 31.1GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  31.1GB  31.1GB  ext4


Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/ubuntu--vg-lv--data: 74.1GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  74.1GB  74.1GB  ext4


Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0
has been opened read-only.
Error: /dev/sr0: unrecognised disk label
Model: QEMU QEMU DVD-ROM (scsi)                                           
Disk /dev/sr0: 2048B
Sector size (logical/physical): 2048B/2048B
Partition Table: unknown
Disk Flags: 

Warning: Not all of the space available to /dev/vda appears to be used, you can
fix the GPT to use all of the space (an extra 104857600 blocks) or continue with
the current setting? 
Fix/Ignore? Fix 
# 填入 Fix                                                   
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 161GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  2097kB  1049kB                     bios_grub
 2      2097kB  2150MB  2147MB  ext4
 3      2150MB  107GB   105GB

确认磁盘分区新建成功后,新建对应的物理卷,并加入到对应的卷组中

# pvcreate /dev/vda4
  Physical volume "/dev/vda4" successfully created.
# vgextend ubuntu-vg /dev/vda4
  Volume group "ubuntu-vg" successfully extended
# lvextend -L +49.9G /dev/mapper/ubuntu--vg-lv--data -r
  Rounding size to boundary between physical extents: 49.90 GiB.
  Size of logical volume ubuntu-vg/lv-data changed from 69.00 GiB (17664 extents) to 118.90 GiB (30439 extents).
  Logical volume ubuntu-vg/lv-data successfully resized.
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/mapper/ubuntu--vg-lv--data is mounted on /data; on-line resizing required
old_desc_blocks = 9, new_desc_blocks = 15
The filesystem on /dev/mapper/ubuntu--vg-lv--data is now 31169536 (4k) blocks long.

查看扩容结果

删除虚拟机

第一步,停掉虚拟机

virsh shutdown ubuntu_1

第二步

virsh destroy ubuntu_1

第三步

virsh undefine ubuntu_1

第四部

rm /data/kvm/ubuntu_1  # 不建议删除硬盘

文章转载自其他地方,由于错误内容较多,部分内容未验证。

克隆虚拟机

停机克隆

virsh shutdown ubuntu_1
virt-clone  --connect qemu:///system  --original ubuntu_1  --name ubuntu_2  --file /data/kvm/ubuntu_2.img
virsh start ubuntu_2
#注意 先修改ubuntu_2 ip
virsh start ubuntu_1