KVM 热添加、扩容

 热添加硬盘

创建磁盘

qemu-img create -f qcow2 /opt/kvm/vm/vdc.qcow2 3G
qemu-img create -f qcow2 /opt/kvm/vm/vdb.qcow2 5G

添加磁盘

[root@kafka-1 kvm]# virsh attach-disk test-bridge-vm /opt/kvm/vm/web02-br0-vdb.qcow2 vdb --subdriver qcow2 --config
Disk attached successfully
参数解释:
--config  下次重启永久生效,当前不生效
--subdriver qcow2  默认以raw识别

立即生效
[root@kafka-1 kvm]# virsh attach-disk test-bridge-vm /opt/kvm/vm/web02-br0-vdb.qcow2 vdb --subdriver qcow2

查看是否挂载成功

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0   10G  0 disk
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0    9G  0 part
  ├─centos-root 253:0    0    8G  0 lvm  /
  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
vdb             252:16   0    5G  0 disk

 磁盘分区,格式化,挂载

[root@localhost ~]# parted /dev/vdb
GNU Parted 3.1
使用 /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
(parted) mkpart primary 1 100%
(parted) align-check optimal 1
1 aligned
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     标志
 1      1049kB  5368MB  5367MB               primary

(parted) quit
信息: You may need to update /etc/fstab.

[root@localhost ~]# partprobe
[root@localhost ~]#  fdisk -lu /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

磁盘 /dev/vdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:gpt
Disk identifier: FEA11E59-A9F2-4741-B1DD-3F2C3FFECA7D


#         Start          End    Size  Type            Name
 1         2048     10483711      5G  Microsoft basic primary

[root@localhost ~]#  mkfs -t ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310208 blocks
65510 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 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

Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

[root@localhost ~]# mkdir /data
[root@localhost ~]# echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /data ext4 defaults 0 0 >> /etc/fstab
[root@localhost ~]#

[root@localhost ~]# mount -a
[root@localhost ~]# df -hT
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  484M     0  484M    0% /dev
tmpfs                   tmpfs     496M     0  496M    0% /dev/shm
tmpfs                   tmpfs     496M  6.8M  489M    2% /run
tmpfs                   tmpfs     496M     0  496M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       8.0G  1.8G  6.3G   22% /
/dev/vda1               xfs      1014M  137M  878M   14% /boot
tmpfs                   tmpfs     100M     0  100M    0% /run/user/0
/dev/vdb1               ext4      4.8G   20M  4.6G    1% /data

剥离磁盘

[root@kafka-1 kvm]# virsh detach-disk test-bridge-vm vdc
Disk detached successfully

# 永久生效,当前不生效
[root@kafka-1 kvm]# virsh detach-disk test-bridge-vm vdc --config
Disk detached successfully

 写入数据到磁盘,剥离后重新挂载

[root@kafka-1 kvm]# virsh detach-disk test-bridge-vm vdc
Disk detached successfully

重新挂载
[root@kafka-1 kvm]# virsh attach-disk test-bridge-vm /opt/kvm/vm/web02-br0-vdc.qcow2 vdc --subdriver qcow2
Disk attached successfully

在虚机查看
[root@localhost data2]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0   10G  0 disk
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0    9G  0 part
  ├─centos-root 253:0    0    8G  0 lvm  /
  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
vdb             252:16   0    5G  0 disk
└─vdb1          252:17   0    5G  0 part /data
vdd             252:48   0    5G  0 disk         # 不能被识别为vdc,而是顺序增加为vdd
└─vdd1          252:49   0    5G  0 part

[root@localhost data2]# blkid
/dev/vdb1: UUID="88a74f33-1ae6-44fd-bf51-7ffb8d507b38" TYPE="ext4" PARTLABEL="primary" PARTUUID="ab8d3b97-8df4-40aa-bba0-b262a95b80e0"
/dev/vda1: UUID="48d46bb2-9964-4564-8178-c0cd3676afd3" TYPE="xfs"
/dev/vda2: UUID="doSPIy-8Pbb-zU5v-6K1b-mXMi-6XMA-c4Qn2p" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="907ee70a-ed75-44c7-95af-b1dbbcb6557e" TYPE="xfs"
/dev/mapper/centos-swap: UUID="bb519eed-32f5-48ba-b7ce-d44286b42ba5" TYPE="swap"
/dev/vdd1: UUID="a82cd262-e1d9-463c-8fd2-87e01529c580" TYPE="ext4" PARTLABEL="primary" PARTUUID="fa124edc-3cdc-42f1-9280-b1525100cc80"
[root@localhost data2]# cat /etc/fstab
...
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=48d46bb2-9964-4564-8178-c0cd3676afd3 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
UUID=88a74f33-1ae6-44fd-bf51-7ffb8d507b38 /data ext4 defaults 0 0
UUID=a82cd262-e1d9-463c-8fd2-87e01529c580 /data2 ext4 defaults 0 0   # 而uuid并未发生改变

