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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(急)linux怎样更改交换空间的大小相关的知识,希望对你有一定的参考价值。

我想新建一个新的交换分区,可是满了,我又不想删了他。怎样才能把他减少呢
3楼的答案正确,可是~~你就不能翻译为中文么~~~这里怎么说都是中国,不过你好厉害~~~还是要谢谢的

从你的截图上看,你的问题应该是如何缩小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 space; the 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.
参考技术A 0,用这个命令fdisk新建一个分区,分区类型是swap
1,用这个命令mkswap来创建一个交换空间,具体信息可查看手册
2,用这个命令swapon来挂载交换空间
4,用这个命令swapoff来缷载交换空间

按这个流程来做,就可以了。有命令不会用的,查man手册
参考技术B 可以使用windows xp启动光盘的PQ-Magic来重新分区。 参考技术C 你好,我不是很理解你的提问,也说说见解吧,呵呵。

为什么要更改交换空间的大小呢?

你是在虚拟机里玩的!如果当时给的硬盘小了点,导致现在的空间不足!你完全可以再添加一块新硬盘的!

我通常在虚拟机里添加多块硬盘的!没什么问题的。

Linux命令--free

free

free命令基本介绍:用于显示内存和交换空间的使用情况。

  • Mem:内存的使用情况

  • Swap:交换空间的使用情况

  • total:总共物理内存大小

  • user:已使用的物理内存大小

  • free:可用物理内存大小

  • shared:多个进程共享的物理内存大小

  • buff/cache:磁盘缓存大小

  • available:还可以被应用程序使用的物理内存大小

free参数

  • free显示的结果默认单位为KB
  • free -h用于将结果带上单位
  • free -b/-k/-m/-g结果用字节、千字节、兆字节和G字节表示
  • free -t 增加一行总计Mem和Swap
  • free -s N每隔N秒打印一次信息
  • free参数可以组合,例如free -hs 3

以上是关于(急)linux怎样更改交换空间的大小的主要内容,如果未能解决你的问题,请参考以下文章

Linux下怎样增加虚拟内存

linux下调整交换分区的大小

linux交换空间

Linux运维之道之admin1.5(分区规划及使用,lvm逻辑卷,交换空间)

Tomcat 进程在交换空间用完后被 Linux 内核杀死;没有任何 JVM OutOfMemory 错误

在Linux6下SWAP空间不够解决方案