Ubuntu lvm 新建分区

注意以下操作只适配lvm哦。

  1. 查看磁盘
root@macmini-ubuntu:/data# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0  49.8M  1 loop /snap/snapd/18357
loop1                       7:1    0  63.3M  1 loop /snap/core20/1822
loop2                       7:2    0 111.9M  1 loop /snap/lxd/24322
loop3                       7:3    0  53.3M  1 loop /snap/snapd/19457
loop4                       7:4    0  63.4M  1 loop /snap/core20/1974
loop5                       7:5    0     4K  1 loop /snap/bare/5
loop6                       7:6    0  91.7M  1 loop /snap/gtk-common-themes/1535
loop7                       7:7    0 237.2M  1 loop /snap/firefox/2952
loop8                       7:8    0 349.7M  1 loop /snap/gnome-3-38-2004/143
sda                         8:0    0 931.5G  0 disk
├─sda1                      8:1    0     1G  0 part /boot/efi
├─sda2                      8:2    0     2G  0 part /boot
└─sda3                      8:3    0 369.5G  0 part
  ├─ubuntu--vg-ubuntu--lv 253:0    0   100G  0 lvm  /var/snap/firefox/common/host-hunspell
  │                                                 /

由上查看到 sda3 有200G 空闲空间未分配。接下来我准备将200G空闲空间分配 根目录下的 data

  1. 先查看一下vg组
root@macmini-ubuntu:/data# vgs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  VG        #PV #LV #SN Attr   VSize    VFree
  ubuntu-vg   1   2   0 wz--n- <928.46g <828.46g

由上可以看到,此vg组总容量为900多G,目前还能分配828G。
⚠️注意:这里是因为安装的时候Ubuntu空间分配我是保持的默认格式分配的,如有不一样很正常。如果未查看到有相关vg组的话需要创建一个。步骤如下:

# 先查看是否已存在pv(物理卷)
pvs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <928.46g <828.46g
# 如果已经存在,那么则无需创建pv,如果没有则需先创建pv
pvcreate /dev/sda
# 创建完pv 后,则需要创建vg(卷组),并绑定到pv上么,自行替换对应的名称
vgcreare <vg_name>  <pv>
# 创建完后使用下面的命令查看vg信息
vgs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  VG        #PV #LV #SN Attr   VSize    VFree
  ubuntu-vg   1   2   0 wz--n- <928.46g <828.46g
  1. 创建lv(逻辑卷)
# 创建lv,并将之绑定的VG。自己指定大小名字,绑定到港创建的VG上
lvcreate -L <Size-of-LV> -n <LV-Name>   <VG-Name>
# 例如
lvcreate -L 200G -n data-lv ubuntu-vg
# 查看创建结果
lvs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data-lv   ubuntu-vg -wi-ao---- 200.00g
  ubuntu-lv ubuntu-vg -wi-ao---- 100.00g
  1. 格式化分区
# 设备名格式 /dev/vg卷组名/lv名  按照格式自行替换
mkfs.ext4 /dev/ubuntu-vg/data-lv

也可以不执行跳过,只是看到有些教程有提到。个人操作未执行,无问题。

# 执行下面的命令,用 xfs 文件系统格式化 LVM 分区:
sudo mkfs.xfs /dev/ubuntu-vg/data-lv
  1. 挂载
# 创建目录
mkdir -p /data
# 挂载
mount /dev/ubuntu-vg/data-lv /data/
# 验证
pvs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  PV         VG        Fmt  Attr PSize    PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <928.46g <628.46g
vgs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  VG        #PV #LV #SN Attr   VSize    VFree
  ubuntu-vg   1   2   0 wz--n- <928.46g <628.46g
lvs
  WARNING: Device /dev/sda3 has size of 774852049 sectors which is smaller than corresponding PV size of 1947123712 sectors. Was device resized?
  WARNING: One or more devices used as PVs in VG ubuntu-vg have changed sizes.
  LV        VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data-lv   ubuntu-vg -wi-ao---- 200.00g
  ubuntu-lv ubuntu-vg -wi-ao---- 100.00g
df -Th
Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  783M  1.7M  781M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    98G   14G   80G  15% /
tmpfs                             tmpfs  3.9G     0  3.9G   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4   2.0G  253M  1.6G  14% /boot
/dev/sda1                         vfat   1.1G  6.1M  1.1G   1% /boot/efi
tmpfs                             tmpfs  3.9G     0  3.9G   0% /run/qemu
tmpfs                             tmpfs  783M  4.0K  783M   1% /run/user/0
/dev/mapper/ubuntu--vg-data--lv   ext4   196G   28K  186G   1% /data
lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0  49.8M  1 loop /snap/snapd/18357
loop1                       7:1    0  63.3M  1 loop /snap/core20/1822
loop2                       7:2    0 111.9M  1 loop /snap/lxd/24322
loop3                       7:3    0  53.3M  1 loop /snap/snapd/19457
loop4                       7:4    0  63.4M  1 loop /snap/core20/1974
loop5                       7:5    0     4K  1 loop /snap/bare/5
loop6                       7:6    0  91.7M  1 loop /snap/gtk-common-themes/1535
loop7                       7:7    0 237.2M  1 loop /snap/firefox/2952
loop8                       7:8    0 349.7M  1 loop /snap/gnome-3-38-2004/143
sda                         8:0    0 931.5G  0 disk
├─sda1                      8:1    0     1G  0 part /boot/efi
├─sda2                      8:2    0     2G  0 part /boot
└─sda3                      8:3    0 369.5G  0 part
  ├─ubuntu--vg-ubuntu--lv 253:0    0   100G  0 lvm  /var/snap/firefox/common/host-hunspell
  │                                                 /
  └─ubuntu--vg-data--lv   253:1    0   200G  0 lvm  /data
# 随便往里面存点什么东西,验证通过后我们需要永久挂载
echo '/dev/ubuntu-vg/data-lv  /data/  ext4  defaults 0 0' | sudo  tee -a /etc/fstab

参考链接

posted @ 2023-08-08 17:20  自在拉基  阅读(315)  评论(0编辑  收藏  举报