给虚拟机挂载一块硬盘(以ubuntu24.04为例)
2024-10-28 14:31 猎手家园 阅读(5) 评论(0) 编辑 收藏 举报一、新增、分区、格式化新盘
1、首先在虚拟机中增加一块新硬盘(500G)
例如:Vmware、Exsi 软件,增加完成后,查看一下:
root@ubuntu:~# lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT32 0CC8-757B 1G 1% /boot/efi ├─sda2 ext4 1.0 602d4169-741c-47c2-9d63-686cc0a09f0d 1.7G 5% /boot └─sda3 LVM2_member LVM2 001 AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE └─ubuntu--vg-ubuntu--lv ext4 1.0 8b41073e-f1ba-412e-9eb2-8127713f827d 84.4G 7% / sdb sr0
其中sdb就是新增加的硬盘。
可以查看更详细的硬盘情况
root@ubuntu:~# fdisk -l Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: A226A3F1-5348-41F9-A64B-0F2F00938DD1 Device Start End Sectors Size Type /dev/sda1 2048 2203647 2201600 1G EFI System /dev/sda2 2203648 6397951 4194304 2G Linux filesystem /dev/sda3 6397952 209713151 203315200 96.9G Linux filesystem Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 96.95 GiB, 104094236672 bytes, 203309056 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 /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 sectors Disk model: Virtual disk Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
请查标红的一行。
2、新硬盘分区(分区表)
root@ubuntu:~# fdisk /dev/sdb Welcome to fdisk (util-linux 2.39.3). 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. Created a new DOS (MBR) disklabel with disk identifier 0x7bfe4259. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-1048575999, default 2048): <直接回车> Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1048575999, default 1048575999): <直接回车> Created a new partition 1 of type 'Linux' and of size 500 GiB. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
3、查看新盘分区结果
root@ubuntu:~# lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT32 0CC8-757B 1G 1% /boot/efi ├─sda2 ext4 1.0 602d4169-741c-47c2-9d63-686cc0a09f0d 1.7G 5% /boot └─sda3 LVM2_member LVM2 001 AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE └─ubuntu--vg-ubuntu--lv ext4 1.0 8b41073e-f1ba-412e-9eb2-8127713f827d 84.4G 7% / sdb └─sdb1 sr0
说明分区成功了
4、将新的分区格式化成 ext4 格式
root@ubuntu:~# mkfs -t ext4 /dev/sdb1 mke2fs 1.47.0 (5-Feb-2023) Creating filesystem with 131071744 4k blocks and 32768000 inodes Filesystem UUID: 8fd776d3-9be5-4a59-8ce4-504bb14849e2 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000 Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done
记住新的UUID,说明格式化成功。
查看一下格式化结果:
root@ubuntu:~# lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT32 0CC8-757B 1G 1% /boot/efi ├─sda2 ext4 1.0 602d4169-741c-47c2-9d63-686cc0a09f0d 1.7G 5% /boot └─sda3 LVM2_member LVM2 001 AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE └─ubuntu--vg-ubuntu--lv ext4 1.0 8b41073e-f1ba-412e-9eb2-8127713f827d 84.4G 7% / sdb └─sdb1 ext4 1.0 8fd776d3-9be5-4a59-8ce4-504bb14849e2 sr0
二、挂载新盘
1、查看卷分组
root@ubuntu:~# vgdisplay -v --- Volume group --- VG Name ubuntu-vg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size <96.95 GiB PE Size 4.00 MiB Total PE 24818 Alloc PE / Size 24818 / <96.95 GiB Free PE / Size 0 / 0 VG UUID O814Hq-CEoa-S0E4-Vl40-bNre-XJV4-cOdaSv --- Logical volume --- LV Path /dev/ubuntu-vg/ubuntu-lv LV Name ubuntu-lv VG Name ubuntu-vg LV UUID y19FTz-xW8K-A5Mn-g0jD-HVQY-06mh-RRb2RQ LV Write Access read/write LV Creation host, time ubuntu-server, 2024-10-26 03:23:59 +0000 LV Status available # open 1 LV Size <96.95 GiB Current LE 24818 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0 --- Physical volumes --- PV Name /dev/sda3 PV UUID AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE PV Status allocatable Total PE / Free PE 24818 / 0
先保存一下信息,一会做对比。
2、创建物理卷
root@ubuntu:~# pvcreate /dev/sdb1 WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/sdb1. Physical volume "/dev/sdb1" successfully created.
查看刚刚创建的物理卷:
root@ubuntu:~# pvdisplay --- Physical volume --- PV Name /dev/sda3 VG Name ubuntu-vg PV Size <96.95 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 24818 Free PE 0 Allocated PE 24818 PV UUID AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE "/dev/sdb1" is a new physical volume of "<500.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size <500.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID btJKcM-RoX6-h6Tu-KSmI-7Vr5-ec3u-LstLU5
给一个 VG Name,把新盘分成一个组里:
root@ubuntu:~# vgextend ubuntu-vg /dev/sdb1 Volume group "ubuntu-vg" successfully extended
root@ubuntu:~# pvdisplay --- Physical volume --- PV Name /dev/sda3 VG Name ubuntu-vg PV Size <96.95 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 24818 Free PE 0 Allocated PE 24818 PV UUID AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE --- Physical volume --- PV Name /dev/sdb1 VG Name ubuntu-vg PV Size <500.00 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 127999 Free PE 127999 Allocated PE 0 PV UUID btJKcM-RoX6-h6Tu-KSmI-7Vr5-ec3u-LstLU5
三、执行挂载操作
方案一:挂载到 ubuntu-lv 上
适用场景:原数据都保存在系统分区中,现在系统分区空间不够使用了,需要扩容。
1、扩展逻辑卷(下面往 100G 现有硬盘上挂载 300G 硬盘的输出信息)
root@ubuntu:~# lvextend -L +300G /dev/ubuntu-vg/ubuntu-lv Insufficient free space:25688 extents needed, but only 25599 available
我们虽然加了500G硬盘,但是实际上要小一点,因此改小一点:
(其实上面也提示了:PV Size <300.00 GiB / not usable 3.00 MiB)
root@ubuntu:~# lvextend -L +299.99G /dev/ubuntu-vg/ubuntu-lv Rounding size to boundary between physical extents: 299.99 GiB. Size of logical volume ubuntu-vg/ubuntu-lv changed from <96.95 GiB (24818 extents) to <396.94 GiB (101616 extents). Logical volume ubuntu-vg/ubuntu-lv successfully resized.
2、扩容
root@ubuntu:~# resize2fs /dev/ubuntu-vg/ubuntu-lv resize2fs 1.47.0 (5-Feb-2023) Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required old_desc_blocks = 13, new_desc_blocks = 50 The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 104054784 (4k) blocks long.
3、查看扩容后的情况
root@ubuntu:~# df -h Filesystem Size Used Avail Use% Mounted on tmpfs 197M 1.1M 196M 1% /run efivarfs 256K 64K 188K 26% /sys/firmware/efi/efivars /dev/mapper/ubuntu--vg-ubuntu--lv 391G 4.5G 370G 2% / tmpfs 984M 0 984M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sda2 2.0G 96M 1.7G 6% /boot /dev/sda1 1.1G 6.2M 1.1G 1% /boot/efi tmpfs 197M 12K 197M 1% /run/user/0
方案二:挂载到新建的逻辑卷上
适用场景:系统盘和数据盘分离的场景
1、创建新的逻辑卷(创建一个mysql数据库存储盘)
root@ubuntu:~# lvcreate -L 499.99G -n mysql-data ubuntu-vg Rounding up size to full physical extent 499.99 GiB Logical volume "mysql-data" created.
前面提到了,实际大小小于500G
2、创建挂载目录
mkdir -p /mnt/mysql-data
3、格式化文件系统
root@ubuntu:~# mkfs.ext4 /dev/ubuntu-vg/mysql-data mke2fs 1.47.0 (5-Feb-2023) Creating filesystem with 131069952 4k blocks and 32768000 inodes Filesystem UUID: 96b2153d-ee57-4ae3-a4e4-0931f1e30e9b Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000 Allocating group tables: done Writing inode tables: done Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done
格式化前:
root@ubuntu:~# lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT32 0CC8-757B 1G 1% /boot/efi ├─sda2 ext4 1.0 602d4169-741c-47c2-9d63-686cc0a09f0d 1.7G 5% /boot └─sda3 LVM2_member LVM2 001 AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE └─ubuntu--vg-ubuntu--lv ext4 1.0 8b41073e-f1ba-412e-9eb2-8127713f827d 84.4G 7% / sdb └─sdb1 LVM2_member LVM2 001 btJKcM-RoX6-h6Tu-KSmI-7Vr5-ec3u-LstLU5 └─ubuntu--vg-mysql--data sr0
格式化后:
root@ubuntu:~# lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT32 0CC8-757B 1G 1% /boot/efi ├─sda2 ext4 1.0 602d4169-741c-47c2-9d63-686cc0a09f0d 1.7G 5% /boot └─sda3 LVM2_member LVM2 001 AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE └─ubuntu--vg-ubuntu--lv ext4 1.0 8b41073e-f1ba-412e-9eb2-8127713f827d 84.4G 7% / sdb └─sdb1 LVM2_member LVM2 001 btJKcM-RoX6-h6Tu-KSmI-7Vr5-ec3u-LstLU5 └─ubuntu--vg-mysql--data ext4 1.0 96b2153d-ee57-4ae3-a4e4-0931f1e30e9b sr0
4、将新的逻辑卷挂载到目录下
mount /dev/ubuntu-vg/mysql-data /mnt/mysql-data
5、配置开机自动挂载
vim /etc/fstab # 开机自动挂载数据目录 /dev/ubuntu-vg/mysql-data /mnt/mysql-data ext4 defaults 0 2
6、查看挂载结果
root@ubuntu:~# lvdisplay --- Logical volume --- LV Path /dev/ubuntu-vg/ubuntu-lv LV Name ubuntu-lv VG Name ubuntu-vg LV UUID y19FTz-xW8K-A5Mn-g0jD-HVQY-06mh-RRb2RQ LV Write Access read/write LV Creation host, time ubuntu-server, 2024-10-26 03:23:59 +0000 LV Status available # open 1 LV Size <96.95 GiB Current LE 24818 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:0 --- Logical volume --- LV Path /dev/ubuntu-vg/mysql-data LV Name mysql-data VG Name ubuntu-vg LV UUID mgioDS-VQDX-wutT-hBgL-gbQB-dCFH-n2iGmD LV Write Access read/write LV Creation host, time ubuntu, 2024-10-28 07:54:24 +0000 LV Status available # open 1 LV Size 499.99 GiB Current LE 127998 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 252:1
root@ubuntu:~# lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sda ├─sda1 vfat FAT32 0CC8-757B 1G 1% /boot/efi ├─sda2 ext4 1.0 602d4169-741c-47c2-9d63-686cc0a09f0d 1.7G 5% /boot └─sda3 LVM2_member LVM2 001 AfCID6-QHNz-OEcg-KXdj-bVeS-KCMK-DQ2fwE └─ubuntu--vg-ubuntu--lv ext4 1.0 8b41073e-f1ba-412e-9eb2-8127713f827d 84.4G 7% / sdb └─sdb1 LVM2_member LVM2 001 btJKcM-RoX6-h6Tu-KSmI-7Vr5-ec3u-LstLU5 └─ubuntu--vg-mysql--data ext4 1.0 96b2153d-ee57-4ae3-a4e4-0931f1e30e9b 466.1G 0% /mnt/mysql-data sr0
root@ubuntu:~# df -h Filesystem Size Used Avail Use% Mounted on tmpfs 1.6G 1.2M 1.6G 1% /run efivarfs 256K 64K 188K 26% /sys/firmware/efi/efivars /dev/mapper/ubuntu--vg-ubuntu--lv 96G 6.4G 85G 8% / tmpfs 7.9G 0 7.9G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock /dev/sda2 2.0G 96M 1.7G 6% /boot /dev/sda1 1.1G 6.2M 1.1G 1% /boot/efi tmpfs 1.6G 12K 1.6G 1% /run/user/0 /dev/mapper/ubuntu--vg-mysql--data 492G 28K 467G 1% /mnt/mysql-data
结束!
欢迎关注我的公众号:云栖语,不一样的研发视界。
云栖语微信公众号:change-1978