鲲鹏宴会酒店前厅经理张玲玲 發表於 2023-11-9 17:00:00

ubuntu的ubuntu--vg-ubuntu--lv磁盘扩容

<p>在我们安装ubuntu时,如果选择的是自动分区,就会按照逻辑卷的形式来分区,并且只分配100G其余的并不会被分配</p>
<p>这对我们大多数情况来说都是不合理的,所以,如何扩充呢</p>
<p>下面以一个小的案例来说明如何扩充</p>
<h1 id="问题">问题</h1>
<p>磁盘空间已满</p>
<pre><code class="language-bash">Usage of /: 99.8% of 9.75GB
</code></pre>
<p>df、fdisk查看磁盘占用情况</p>
<pre><code class="language-bash">df -h
Filesystem                         SizeUsed Avail Use% Mounted on
tmpfs                              197M1.4M196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv9.8G9.3G   0 100% /
tmpfs                              983M   0983M   0% /dev/shm
tmpfs                              5.0M   05.0M   0% /run/lock
/dev/sda2                        1.7G253M1.4G16% /boot
tmpfs                              197M4.0K197M   1% /run/user/1000
</code></pre>
<pre><code class="language-bash">fdisk -l

Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 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

Device       Start      EndSectorsSize Type
/dev/sda1   2048   4095   2048    1M BIOS boot
/dev/sda2   4096367411136700161.8G Linux filesystem
/dev/sda33674112 33552383 29878272 14.2G Linux filesystem
</code></pre>
<p>查看磁盘挂载情况</p>
<pre><code class="language-bash">lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0    25G0 disk
├─sda1                      8:1    0   1M0 part
├─sda2                      8:2    0   1.8G0 part /boot
└─sda3                      8:3    014.2G0 part
└─ubuntu--vg-ubuntu--lv 253:0    09.8G0 lvm/
</code></pre>
<p>可以看到sda3有15G,但是只分配了10G。</p>
<p>命令vgdisplay可以查看lvm卷组的信息;</p>
<pre><code class="language-bash">vgdisplay
</code></pre>
<p>如果看到 Free PE / Size &gt; 0,表示还有扩容空间。</p>
<h1 id="解决">解决</h1>
<p>这里分两种情况进行处理:</p>
<ul>
<li>
<p>LV(Logical Volume) 没有用尽 VG(Volume Group)。</p>
</li>
<li>
<p>VG(Volume Group) 没有用尽 PV(Physical Volume),或者 PV(Physical Volume) 没有关联物理分区。</p>
</li>
</ul>
<h2 id="lv-使用全部-vg">LV 使用全部 VG</h2>
<p>使用lvresize扩容剩下所有空间。</p>
<pre><code class="language-bash">#lvextend -L 10G /dev/mapper/ubuntu--vg-ubuntu--lv      //增大或减小至19G
#lvextend -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv   //增加10G
#lvreduce -L -10G /dev/mapper/ubuntu--vg-ubuntu--lv   //减小10G
#lvresize -l+100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv   //按百分比扩

lvresize -l+100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
</code></pre>
<p>执行resize2fs使扩容生效 <strong>【注意:不执行这个命令以上修改是不会生效的!】</strong></p>
<pre><code class="language-bash">resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 3734528 (4k) blocks long.
</code></pre>
<p>查看结果</p>
<pre><code class="language-bash">vgdisplay
--- Volume group ---
VG Name               ubuntu-vg
System ID
Format                lvm2
VG Size               &lt;14.25 GiB
PE Size               4.00 MiB
Total PE            3647
Alloc PE / Size       3647 / &lt;14.25 GiB
FreePE / Size       0 / 0
</code></pre>
<h2 id="vg-关联物理分区进行扩容">VG 关联物理分区进行扩容</h2>
<p>补充:本系统使用的是虚拟机,所以需要在虚拟机设置中先扩容“物理”硬盘空间。</p>
<p>接下来采用的方法是:不修改原有分区,在新的空间上新建分区,并添加到LVM的LV中。</p>
<p>创建分区 sda4,并设置为 lvm 类型:</p>
<pre><code class="language-bash"># 修改前分区状态
$ lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0    25G0 disk
├─sda1                      8:1    0   1M0 part
├─sda2                      8:2    0   1.8G0 part /boot
└─sda3                      8:3    014.2G0 part
└─ubuntu--vg-ubuntu--lv 253:0    014.2G0 lvm/

# 开始分区
$ sudo fdisk /dev/sda

Command (m for help): m#查看帮助

Help:

GPT
   M   enter protective/hybrid MBR

Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

Misc
   m   print this menu
   x   extra functionality (experts only)

Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

Save &amp; Exit
   w   write table to disk and exit
   q   quit without saving changes

Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): p#打印分区

Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 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

Device       Start      EndSectorsSize Type
/dev/sda1   2048   4095   2048    1M BIOS boot
/dev/sda2   4096367411136700161.8G Linux filesystem
/dev/sda33674112 33552383 29878272 14.2G Linux filesystem