[root@localhost data2]# mount -a [root@localhost data2]# df -hT 文件系统 类型 容量 已用 可用 已用% 挂载点 devtmpfs devtmpfs 484M 0 484M 0% /dev tmpfs tmpfs 496M 0 496M 0% /dev/shm tmpfs tmpfs 496M 6.8M 489M 2% /run tmpfs tmpfs 496M 0 496M 0% /sys/fs/cgroup /dev/mapper/centos-root xfs 8.0G 1.8G 6.3G 22% / /dev/vda1 xfs 1014M 137M 878M 14% /boot tmpfs tmpfs 100M 0 100M 0% /run/user/0 /dev/vdb1 ext4 4.8G 20M 4.6G 1% /data /dev/vdd1 ext4 4.8G 21M 4.6G 1% /data2 [root@localhost /]# ll 总用量 24 lrwxrwxrwx. 1 root root 7 7月 29 19:27 bin -> usr/bin dr-xr-xr-x. 5 root root 4096 7月 29 19:33 boot drwxr-xr-x. 3 root root 4096 7月 29 22:00 data drwxr-xr-x. 5 root root 4096 7月 29 22:07 data2 drwxr-xr-x. 21 root root 3260 7月 29 22:11 dev
# 数据仍在
[root@localhost /]# cd data2 [root@localhost data2]# ll 总用量 24 drwxr-xr-x. 2 root root 4096 7月 29 22:07 file drwx------. 2 root root 16384 7月 29 22:05 lost+found drwxr-xr-x. 2 root root 4096 7月 29 22:08 video [root@localhost data2]# ll video/ 总用量 0 -rw-r--r--. 1 root root 0 7月 29 22:08 1.mp4 -rw-r--r--. 1 root root 0 7月 29 22:08 2.mp4 [root@localhost data2]# ll file 总用量 12 -rw-r--r--. 1 root root 12 7月 29 22:07 index2.html -rw-r--r--. 1 root root 12 7月 29 22:07 index3.html -rw-r--r--. 1 root root 12 7月 29 22:06 index.html

热添加磁盘容量

[root@kafka-1 kvm]# qemu-img  resize /opt/kvm/vm/web02-br0-vdb.qcow2 +1G
Image resized.

[root@kafka-1 kvm]# virsh blockresize test-bridge-vm vdb 22G
Block device 'vdb' is resized

虚机扩容分区
type growpart || sudo yum install -y cloud-utils-growpart
sudo yum update cloud-utils-growpart
sudo LC_ALL=en_US.UTF-8 growpart /dev/vdb 1
sudo resize2fs  /dev/vdb1

扩容文件系统
[root@localhost /]#  resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vdb1 is mounted on /data; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
The filesystem on /dev/vdb1 is now 5766907 blocks long.

[root@localhost /]# df -hT
文件系统                类型      容量  已用  可用 已用% 挂载点
devtmpfs                devtmpfs  484M     0  484M    0% /dev
tmpfs                   tmpfs     496M     0  496M    0% /dev/shm
tmpfs                   tmpfs     496M  6.8M  489M    2% /run
tmpfs                   tmpfs     496M     0  496M    0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       8.0G  1.8G  6.3G   22% /
/dev/vda1               xfs      1014M  137M  878M   14% /boot
tmpfs                   tmpfs     100M     0  100M    0% /run/user/0
/dev/vdb1               ext4       22G   28M   21G    1% /data
/dev/vdd1               ext4      4.8G   21M  4.6G    1% /data2

[root@localhost /]# ll data
总用量 20
drwxr-xr-x. 2 root root  4096 7月  29 23:02 haha
-rw-r--r--. 1 root root     0 7月  29 23:02 index.html
drwx------. 2 root root 16384 7月  29 22:00 lost+found
-rw-r--r--. 1 root root     0 7月  29 23:02 test.txt

热添加网卡

[root@kafka-1 kvm]# virsh attach-interface test-bridge-vm --type bridge --source br0 --model virtio --config
Interface attached successfully

[root@kafka-1 kvm]# virsh attach-interface test-bridge-vm  --type bridge --source br0 --model virtio
Interface attached successfully

参数解释:
--type bridge   # 网卡类型
--source br0   # 桥接到br0
--model virtio  # 假如是eth0,新添加就是eth1 
--config  # 当前不生效,重启生效,类型为永久,不加config为临时添加,重启失效


