(急)linux怎样更改交换空间的大小

(急)linux怎样更改交换空间的大小,第1张

从你的截图上看,你的问题应该是如何缩小LVM分区,而不是更改交换分区,请明确一下。

这篇文章,你参考一下。

How to shrink a default install LVM PV to create another partition

A default installation of Fedora will use all available free space to create two new partitions: a small /boot, and all the remaining space for a single LVM physical volume (PV). This sometimes poses problems for those who later wish to make room for more regular partitions, to install Another Operating System into, for example. The short solution: reinstall Fedora with custom sizes to leave free spacethe long solution: this howto.

Applicable to Fedora Versions

* All

Requirements

1. A default installation of Fedora using LVM (without LUKS encryption)

2. A desire to do things the hard way.

Doing the Work

Summary of steps below: Boot rescue and skip mounting, activate lvm (vgchange -ay), fsck root filesystem, shrink root filesystem (resize2fs), shrink root logical volume (lvresize), lvremove LogVol01 swap LV and add it back to allow pvresize to succeed, shrink PV (pvresize), shrink partition (parted), and finally create new partitions in the free space, now or later.

For the example below, assume we installed Fedora onto all of an 80GB drive, and now we want to squish Fedora down to 50GB to make room for a new 30GB partition <i>after it</i>.

1. Boot into rescue mode and on the last step make sure to SKIP mounting your filesystems under /mnt/sysimage

2. Activate LVM

lvm vgchange -a y

3. fsck your single, large root filesystem on LogVol00

fsck -fC /dev/VolGroup00/LogVol00

4. Shrink your (ext3) filesystem inside the LogVol00 container BEFORE shrinking its container

resize2fs -p /dev/VolGroup00/LogVol00 50G

5. Shrink the LogVol container to match

lvm lvresize /dev/VolGroup/LogVol00 --size 50G

6.

There is now enough free physical space available (lvm pvs) to shrink the physical volume by that much in order to make room for a new partition of that size or smaller. Unfortunately, the current implementation of "pvresize" is lacking - from the manpage: "pvresize will refuse to shrink PhysicalVolume if it has allocated extents after where its new end would be. In the future, it should relocate these elsewhere in the volume group if there is sufficient free space, like pvmove does."

Because of this limitation, it is necessary to temporarily remove your LogVol01 swap LV, which is still physically allocated at the very end of your PV, then add it back such that it's newly located immediately after the smaller LogVol00 nearer to the start of the PV (and partition).

You first must determine how large your current LogVol01 swap is so you can recreate it with the same size. Take note of the "LSize" column for LogVol01 from the output of:

lvm lvs

Assuming your swap was "2.00G", for example, do:

lvm lvremove /dev/VolGroup00/LogVol01

lvm lvcreate --size 2G --name LogVol01 VolGroup00

mkswap /dev/VolGroup00/LogVol01

7. Shrink the Physical Volume

First, determine how much you can shrink it, by running:

sh-3.2# lvm pvs

PV VG Fmt Attr PSize PFree

/dev/sda2 VolGroup00 lvm2 a- 80.00G 71.00G

Take note of the PV to be resized, and of PSize and PFree. You can now resize your /dev/sda2 PV down to a final size of anything between 9G and 80G. We want to shrink down to 50G in this example, leaving 30G free for later.

And now for the pvresize:

lvm pvresize /dev/sda2 --setphysicalvolumesize 50G

If this step fails, it's probably because you're trying to shrink too small, and/or there are still allocated extents -- other than swap, which you moved in the step above -- after where the new tail end would be. A future version of pvresize will automatically move those to earlier free space as needed. (As an alternative, if you have lots of free space on another drive, you could pvcreate there, then pvmove temporarily, then pvresize, and pvmove back.)

8. FINALLY - Shrink the PARTITION containing the shrunk PV to match its new size. WARNING: This is the trickiest bit to get right due to unit conversion and conventions (GB vs GiB, rounding error, etc), so we'll be using the SECTOR unit (512bytes) as well as giving ourselves a margin of error. It also involves a tiny bit of critical advanced addition arithmetic.

