使用 LVM 扩展卷空间
LVM
![](/data/attachment/album/202011/27/193719d3df4p4z748f33f3.jpg)


LVM 是一个逻辑卷管理工具，包括分配磁盘、条带化、镜像和调整逻辑卷的大小。它在 Fedora 安装中被普遍使用（在 BTRFS 作为默认文件系统之前是 LVM + Ext4）。但是你是否曾经遇到过启动系统时，GNOME 提示 home 卷几乎没有空间了！幸运的是，很有可能有一些空间在另一个卷中，而未被使用，可以用于重新分配。下面就来看看如何用 LVM 回收硬盘空间。


在卷之间轻松重新分配空间的关键是[逻辑卷管理器（LVM）](http://sourceware.org/lvm2/)。Fedora 32 及以前的系统默认使用 LVM 来划分磁盘空间。这种技术类似于标准的硬盘分区，但 LVM 更加灵活。LVM 不仅可以实现灵活的卷大小管理，还可以实现一些高级功能，比如读写快照、在多个硬盘上进行数据条带化或镜像、将高速硬盘作为慢速硬盘的缓存等等。所有这些高级选项可能会让人有点不知所措，但调整卷的大小很简单的。


### LVM 基础


**卷组**（VG）作为 LVM 系统中的主要容器。默认情况下，Fedora 只定义了一个卷组，但你可以根据需要定义多个卷组。实际的硬盘和硬盘分区被添加到卷组中作为**物理卷**（PV）。物理卷会将可用的空间添加到卷组中。一个典型的 Fedora 安装有一个格式化的启动分区，其余的硬盘是一个配置为 LVM 物理卷的分区。


从这个可用空间池中，卷组分配了一个或多个**逻辑卷**（LV）。这些卷类似于硬盘分区，但没有磁盘上连续空间的限制。LVM 的逻辑卷甚至可以跨越多个设备！就像硬盘分区一样，逻辑卷有一个定义的大小，可以包含任何文件系统，然后可以挂载到特定的目录。


### 需要什么


在 gnome-disks 应用中确认系统使用 LVM ，并确保其他卷中有可用的空间。如果没有可以从另一个卷中回收的空间，这个指南就没有用。还需要一个 [Fedora 临场 CD/USB](https://getfedora.org/en/workstation/download/)。任何需要收缩的文件系统都必须卸载。从<ruby> 临场 <rt>  Live </rt></ruby>镜像运行，可以让硬盘上的所有卷保持未挂载状态，甚至包括像 `/` 和 `/home` 这样的重要目录。


![Use gnome-disks to verify free space](/data/attachment/album/202011/27/193855ypsr6jr8622hrfdd.png)


### 一句话警告


按照这个指南，应该不会丢失任何数据，但它确实会使用一些非常底层和强大的命令。一个错误可能会破坏硬盘上的所有数据。所以要先备份磁盘上的所有数据！


### 调整 LVM 卷的大小


开始时，启动 Fedora 临场镜像并在对话框中选择 “Try Fedora”。接下来，使用 “Run Command” 启动 “blivet-gui” 应用（按 `Alt-F2`，输入 `blivet-gui`，然后按回车）。选择左侧 “LVM” 下的卷组。逻辑卷在右边。


![Explore logical volumes in blivet-gui](/data/attachment/album/202011/27/193856y0yu3wfb9ffb7ffb.png)


逻辑卷标签由卷组名称和逻辑卷名称组成。在本例中，卷组是 `fedora_localhost-live`，分配了 `home`、`root` 和 `swap` 逻辑卷。要找到完整的卷，选择每一个卷，点击“齿轮”图标，然后选择 “resize”。调整大小对话框中的滑块表示卷的允许大小。左边的最小值是文件系统中已经使用的空间，所以这是最小可能的尺寸大小（不删除数据）。右边的最大值是基于卷组中的最大可用空间。


![Resize dialog in blivet-gui](/data/attachment/album/202011/27/193857oqe5p20jqohehreq.png)


灰色的 “resize” 选项意味着该卷已经满了，而且卷组中没有可用空间。


现在可以更改大小了！查看所有的卷，就像上面的截图那样，找到一个有足够空间的卷。并像上面的截图那样，在所有的卷中找到一个有大量额外空间的卷。向左移动滑块来设置新的大小。腾出足够的空间对整个卷有用，但仍然要为未来的数据增长留出足够的空间。否则，这个卷将是下一个被填满的卷。


点击 “resize”，注意卷列表中出现了一个新项目：“free space”。现在选择这次要调整的卷，并将滑块一直向右移动。按 “resize” 键，并查看新改进的卷的布局。然而，硬盘驱动器上的任何东西现在都还没有改变。点击“勾选”选项将更改提交到磁盘。


![Review changes in blivet-gui](/data/attachment/album/202011/27/193857l4ejyic47s8j8rn7.png)


查看更改的摘要，如果一切看起来都是正确的，点击 “Ok” 继续。等待 “blivet-gui” 完成。现在重新启动回到 Fedora，可以使用之前被充满的卷上的新空间了。


### 为未来计划


要知道任何特定卷在未来需要多少空间是很困难的。与其立即分配所有可用的空闲空间，不如考虑在卷组中留出空闲空间。事实上，Fedora Server 默认在卷组中保留空间。当一个卷处于在线和使用中时，扩展卷是可能的。不需要临场镜像或重启。当一个卷几乎满的时候，可以使用部分可用空间轻松扩展卷并继续工作。遗憾的是，默认的磁盘管理器 gnome-disks 不支持 LVM 卷的大小调整，所以安装 [blivet-gui](https://fedoraproject.org/wiki/Blivet-gui) 作为图形化管理工具。另外，还有一个简单的终端命令来扩展卷：



```
lvresize -r -L +1G /dev/fedora_localhost-live/root

```

### 总结


用 LVM 回收硬盘空间只是 LVM 功能的表面。大多数人，特别是在桌面中，可能不需要更高级的功能。然而，当需要的时候，LVM 就在那里，尽管它的实现可能会变得有点复杂。从 [Fedora 33](https://fedoramagazine.org/whats-new-fedora-33-workstation/) 开始，[BTRFS](https://fedoramagazine.org/btrfs-coming-to-fedora-33/) 是默认的文件系统，没有 LVM。BTRFS 可以更容易管理，同时对于大多数常见的使用来说也足够灵活。查看最近 [Fedora Magazine 关于 BTRFS 的文章](https://fedoramagazine.org/btrfs-snapshots-backup-incremental/)了解更多。




---


via: <https://fedoramagazine.org/reclaim-hard-drive-space-with-lvm/>


作者：[Troy Curtis Jr](https://fedoramagazine.org/author/troycurtisjr/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes. It is commonly used on Fedora installations (prior to BTRFS as default it was LVM+Ext4). But have you ever started up your system to find out that Gnome just said the home volume is almost out of space! Luckily, there is likely some space sitting around in another volume, unused and ready to re-alocate. Here’s how to reclaim hard-drive space with LVM.

The key to easily re-alocate space between volumes is the [Logical Volume Manager (LVM)](http://sourceware.org/lvm2/). Fedora 32 and before use LVM to divide disk space by default. This technology is similar to standard hard-drive partitions, but LVM is a lot more flexible. LVM enables not only flexible volume size management, but also advanced capabilities such as read-write snapshots, striping or mirroring data across multiple drives, using a high-speed drive as a cache for a slower drive, and much more. All of these advanced options can get a bit overwhelming, but resizing a volume is straight-forward.

## LVM basics

The *volume group* serves as the main container in the LVM system. By default Fedora only defines a single volume group, but there can be as many as needed. Actual hard-drive and hard-drive partitions are added to the *volume group* as *physical volumes*. *Physical volumes* add available free space to the *volume group*. A typical Fedora install has one formatted boot partition, and the rest of the drive is a partition configured as an LVM *physical volume*.

Out of this pool of available space, the *volume group* allocates one or more *logical volumes*. These volumes are similar to hard-drive partitions, but without the limitation of contiguous space on the disk. LVM *logical volumes* can even span multiple devices! Just like hard-drive partitions, *logical volumes *have a defined size and can contain any filesystem which can then be mounted to specific directories.

## What’s needed

Confirm the system uses LVM with the *gnome-disks* application, and make sure there is free space available in some other volume. Without space to reclaim from another volume, this guide isn’t useful. A [Fedora live CD/USB](https://getfedora.org/en/workstation/download/) is also needed. Any file system that needs to shrink must be unmounted. Running from a live image allows all the volumes on the hard-disk to remain unmounted, even important directories like */* and */home*.

![Use gnome-disks to verify free space.](https://fedoramagazine.org/wp-content/uploads/2020/11/gnome-disks.png)

## A word of warning

No data should be lost by following this guide, but it does muck around with some very low-level and powerful commands. One mistake could destroy all data on the hard-drive. So backup all the data on the disk first!

## Resizing LVM volumes

To begin, boot the Fedora live image and select *Try Fedora* at the dialog. Next, use the *Run Command* to launch the *blivet-gui* application (accessible by pressing *Alt-F2*, typing *blivet-gui*, then pressing *enter*). Select the volume group on the left under *LVM*. The logical volumes are on the right.

![Explore logical volumes in blivet gui.](https://fedoramagazine.org/wp-content/uploads/2020/11/blivet-overview.png)

The logical volume labels consist of both the volume group name and the logical volume name. In the example, the volume group is “fedora_localhost-live” and there are “home”, “root”, and “swap” logical volumes allocated. To find the full volume, select each one, click on the *gear* icon, and choose *resize*. The slider in the resize dialog indicates the allowable sizes for the volume. The minimum value on the left is the space already in use within the file system, so this is the minimum possible volume size (without deleting data). The maximum value on the right is the greatest size the volume can have based on available free space in the *volume group*.

![Use the resize dialog in blivet-gui to set the new volume sizes.](https://fedoramagazine.org/wp-content/uploads/2020/11/blivet-resize-1024x525.png)

A grayed out *resize* option means the volume is full and there is no free space in the volume group. It’s time to change that! Look through all of the volumes to find one with plenty of extra space, like in the screenshot above. Move the slider to the left to set the new size. Free up enough space to be useful for the full volume, but still leave plenty of space for future data growth. Otherwise, this volume will be the next to fill up.

Click *resize* and note that a new item appears in the volume listing: *free space*. Now select the full volume that started this whole endeavor, and move the slider all the way to the right. Press *resize* and marvel at the new improved volume layout. However, nothing has changed on the hard drive yet. Click on the *check-mark* to commit the changes to disk.

![Review the changes in blivet-gui and then accept to reclaim hard-drive space.](https://fedoramagazine.org/wp-content/uploads/2020/11/blivet-summary-1024x525.png)

Review the summary of the changes, and if everything looks right, click *Ok* to proceed. Wait for *blivet-gui* to finish. Now reboot back into the main Fedora install and enjoy all the new space in the previously full volume.

## Planning for the future

It is challenging to know how much space any particular volume will need in the future. Instead of immediately allocating all available free space, consider leaving it free in the volume group. In fact, Fedora Server reserves space in the volume group by default. Extending a volume is possible while it is online and in use. No live image or reboot needed. When a volume is almost full, easily extend the volume using part of the available free space and keep working. Unfortunately the default disk manager, *gnome-disks*, does not support LVM volume resizing, so install * blivet-gui* for a graphical management tool. Alternately, there is a simple terminal command to extend a volume:

lvresize -r -L +1G /dev/fedora_localhost-live/root

## Wrap-up

Reclaiming hard-drive space with LVM just scratches the surface of LVM capabilities. Most people, especially on the desktop, probably don’t need the more advanced features. However, LVM is there when the need arises, though it can get a bit complex to implement. [BTRFS](https://fedoramagazine.org/btrfs-coming-to-fedora-33/) is the default filesystem, without LVM, starting with [Fedora 33](https://fedoramagazine.org/whats-new-fedora-33-workstation/). BTRFS can be easier to manage while still flexible enough for most common usages. Check out the recent [Fedora Magazine articles on BTRFS](https://fedoramagazine.org/btrfs-snapshots-backup-incremental/) to learn more.

## Esc

Thank You

## Sergey

Thanks.

## Simon

XFS cannot be shrunk

## Jeffrey Goh

ooh – such perfect timing – I’m just now trying to figure out if it’s safe to downsize my root partition so my home partition can grow.

## Troy Curtis Jr

I’ll be very interested to hear how it goes Jeffrey!

## Doug

Thanks for listening to my article suggestion. Having a ~120 GB SSD means that, using the automatic setup, you end up with a very small /home partition. This is useful to tweak it after the installation is completed, although you can also use Anaconda’s custom partitioning functionality to customize the default setup yourself (it’s not so difficult).

## Troy Curtis Jr

It was perfect for me to pick up! I am a big LVM fan and I was happy to have a way to contribute to Fedora. Definitely a great idea (even if BTRFS is the new default 😁).

## Cornel Panceac

How can i shrink the volume group so that i can allocate more storage space to the other, more storage-hungry OS?

## Troy Curtis Jr

It is definitely possible to shrink the volume group, but it is a bit more complex and a lot more manual. My

untestedandunverifiedapproach would go something like:Reduce logical volumes (similar to the article) to add free space to the volume group

Use pvresize to shrink the physical volume slightly less the intended size (and hope that extents aren’t already allocated past the new size, pvresize

shouldfail to change the size in this case).Resize the underlying partition

Use pvresize again, but do not specify a size so that it fills up to exactly the new size of the partition

Add a new partition to the now free space on the hard disk.

There is definitely a decent chance to get various steps wrong here, so definitely make sure there are backups before attempting.

Doing 2 calls to pvresize isn’t strictly necessary, but with only 1 call the size calculation must be exactly right. This is challenging because the units are not always obvious (bytes, blocks, etc). That is why it is best to shrink a little more than necessary with the first pvresize to provide a bit of buffer, and then allow the second pvresize (after shrinking the partition) to calculate the precise values.

Also if extents have been allocated past the end of where the desired pv size would be, then some clever shuffling of extent ranges with a combination of “lvdisplay -m” and “pvmove ..” will be necessary to relocate the extents into an earlier part of the physical volume.

## Guus

I currently have LVM + ext4 and am still running F32, but I want to get to F33 and if possible convert to btrfs. So I wonder, does LVM play any roll at all with btrfs? Or is the roll that LVM plays now, being taken over by btrfs?

## Troy Curtis Jr

It’s definitely possible to have BTRFS on a LVM logical volume, and there are some reasons you may want to go with that approach. However, there are definitely some trade offs. For the most common use cases, especially for desktop, using BTRFS directly on the harddrive probably gives most of features you might want. In particular, subvolumes allow for easy and fast snapshot creation for backups. However, with BTRFS you don’t have some of the features that LVM provides, such as a thinly provisioned volumes for use by different filesystems or direct block device volumes (which are nice for virtual machines). Note that with BTRFS copy on write built in capabilities, and read-write subvolume snapshots, the thinly provisioned use case is completely covered as long as you want the volumes (really subvolumes in BTRFS case) to be BTRFS and not some other filesystem.

So really you have to decide what your needs are. Most likely reformatting the disk and using BTRFS directly makes sense if you want easy subvolume snapshot functionality and to play around with a new modern filesystem.

## Guus

Thank you for your reply. I get that it is my choice, but I don’t have a particular good feeling about the difference yet. My suspicion is that more people would like to know about the differences. Do you think a comparion between the two variants of using btrfs is possible?

Another point is, up to now, I had never heard of thin provisioning. I don’t think that is an issue for me. I do however, sometimes use other filesystems. If I do, does btrfs constrain me to use physical partitions?

I apologize. I have been using Linux for a long time (94 – 98 + 2002 – now) but I am not an advanced user when it comes to storage space. The most advanced I got was repairing a partition I had accidentally removed and that made me pretty anxious, working with fdisk on byte level.

So, if you ever feel the need to use your knowledge to compare btrfs with or without LVM, with different use cases (workstation, server), I would really like that.

## Stephen Brown

There was a recent article titled “Friends Don’t let Friends Use BTRS”, which has somehow, mysteriously disappeared. from this address https://blog.pgaddict.com/posts/friends-dont-let-friends-use-btrfs

While there are some great wins wrt using BTRS, there were two takeaways from this article, that I noted.

Lousy database performance. Which the article attributes to its ‘copy on write’ features. The author did extensive testing across a number of applications and it most definitely is that. I use ‘cow’ a lot. All of my kvm machines are ‘cow2’ disks and it does slow things down. But I want that sparse disk space capability that ‘cow2’ provides so I live with it.

Data recovery and or data corruption is more likely to occur with btrs than ext4, lets say. File systems like XFS are light years ahead of BTRS wrt this.

I use LVM a lot. My biggest complaint with LVM is that it’s not so forgiving wrt shrinking volumes. And I really hate the fact that you can’t change volume names and get away with it, if its a volume that your root file system resides on. So unless you know how to rebuild all of that device ID stuff within grub2 and initramfs, don’t even think about it.

I’m certain that I’m going to waste more disk space with LVM than with BTRS and each year about this time, I’m going to burn incense and wail about it. But in the end, I’ll take all of those file recovery and data recovery capabilities inherent in EXT4 and LVM systems than chance some data corruption with BTRS.

## Patrice

Nice, but funnily anachronistic on Fedora Magazine since BTRFS is the new filesystem on Fedora 33.

I’d like to see some how-to for BTRFS: what’s the benefit, how to manage it and how to make backups.

I’m on Fedora 33 but still using the same LVM+Ext4 filesystem until I get more information on BTRFS, specifically how to manage it for simple day-to-day use and how to perform backups.

## Troy Curtis Jr

It did seem a bit late to the party to write the LVM article right after Fedora 33 came out with a default filesystem of BTRFS. However, I realized that is only for the desktop variants, LVM+ext4 is still the default on the server. Also, many are probably in the same boat as you (and me) if you just upgrade from previous releases. I spent some time considering switching on my desktop install to BTRFS since I was doing a clean install anyway. Ultimately I decided to stick with LVM+ext4 because I had already setup caching volumes, thin pools, and I didn’t necessarily want to wipe and restore my home volume. I am considering BTRFS on my laptop though, if nothing else than for the learning experience.

## Riley

Thanks so much for this guide. It was super helpful!

Years ago I made my laptop’s root partition smaller, so I’d have more space in my home partition. But when Flatpak became a thing, it started eating up space in my root folder. So much so, that I didn’t have enough room to download and install new versions of Fedora.

I was able to follow this simple guide and fix the issue. Now my laptop is running the latest and greatest Fedora.