虚机查看
[root@localhost /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.64.3  netmask 255.255.255.0  broadcast 192.168.64.255
        inet6 fe80::5e00:2097:3b56:a87b  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:06:46:32  txqueuelen 1000  (Ethernet)
        RX packets 55156  bytes 67268996 (64.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48845  bytes 4928081 (4.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.64.5  netmask 255.255.255.0  broadcast 192.168.64.255
        inet6 fe80::8835:827b:d545:23db  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:6a:ed:38  txqueuelen 1000  (Ethernet)
        RX packets 363  bytes 19288 (18.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13  bytes 1614 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

热添加内存

# 在创建虚机的时候指定最大内存和cpu核心数
virt-install --name=hotmodify-bridge-vm \
--vcpus=1,maxvcpus=4 --memory=1024,maxmemory=4096 \
--disk path=/opt/kvm/vm/hotmodify-br0.qcow2,size=10 \
--cdrom=/opt/kvm/iso/CentOS-7-x86_64-Minimal-2009.iso \
--os-variant=rhel7 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0,port=30005 \
--noautoconsole --autostart
# 扩容虚机内存
[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 4096M --live

 当最大内存上限后,需要关闭当前虚机,然后可以使用命令或者编辑xml文件设置最大管理员允许设置最大内存。

[root@kafka-1 kvm]# virsh setmaxmem hotmodify-bridge-vm 6096M --config
或者
[root@kafka-1 kvm]# virsh edit hotmodify-bridge-vm
<domain type='kvm'>
  <name>hotmodify-bridge-vm</name>
  <uuid>fe558fd2-6c82-4e4e-a4fd-e1be06ecb232</uuid>
  <memory unit='KiB'>6242304</memory>
  <currentMemory unit='KiB'>3072000</currentMemory>
  <vcpu placement='static' current='1'>4</vcpu>

 调整虚机内存

[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 4096M --live

[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 4097M --live

[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 6096M --live

[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 6097M --live
error: 无效参数:无法将内存设置为高于最大内存

 使用edit 虚机xml文件修改最大内存,需要关机

<domain type='kvm'>
  <name>hotmodify-bridge-vm</name>
  <uuid>fe558fd2-6c82-4e4e-a4fd-e1be06ecb232</uuid>
  <memory unit='KiB'>3197376</memory>
  <currentMemory unit='KiB'>3196928</currentMemory>
  <vcpu placement='static' current='1'>4</vcpu>

调整虚机内存

[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 3122M --config

[root@kafka-1 kvm]# virsh setmem hotmodify-bridge-vm 3123M --config
error: 无效参数:无法将内存设置为高于最大内存

热添加cpu

在创建虚机的时候指定虚机内存,管理员可以设置的最大cpu
注意:cpu核心数只能扩,不能缩小
virt-install --name=hotmodify-bridge-vm \
--vcpus=1,maxvcpus=4 --memory=1024,maxmemory=4096 \
--disk path=/opt/kvm/vm/hotmodify-br0.qcow2,size=10 \
--cdrom=/opt/kvm/iso/CentOS-7-x86_64-Minimal-2009.iso \
--os-variant=rhel7 \
--network bridge=br0 \
--graphics vnc,listen=0.0.0.0,port=30005 \
--noautoconsole --autostart

热扩容cpu核心数

# 立即生效,但是重启失效
[root@kafka-1 ~]# virsh  setvcpus hotmodify-bridge-vm 4 --live

# 重启生效,当前不生效
[root@kafka-1 ~]# virsh  setvcpus hotmodify-bridge-vm 4 --config

当设置超过可以配置最大cpus数量会报错,并且不支持热修改最大cpu核心数。

[root@kafka-1 ~]# virsh  setvcpus hotmodify-bridge-vm 5
error: 无效参数:requested vcpus is greater than max allowable vcpus for the live domain: 5 > 4

# 修改最大cpu核心数
1.关机
[root@kafka-1 ~]# virsh shutdown hotmodify-bridge-vm

2.修改配置虚机文件
[root@kafka-1 ~]# virsh edit hotmodify-bridge-vm
<domain type='kvm'>
  <name>hotmodify-bridge-vm</name>
  <uuid>fe558fd2-6c82-4e4e-a4fd-e1be06ecb232</uuid>
  <memory unit='KiB'>4194304</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static' current='2'>5</vcpu>
  <os>

3.开启并设置cpu核心数为5
[root@kafka-1 ~]# virsh setvcpus hotmodify-bridge-vm 5

[root@kafka-1 ~]#
[root@kafka-1 ~]# virsh setvcpus hotmodify-bridge-vm 6
error: 无效参数:requested vcpus is greater than max allowable vcpus for the live domain: 6 > 5

 

posted @ 2024-07-29 20:42  不会跳舞的胖子  阅读(16)  评论(0编辑  收藏  举报