Display the PV sizes in SECTORS and take note of the smaller PSize:

lvm pvs --units s

Display the partition Size in SECTORS using parted and take note of the Start sector and partition Number of the partition with "lvm" in the Flags column:

parted /dev/sda unit s print

POINT OF NO RETURN - HAVE BACKUPS

So now we need to nuke the old, large partition from the partition table, and immediately recreate it with the exact same Start sector, but with a newer, shorter End sector. To calculate the new End sector, ADD the partition Start sector to the PV PSize, and then add a safety margin of around 131072 sectors (64MB) to that.

Nuke it

parted /dev/sda rm 2

Recreate it smaller + safety margin. NOTE: remeber to append the letter "s" to your values to qualify them as sectors. (e.g. "123s")

parted /dev/sda mkpart primary 401625s 105390297s

parted /dev/sda set 2 lvm on

parted /dev/sda print

fdisk -l /dev/sda

9. Almost done. OPTIONAL STEP: pvresize, lvresize, and resize2fs once more, in that order, but without an explicit size given, such that you can safely grow it back up into the small safety margin you used above. Skip it.

10. Verify you didn't make a major oops truncating something by running fsck again:

fsck -fC /dev/VolGroup00/LogVol00

11. All done. You now have some free unpartitioned space outside of LVM to play with.

检查现有的交换空间/分区的大小,使用命令 ‘free -m‘ 或者 ‘swapon -s‘

dd if=/dev/zero of=/swap_file bs=1G count=1

根据你的需要替换 ‘bs‘ 和 ‘count‘ 的大小.

chmod 600/swap_file

mkswap /swap_file

在fstab文件中添加以下条目,以便交换文件每次重启后互能继续使用.

/swap_file swap swap defaults 0

swapon /swap_file

swapoff /swap_file

计算机的存储空间问题相信大部分的管理员都有不同的处理方式。今天我们就一起来了解一下,在linux系统中,交换空间的具体使用情况是什么。希望通过对本文的阅读,大家对于linux系统有更多的了解,下面就开始今天的主要内容吧。

交换空间

交换空间是现代Linux系统中的二种内存类型。交换空间的主要功能是当全部的RAM被占用并且需要更多内存时,用磁盘空间代替RAM内存。

例如,假设你有一个8GBRAM的计算机。如果你启动的程序没有填满RAM,一切都好,不需要交换。假设你在处理电子表格,当添加更多的行时,你电子表格会增长,加上所有正在运行的程序,将会占用全部的RAM。如果这时没有可用的交换空间,你将不得不停止处理电子表格,直到关闭一些其他程序来释放一些RAM。

内核使用一个内存管理程序来检测近没有使用的内存块(内存页)。内存管理程序将这些相对不经常使用的内存页交换到硬盘上专门指定用于“分页”或交换的特殊分区。这会释放RAM,为输入电子表格更多数据腾出了空间。那些换出到硬盘的内存页面被内核的内存管理代码跟踪,如果需要,可以被分页回RAM。

Linux计算机中的内存总量是RAM+交换分区,交换分区被称为虚拟内存.

Linux交换分区类型

Linux提供了两种类型的交换空间。默认情况下,大多数Linux在安装时都会创建一个交换分区,但是也可以使用一个特殊配置的文件作为交换文件。电脑培训http://www.kmbdqn.com/发现交换分区顾名思义就是一个标准磁盘分区,由mkswap命令指定交换空间。

如果没有可用磁盘空间来创建新的交换分区,或者卷组中没有空间为交换空间创建逻辑卷,则可以使用交换文件。这只是一个创建好并预分配指定大小的常规文件。然后运行mkswap命令将其配置为交换空间。除非绝对必要,否则我不建议使用文件来做交换空间。(LCTT译注:Ubuntu近来的版本采用了交换文件而非交换空间,所以我对于这种说法保留看法)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7592709.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存