乐趣区

关于kvm:KVM-给虚拟机新增一块磁盘

前情

需要就是普普通通的给 kvm 下的一台虚拟机加 1000G 的磁盘

计划一

  • 虚拟机不关机在线增加磁盘
1. 查看指标 vm4 的磁盘 状况
2. 在 kvm 主机上创立一个新的虚构磁盘
root@szo-192:/opt/kvm/image# qemu-img create -f qcow2 -o size=1000G /opt/kvm/image/kvm-vm4-02-1T.qcow2
Formatting '/opt/kvm/image/kvm-vm4-02-1T.qcow2', fmt=qcow2 size=1073741824000 cluster_size=65536 lazy_refcounts=off refcount_bits=16
3. 从 kvm 主机挂载盘到 vm4 上
root@szo-192:/opt/kvm/image# ls
kvm-vm1_1.qcow2  kvm-vm2.qcow2  kvm-vm4-02-1T.qcow2  kvm-vm5.qcow2  kvm-vm7.qcow2
kvm-vm1.qcow2    kvm-vm3.qcow2  kvm-vm4.qcow2        kvm-vm6.qcow2  kvm-vm8.qcow2
root@szo-192:/opt/kvm/image# virsh attach-disk --domain kvm-vm4 --source /opt/kvm/image/kvm-vm4-02-1T.qcow2 --target vdb --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent
Disk attached successfully

4. 验证

kvm 主机端:virsh domblklist kvm-vm4

在 vm4

扩容 /home 分区

[root@vm4 ~]# fdisk -l

Disk /dev/vda: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a6a14

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    18616319     8258560   82  Linux swap / Solaris
/dev/vda3        18616320   123473919    52428800   83  Linux
/dev/vda4       123473920  1048575999   462551040    5  Extended
/dev/vda5       123475968  1048575999   462550016   83  Linux

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@vm4 ~]# 
[root@vm4 ~]# 
[root@vm4 ~]# 
[root@vm4 ~]# fdisk /dev/vdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xff552d5a.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-2097151999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): 
Using default value 2097151999
Partition 1 of type Linux and of size 1000 GiB is set

Command (m for help): p

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xff552d5a

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  2097151999  1048574976   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@vm4 ~]# partprobe
[root@vm4 ~]# fdisk -l

Disk /dev/vda: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000a6a14

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    18616319     8258560   82  Linux swap / Solaris
/dev/vda3        18616320   123473919    52428800   83  Linux
/dev/vda4       123473920  1048575999   462551040    5  Extended
/dev/vda5       123475968  1048575999   462550016   83  Linux

Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xff552d5a

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048  2097151999  1048574976   83  Linux
[root@vm4 ~]# 
[root@vm4 ~]# 
[root@vm4 ~]# 
[root@vm4 ~]# pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.

[root@vm4 ~]# vgdisplay 
  selabel_open failed: No such file or directory
[root@vm4 ~]# pvdisplay 

  "/dev/vdb1" is a new physical volume of "<1000.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               <1000.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               CNDRjs-534c-FK5w-EcCY-WAr1-EKsk-RMyWQw
   
[root@vm4 ~]# vgcreate -s 64M centos /dev/vdb1
  Volume group "centos" successfully created
[root@vm4 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/vdb1
  VG Name               centos
  PV Size               <1000.00 GiB / not usable 63.00 MiB
  Allocatable           yes 
  PE Size               64.00 MiB
  Total PE              15999
  Free PE               15999
  Allocated PE          0
  PV UUID               CNDRjs-534c-FK5w-EcCY-WAr1-EKsk-RMyWQw
   

[root@vm4 ~]# vgs

  VG     #PV #LV #SN Attr   VSize    VFree   
  centos   1   0   0 wz--n- <999.94g <999.94g
[root@vm4 ~]# vgdisplay 

  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <999.94 GiB
  PE Size               64.00 MiB
  Total PE              15999
  Alloc PE / Size       0 / 0   
  Free  PE / Size       15999 / <999.94 GiB
  VG UUID               cPr09p-NEI9-AqzI-dplr-236J-cY4q-89WajD

[root@vm4 ~]# pvdisplay 

  --- Physical volume ---
  PV Name               /dev/vdb1
  VG Name               centos
  PV Size               <1000.00 GiB / not usable 63.00 MiB
  Allocatable           yes 
  PE Size               64.00 MiB
  Total PE              15999
  Free PE               15999
  Allocated PE          0
  PV UUID               CNDRjs-534c-FK5w-EcCY-WAr1-EKsk-RMyWQw

[root@vm4 ~]# mkdir /data
[root@vm4 ~]# lvcreate -L 999G -n data centos

  Logical volume "data" created.
[root@vm4 ~]# echo $?
0
[root@vm4 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/data
  LV Name                data
  VG Name                centos
  LV UUID                pFjVfe-WyxD-Wldo-zkyr-JDhY-mt2a-JK0W61
  LV Write Access        read/write
  LV Creation host, time vm4, 2023-06-19 17:31:11 +0800
  LV Status              available
  # open                 0
  LV Size                999.00 GiB
  Current LE             15984
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0

[root@vm4 ~]# mkfs.xfs /dev/centos/data
meta-data=/dev/centos/data       isize=512    agcount=4, agsize=65470464 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=261881856, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=127872, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@vm4 ~]# echo $?
0
[root@vm4 ~]# vim /etc/fstab
[root@vm4 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Jun 13 05:25:45 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=76f19609-5d15-47ec-8334-1070e09a71a6 /                       xfs     defaults        0 0
UUID=5b531ad0-8ba7-443c-b873-cfedd11caaa3 /boot                   xfs     defaults        0 0
UUID=3ed24818-0c16-4e69-9cf9-de77429a868f /home                   xfs     defaults        0 0
#UUID=16616dc6-ef8f-4cc2-be9a-45897bd963fa swap                    swap    defaults        0 0
/dev/centos/data         /data                  xfs    defaults        0 0 
[root@vm4 ~]# mount -a
[root@vm4 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 7.8G     0  7.8G   0% /dev
tmpfs                    7.8G     0  7.8G   0% /dev/shm
tmpfs                    7.8G  102M  7.7G   2% /run
tmpfs                    7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda3                 50G  3.5G   47G   7% /
/dev/vda5                441G  1.9G  440G   1% /home
/dev/vda1               1014M  170M  845M  17% /boot
/dev/mapper/centos-data  999G   33M  999G   1% /data

计划二

  • 虚构机关机新增磁盘
# 第一:创立一块磁盘
    qemu-img create -f raw /kvm-hosts/kvm-web011.raw 10G
#第二:编辑 xml 文件,在本来磁盘信息的上面,增加如下内容
    virsh edit kvm-web01

<disk type='file' device='disk'>
      <driver name='qemu' type='raw'></driver>
      <source file='/kvm-hosts/kvm-web011.raw'></source>
      <target dev='vdb' bus='virtio'></target>
    </disk>
    #留神点:vdb,virtio,type,file

#第三:启动虚拟机,并查看配置
    virsh start kvm-web01
    ps -ef |grep kvm
#第四:进入虚拟机,格式化,挂载应用 
退出移动版