一、问题描述
因为我初始分配的虚拟机磁盘巨细不敷,所以现在必要重新添加一块虚拟机磁盘。
注:此方法添加磁盘是有一些错误,导致启动盘变成了新添加的磁盘,博主现在正在找方法。
这个必要将原先的vim /etc/fstab 加的删除 不发起采用本教程
可参考:https://blog.csdn.net/Edwinwzy/article/details/130614205
二、办理办法
添加磁盘
- 点击虚拟机配置
2、在”虚拟机设置“中 点击 “添加”
3、在”添加硬件导向“中选择”磁盘“,然后点击”下一步“
4、选择 “SCSI”
5、选择”创建虚拟磁盘“
6、给磁盘设定空间巨细(根据你自己必要提添加的磁盘巨细,这里是40GB)
7、这一步直接点击 ”完成“ 就可以了
Linux 下磁盘管理–逻辑卷–LV
1、进入虚拟机检察,在root用户下输入以下命令
fdisk -l
新增的磁盘/dev/sdb已添加
2、创建一个40G的分区
- root@wuhaonan-virtual-machine:~# fdisk /dev/sdb
- 欢迎使用 fdisk (util-linux 2.37.2)。
- 更改将停留在内存中,直到您决定将更改写入磁盘。
- 使用写入命令前请三思。
- 命令(输入 m 获取帮助): n
- 分区类型
- p 主分区 (0 primary, 0 extended, 4 free)
- e 扩展分区 (逻辑分区容器)
- 选择 (默认 p): p
- 分区号 (1-4, 默认 1): 1
- 第一个扇区 (2048-83886079, 默认 2048): 2048
- Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-83886079, 默认 83886079): 83886079
- 创建了一个新分区 1,类型为“Linux”,大小为 40 GiB。
- 命令(输入 m 获取帮助): t
- 已选择分区 1
- Hex code or alias (type L to list all): 8e
- 已将分区“Linux”的类型更改为“Linux LVM”。
- 命令(输入 m 获取帮助): w
- 分区表已调整。
- 将调用 ioctl() 来重新读分区表。
复制代码 命令先容:
使用 fdisk /dev/sdb 创建一个新分区:
输入 n 创建新分区。
选择 p(主分区)。
设置分区编号为 1。
出发点扇区输入默认值 2048。
末尾扇区输入 83886079,这是磁盘的最大范围。
输入 t 修改分区范例为 8e(Linux LVM 范例)。
输入 w 保存并退出。
3、创建物理卷(PV)
将sdbq选择为自己的分区
1、pvcreate /dev/sdb1
2、pvdisplay
3、pvs
- root@wuhaonan-virtual-machine:~# pvcreate /dev/sdb1
- Physical volume "/dev/sdb1" successfully created.
- root@wuhaonan-virtual-machine:~# pvdisplay
- "/dev/sdb1" is a new physical volume of "<40.00 GiB"
- --- NEW Physical volume ---
- PV Name /dev/sdb1
- VG Name
- PV Size <40.00 GiB
- Allocatable NO
- PE Size 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID hI5Ghp-YO3a-jwD5-SKd4-jKGj-fxnY-6fsQHe
-
- root@wuhaonan-virtual-machine:~# pvs
- PV VG Fmt Attr PSize PFree
- /dev/sdb1 lvm2 --- <40.00g <40.00g
复制代码 4、创建逻辑卷(LV)
创建逻辑卷 lvcreate -n 逻辑卷名 -L 卷组巨细 卷组名
1、lvcreate -n lvlv -L 40G vgvg (这里的40才是我们最后磁盘的巨细)非常重要
2、lvdisplay
3、lvs
但是这时会重新一个问题,现在 /dev/sdb2 的分区只有一个扇区,巨细仅为 512 字节,因此无法正常分配或使用。这里是发起怎样重新分配整个磁盘 /dev/sdb 的 40 GiB 空间
- root@wuhaonan-virtual-machine:~# lvcreate -n lvlv -L 40G vgvg
- Volume group "vgvg" has insufficient free space (10239 extents): 10240 required.
- root@wuhaonan-virtual-machine:~# sudo vgdisplay vgvg
- --- Volume group ---
- VG Name vgvg
- 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 <40.00 GiB
- PE Size 4.00 MiB
- Total PE 10239
- Alloc PE / Size 0 / 0
- Free PE / Size 10239 / <40.00 GiB
- VG UUID Ra3H62-4zw1-1BcZ-uRh0-Rluf-gjFm-ScHWuW
复制代码 办理办法:重新创建一个小一点的39.9G
- root@wuhaonan-virtual-machine:~# sudo lvcreate -n lvlv -L 39.9G vgvg
- Rounding up size to full physical extent 39.90 GiB
- Logical volume "lvlv" created.
- root@wuhaonan-virtual-machine:~# lvdisplay
- --- Logical volume ---
- LV Path /dev/vgvg/lvlv
- LV Name lvlv
- VG Name vgvg
- LV UUID alE4Fm-hf3x-9Xng-MvnW-GIXw-vUxs-onbzab
- LV Write Access read/write
- LV Creation host, time wuhaonan-virtual-machine, 2025-03-30 22:37:28 +0800
- LV Status available
- # open 0
- LV Size 39.90 GiB
- Current LE 10215
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 252:0
-
- root@wuhaonan-virtual-machine:~# lvs
- LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
- lvlv vgvg -wi-a----- 39.90g
复制代码 5、格式化并挂载
1、mkfs.ext4 /dev/vgvg/lvlv
2、mkdir /lvmdata
3、mount /dev/vgvg/lvlv /lvmdata
- root@wuhaonan-virtual-machine:~# mkfs.ext4 /dev/vgvg/lvlv
- mke2fs 1.46.5 (30-Dec-2021)
- 创建含有 10460160 个块(每块 4k)和 2616320 个 inode 的文件系统
- 文件系统 UUID:b9ac44dd-7200-4d61-9dff-af49026d35ff
- 超级块的备份存储于下列块:
- 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
- 4096000, 7962624
- 正在分配组表: 完成
- 正在写入 inode表: 完成
- 创建日志(65536 个块): 完成
- 写入超级块和文件系统账户统计信息: 已完成
- root@wuhaonan-virtual-machine:~# mkdir /lvmdata
- root@wuhaonan-virtual-machine:~# mount /dev/vgvg/lvlv /lvmdata
复制代码 6、创建永久挂载点
1、使用 blkid 命令检察UUID
2、将设备加到 /etc/fstab 文件中
vim /etc/fstab ###设置fstab配置文件
在文件中输入的内容的格式是:
硬件文件或uuid号 >挂载点>文件范例>设备自界说选项>0>0
这是我的文件里新加的语句
- root@wuhaonan-virtual-machine:~# blkid
- /dev/sda3: UUID="9cacb9c5-d445-42d2-8404-71cd5214b63f" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="7490e8c3-44b3-45da-bce8-1846965a2d03"
- /dev/loop1: TYPE="squashfs"
- /dev/loop19: TYPE="squashfs"
- /dev/loop17: TYPE="squashfs"
- /dev/loop8: TYPE="squashfs"
- # 这是我添加的
- /dev/sdb1: UUID="hI5Ghp-YO3a-jwD5-SKd4-jKGj-fxnY-6fsQHe" TYPE="LVM2_member" PARTUUID="4e3b4538-01"
- /dev/loop15: TYPE="squashfs"
- /dev/loop6: TYPE="squashfs"
- /dev/loop13: TYPE="squashfs"
- /dev/loop4: TYPE="squashfs"
- /dev/loop11: TYPE="squashfs"
- /dev/sr0: BLOCK_SIZE="2048" UUID="2024-02-20-19-39-27-00" LABEL="Ubuntu 22.04.4 LTS amd64" TYPE="iso9660" PTTYPE="PMBR"
- /dev/loop2: TYPE="squashfs"
- /dev/loop0: TYPE="squashfs"
- /dev/mapper/vgvg-lvlv: UUID="b9ac44dd-7200-4d61-9dff-af49026d35ff" BLOCK_SIZE="4096" TYPE="ext4"
- /dev/loop18: TYPE="squashfs"
- /dev/loop9: TYPE="squashfs"
- /dev/loop16: TYPE="squashfs"
- /dev/loop7: TYPE="squashfs"
- /dev/sda2: UUID="6140-BDD7" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="cf9e34af-ab70-435f-b50b-26519d94fdae"
- /dev/sda1: PARTUUID="5d515608-6a1f-49f1-829d-f01b08a81219"
- /dev/loop14: TYPE="squashfs"
- /dev/loop5: TYPE="squashfs"
- /dev/loop12: TYPE="squashfs"
- /dev/loop3: TYPE="squashfs"
- /dev/loop20: TYPE="squashfs"
- /dev/loop10: TYPE="squashfs"
- root@wuhaonan-virtual-machine:~# vim /etc/fstab
- # /etc/fstab: static file system information.
- #
- # Use 'blkid' to print the universally unique identifier for a
- # device; this may be used with UUID= as a more robust way to name devices
- # that works even if disks are added and removed. See fstab(5).
- #
- # <file system> <mount point> <type> <options> <dump> <pass>
- # / was on /dev/sda3 during installation
- UUID=9cacb9c5-d445-42d2-8404-71cd5214b63f / ext4 errors=remount-ro 0 1
- # /boot/efi was on /dev/sda2 during installation
- UUID=6140-BDD7 /boot/efi vfat umask=0077 0 1
- /swapfile none swap sw 0 0
- # add disk
- UUID=hI5Ghp-YO3a-jwD5-SKd4-jKGj-fxnY-6fsQHe /lvmdata ext4 defaults 0 0
- # 用于根据 /etc/fstab 配置文件中的内容自动挂载所有定义的文件系统。
- root@wuhaonan-virtual-machine:~# mount -a
- mount: /lvmdata: /dev/sdb1 already mounted or mount point busy.
- root@wuhaonan-virtual-machine:~# mount | grep /dev/sdb1
- root@wuhaonan-virtual-machine:~# mount | grep /dev/sdb1
- root@wuhaonan-virtual-machine:~# lsof | grep /lvmdata
- lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
- Output information may be incomplete.
- lsof: WARNING: can't stat() fuse.portal file system /run/user/1000/doc
- Output information may be incomplete.
- # 确认当前挂载状态
- # 检查 /dev/sdb1 或 /lvmdata 是否已经挂载
- root@wuhaonan-virtual-machine:~# mount | grep /lvmdata
- mount | grep /dev/sdb1
- /dev/mapper/vgvg-lvlv on /lvmdata type ext4 (rw,relatime)
- # 验证挂载点状态
- # 使用以下命令查看磁盘使用情况
- root@wuhaonan-virtual-machine:~# df -h
- 文件系统 大小 已用 可用 已用% 挂载点
- tmpfs 387M 3.7M 384M 1% /run
- /dev/sda3 39G 35G 2.6G 93% /
- tmpfs 1.9G 0 1.9G 0% /dev/shm
- tmpfs 5.0M 4.0K 5.0M 1% /run/lock
- /dev/sda2 512M 6.1M 506M 2% /boot/efi
- tmpfs 387M 140K 387M 1% /run/user/1000
- /dev/sr0 4.7G 4.7G 0 100% /media/wuhaonan/Ubuntu 22.04.4 LTS amd64
- /dev/mapper/vgvg-lvlv 39G 24K 37G 1% /lvmdata
复制代码 挂载已经完成。
参考链接:https://blog.csdn.net/ITpfzl/article/details/89036255
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |