通过qemu-img 命令创建qcow2的磁盘,也可以创建其他类型的磁盘,但扩展磁盘的时候,在kvm里面的命令有所区别
[root@server10 ~]# qemu-img create -f qcow2 /opt/extern.qcow2 100G
Formatting '/opt/extern.qcow2', fmt=qcow2 size=107374182400 encryption=off
[root@server10 opt]# qemu-img info extern.qcow2
image: extern.qcow2
file format: qcow2
virtual size: 100G (107374182400 bytes)
disk size: 294M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
直接挂载磁盘到虚拟机.虚拟机需要在运行状态可直接看到有其他硬盘被挂载
[root@server10 opt]# virsh attach-disk centos7V1-bigdata /opt/extern.qcow2 vdb --subdriver=qcow2 --persistent
virsh help attach-disk
NAME
attach-disk - attach disk deviceSYNOPSIS
attach-diskDESCRIPTION
Attach new disk device.OPTIONS
[--domain]domain name, id or uuid
[--source]source of disk device
[--target]target of disk device
--targetbustarget bus of disk device
--driverdriver of disk device
--subdriversubdriver of disk device
--iothreadIOThread to be used by supported device
--cachecache mode of disk device
--ioio policy of disk device
--typetarget device type
--modemode of device reading and writing
--sourcetypetype of source (block|file)
--serialserial of disk device
--wwnwwn of disk device
--rawio needs rawio capability
--addressaddress of disk device
--multifunction use multifunction pci under specified address
--print-xml print XML document rather than attach the disk
--persistent make live change persistent
--config affect next boot
--live affect running domain
--current affect current domain
选择卷标必须使用8e,这个表示是LVM磁盘格式
[root@localhost ~]# fdisk /dev/vdc
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 0x14401f11.
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): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
[38384.918771] vdc: vdc1
Syncing disks.
[root@localhost ~]# [38384.934024] vdc: vdc1
我们进入虚拟机系统,然后看下系统硬盘的情况,可以看到硬盘已经被虚拟机监测到.
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x000be9e1
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 20971519 9436160 8e Linux LVM
Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x8e9d6aff
Device Boot Start End Blocks Id System
/dev/vdb1 2048 209715199 104856576 8e Linux LVM
从物理磁盘创建LVM磁盘分区.
[root@localhost ~]# pvcreate /dev/vdb
Physical volume "/dev/vdb1" successfully created.
VG是逻辑卷,可以容纳很多PE,以后可以扩展VG达到增长磁盘大小的作用,这里我们命名逻辑卷名称VolGroup01
[root@localhost ~]# vgcreate VolGroup01 /dev/vdb1
Volume group "VolGroup01" successfully created
LV是真正分给应用程序使用的分区,属于VG.在VG容量许可的情况下,LV可以自己扩展大小,
[root@localhost ~]# lvcreate -L 10G -n lv01 VolGroup01
Logical volume "lv01" created.
[root@localohst ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID JrHsb3-E9Ym-Rh79-iosJ-hzHJ-ylPD-Gcc4Co
LV Write Access read/write
LV Creation host, time localhost, 2020-12-23 18:47:47 +0800
LV Status available
# open 1
LV Size 10 GiB
Current LE 11047
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/VolGroup01/lv01
LV Name lv01
VG Name VolGroup01
LV UUID Fjk4G9-g2rP-W8h4-JEEF-2q3F-kykM-cjBoMt
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2021-04-04 23:30:12 +0800
LV Status available
# open 0
LV Size 10.00 GiB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@localhost ~]# mkfs -t ext4 /dev/VolGroup01/lv01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done
[root@localhost ~]# mkdir /extend
[root@localhost ~]# mount /dev/VolGroup01/lv01 /extend
[ 498.219610] EXT4-fs (dm-2): mounted filesystem with ordered data mode. Opts: (null)
[root@localhost ~]# cd /extend/
[root@localhost extend]# ls
lost+found
[root@localhost extend]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 894512 0 894512 0% /dev
tmpfs 906156 0 906156 0% /dev/shm
tmpfs 906156 8664 897492 1% /run
tmpfs 906156 0 906156 0% /sys/fs/cgroup
/dev/mapper/centos-root 8374272 3695940 4678332 45% /
/dev/vda1 1038336 196740 841596 19% /boot
tmpfs 181232 0 181232 0% /run/user/0
/dev/mapper/VolGroup01-lv01 10190100 36888 9612540 1% /extend
直接扩展磁盘就是扩大系统原有的逻辑分区,而不要将新的磁盘直接挂载到某个目录.这种方法应该更加流畅.
[root@server10 opt]# qemu-img create -f qcow2 /opt/vgextend.qcow2 100G
Formatting '/opt/vgextend.qcow2', fmt=qcow2 size=107374182400 encryption=off cluster_size=65536 lazy_refcounts=off
[root@server10 opt]# virsh attach-disk centos7V1-bigdata /opt/vgextend.qcow2 vdc --subdriver=qcow2
[root@localhost ~]# fdisk /dev/vdc
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 0x14401f11.
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): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
[38384.918771] vdc: vdc1
Syncing disks.
[root@localhost ~]# [38384.934024] vdc: vdc1
到KVM虚拟机内部,查看下需要扩展的磁盘,使用命令vgdisplay,我们扩展centos逻辑分区
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 10 GiB
PE Size 4.00 MiB
Total PE 27902
Alloc PE / Size 2303 / <9.00 GiB
Free PE / Size 25599 / <100.00 GiB
VG UUID DHsBK6-fe7b-n4V2-H5Lx-1p1g-oaPI-HWwS6k
--- Volume group ---
VG Name VolGroup01
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <100.00 GiB
PE Size 4.00 MiB
Total PE 25599
Alloc PE / Size 2560 / 10.00 GiB
Free PE / Size 23039 / <90.00 GiB
VG UUID OIaURj-GFML-uhcA-ibgz-9tZg-OPfP-Obtak5
[root@localhost ~]# vgextend centos /dev/vdc
Physical volume "/dev/vdc" successfully created.
Volume group "centos" successfully extended
再查看分期,看到VG的大小已经扩大,就是说包含了我们刚才创建的磁盘.
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 108.99 GiB
PE Size 4.00 MiB
Total PE 27902
Alloc PE / Size 2303 / <9.00 GiB
Free PE / Size 25599 / <100.00 GiB
VG UUID DHsBK6-fe7b-n4V2-H5Lx-1p1g-oaPI-HWwS6k
--- Volume group ---
VG Name VolGroup01
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <100.00 GiB
PE Size 4.00 MiB
Total PE 25599
Alloc PE / Size 2560 / 10.00 GiB
Free PE / Size 23039 / <90.00 GiB
VG UUID OIaURj-GFML-uhcA-ibgz-9tZg-OPfP-Obtak5
扩展好了默认系统不会立刻有效果
[root@localhost ~]# lvextend -l +9000 /dev/centos/root (9000是柱面)
Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to 43.15 GiB (11047 extents).
Logical volume centos/root successfully resized.
# 上述命令也可以使用 -L 30000m表示增加30G空间
使用resize2fs 继续查看 ,发现没增加,实际上系统目前用的是xfs系统,使用xfs_growfs命令会看到系root分区扩大成功
[root@localhost ~]# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.
[root@localhost ~]# mount |grep root
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=524032 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2096128, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2096128 to 11312128
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 874M 0 874M 0% /dev
tmpfs tmpfs 885M 0 885M 0% /dev/shm
tmpfs tmpfs 885M 8.5M 877M 1% /run
tmpfs tmpfs 885M 0 885M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 44G 3.6G 40G 9% /
/dev/vda1 xfs 1014M 193M 822M 19% /boot
tmpfs tmpfs 177M 0 177M 0% /run/user/0
更改完成