我刚刚升级了 CPU/主板/RAM,现在我想增加交换大小,而不必重新格式化整个驱动器。这可能吗?我的硬盘设置如下。
mount
/dev/mapper/ubuntu-root on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
none on /run/user type tmpfs (rw,noexec,nosuid,nodev,size=104857600,mode=0755)
/dev/sda1 on /boot type ext2 (rw)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
gvfsd-fuse on /run/user/raph/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,user=raph)
sudo fdisk -l
Disk /dev/sda: 200.0 GB, 200049647616 bytes
255 heads, 63 sectors/track, 24321 cylinders, total 390721968 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007f9dc
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 499711 248832 83 Linux
/dev/sda2 501758 390721535 195109889 5 Extended
/dev/sda5 501760 390721535 195109888 8e Linux LVM
Disk /dev/mapper/ubuntu-root: 198.8 GB, 198784843776 bytes
255 heads, 63 sectors/track, 24167 cylinders, total 388251648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/ubuntu-root doesn't contain a valid partition table
Disk /dev/mapper/ubuntu-swap_1: 1006 MB, 1006632960 bytes
255 heads, 63 sectors/track, 122 cylinders, total 1966080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/ubuntu-swap_1 doesn't contain a valid partition table
答案1
在执行以下步骤之前,请先备份您的数据。这通常不会出现任何问题,但不包括错误、电源故障等。
- 首先 使用
e2resize
缩小根逻辑卷( )。/dev/mapper/ubuntu-root
- 然后使用
lvresize
将逻辑卷缩小到刚刚缩小的文件系统的大小。 - 现在,当您使用时
vgdisplay
,您会注意到该卷组中有一些可用空间(范围)。 - 暂时禁用交换
swapoff -a
- 您可以使用刚刚通过缩小根 LV 获得的范围来
lvextend
扩展。/dev/mapper/ubuntu-swap_1
- 使用以下方式创建并重新启用交换
mkswap /dev/mapper/ubuntu-swap_1 && swapon -a
答案2
如何在 Ubuntu 18.04 上更改交换大小,大约需要 10 分钟(无需重新启动):
sudo swapoff -a
# For 16GB of swap size
sudo dd if=/dev/zero of=/swapfile bs=1G count=16
sudo mkswap /swapfile
sudo swapon /swapfile
检查交换文件:
grep SwapTotal /proc/meminfo
sudo swapon --show
free -m