Command (m for help): n# 新建分区
Partition number (4-128, default 4):   # 使用默认的序号4
First sector (33552384-52428766, default 33552384):#使用默认的开始
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33552384-52428766, default 52428766):#使用默认的结束也就也就意味着使用剩下的全部

Created a new partition 4 of type 'Linux filesystem' and of size 9 GiB.

Command (m for help): p   # 打印分区
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 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

Device      Start      EndSectorsSize Type
/dev/sda1      2048   4095   2048    1M BIOS boot
/dev/sda2      4096367411136700161.8G Linux filesystem
/dev/sda3   3674112 33552383 29878272 14.2G Linux filesystem
/dev/sda433552384 52428766 18876383    9G Linux filesystem

Command (m for help): t#改变分区类型
Partition number (1-4, default 4):#默认选择刚刚的4
Partition type or alias (type L to list all): lvm# 改编成lvm

Changed type of partition 'Linux filesystem' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 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

Device      Start      EndSectorsSize Type
/dev/sda1      2048   4095   2048    1M BIOS boot
/dev/sda2      4096367411136700161.8G Linux filesystem
/dev/sda3   3674112 33552383 29878272 14.2G Linux filesystem
/dev/sda433552384 52428766 18876383    9G Linux LVM

Command (m for help): w# 写入保存并退出
The partition table has been altered.
Syncing disks.
</code></pre>
<p>建物理分区并关联到LVM</p>
<pre><code class="language-bash">$ sudo lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
..
sda                         8:0    0    25G0 disk
├─sda1                      8:1    0   1M0 part
├─sda2                      8:2    0   1.8G0 part /boot
├─sda3                      8:3    014.2G0 part
│ └─ubuntu--vg-ubuntu--lv 253:0    014.2G0 lvm/
└─sda4                      8:4    0   9G0 part

# 创建物理卷PV
$ sudo pvcreate /dev/sda4
Physical volume "/dev/sda4" successfully created.

# 查看物理卷PV
$ sudo pvdisplay
--- Physical volume ---
PV Name               /dev/sda3
VG Name               ubuntu-vg
PV Size               &lt;14.25 GiB / not usable 0
Allocatable         yes (but full)
PE Size               4.00 MiB
Total PE            3647
Free PE               0
Allocated PE          3647

"/dev/sda4" is a new physical volume of "9.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sda4
VG Name
PV Size               9.00 GiB
Allocatable         NO
PE Size               0
Total PE            0
Free PE               0
Allocated PE          0

# 查看卷组VG
$ sudo vgdisplay
--- Volume group ---
VG Name               ubuntu-vg
System ID
Format                lvm2
VG Size               &lt;14.25 GiB
PE Size               4.00 MiB
Total PE            3647
Alloc PE / Size       3647 / &lt;14.25 GiB
FreePE / Size       0 / 0

# 扩容卷组VG
$ sudo vgextend ubuntu-vg /dev/sda4
Volume group "ubuntu-vg" successfully extended

# 查看卷组VG
$ sudo vgdisplay
--- Volume group ---
VG Name               ubuntu-vg
System ID
Format                lvm2
VG Size               23.24 GiB
PE Size               4.00 MiB
Total PE            5950
Alloc PE / Size       3647 / &lt;14.25 GiB
FreePE / Size       2303 / &lt;9.00 GiB

# 扩容逻辑卷LV
$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from &lt;14.25 GiB (3647 extents) to 23.24 GiB (5950 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.

# 查看卷组VG
$ sudo vgdisplay
--- Volume group ---
VG Name               ubuntu-vg
System ID
Format                lvm2
VG Size               23.24 GiB
PE Size               4.00 MiB
Total PE            5950
Alloc PE / Size       5950 / 23.24 GiB
FreePE / Size       0 / 0

# 执行修改
$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 6092800 (4k) blocks long.

# 查看结果
$ df -h
Filesystem                         SizeUsed Avail Use% Mounted on
tmpfs                              197M1.6M195M   1% /run
# 可以看到/已经变成了扩容后的容量
/dev/mapper/ubuntu--vg-ubuntu--lv   23G9.2G   13G43% /
tmpfs                              983M   0983M   0% /dev/shm
tmpfs                              5.0M   05.0M   0% /run/lock
/dev/sda2                        1.7G253M1.4G16% /boot
tmpfs                              197M4.0K197M   1% /run/user/1000

$ sudo lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                         8:0    0    25G0 disk
├─sda1                      8:1    0   1M0 part
├─sda2                      8:2    0   1.8G0 part /boot
├─sda3                      8:3    014.2G0 part
│ └─ubuntu--vg-ubuntu--lv 253:0    023.2G0 lvm/
└─sda4                      8:4    0   9G0 part
└─ubuntu--vg-ubuntu--lv 253:0    023.2G0 lvm/
</code></pre>
<p>完成</p>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:厚礼蝎,转载请注明原文链接:https://www.cnblogs.com/guangdelw/p/17822292.html</p><br><br>
来源:https://www.cnblogs.com/guangdelw/p/17822292.html
頁: [1]
查看完整版本: ubuntu的ubuntu--vg-ubuntu--lv磁盘扩容