我有带 8 GB RAM 和 1TB HDD 的笔记本电脑。我有 2 GB 的交换文件(Ubuntu 18.04 默认使用交换文件而不是单独的交换分区),我想增加它以使用休眠。
我想将它从 2 GB 增加到 16 GB。这是 GParted 的屏幕截图:
我试图增加它,fallocate -l 16G但它没有用。
还有图片来自free -m:
van*_*ium 127
从 Ubuntu 18.04 开始,使用交换文件而不是专用交换分区(使用 LVM 时除外)。交换文件被命名为“swapfile”。要更改此交换文件的大小:
禁用交换文件并删除它(不是真的需要,因为你会覆盖它)
sudo swapoff /swapfile
sudo rm /swapfile
Run Code Online (Sandbox Code Playgroud)
创建所需大小的新交换文件。感谢用户 Hackinet,您可以使用以下命令创建 4 GB 交换文件
sudo fallocate -l 4G /swapfile
Run Code Online (Sandbox Code Playgroud)
在这个命令中,调整4G到你想要的大小。
或者,您可以使用dd但提供正确的参数需要一些计算。如果要制作 4 GB 交换文件,则需要写入 4 * 1024 个 1024 2字节 (= 1 MiB) 的块。这将使您的计数等于 4 * 1024 = 4096。使用命令创建此大小的文件
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
Run Code Online (Sandbox Code Playgroud)
仅为 root 分配读/写权限(不是严格需要的,但它加强了安全性)
sudo chmod 600 /swapfile
Run Code Online (Sandbox Code Playgroud)
将文件格式化为交换:
sudo mkswap /swapfile
Run Code Online (Sandbox Code Playgroud)
该文件将在下次重新启动时激活。如果要为当前会话激活它:
sudo swapon /swapfile
Run Code Online (Sandbox Code Playgroud)
您可以使用该命令检查可用的交换swapon -s(不需要 root 权限)。
建议man mkswap使用@vanadium postdd中演示的命令。
If you don\'t know the page size that your machine uses, \nyou may be able to look it up with \n"cat /proc/cpuinfo" \n(or you may not \xe2\x80\x93 the contents of this file depend on architecture and kernel version).\n\n To set up a swap file, it is necessary to create that file before \n initializing it with mkswap, e.g. using a command like\n\n # fallocate --length 8GiB swapfile\n\n Note that a swap file must not contain any holes. Using cp(1) to \n create the file is not acceptable.\n Neither is use of fallocate(1) on file systems that support preallocated \n files, such as XFS or ext4, or on copy-on-write filesystems like btrfs. \n\n It is recommended to use dd(1) and /dev/zero in these cases.\n Please read notes from swapon(8) before adding a swap file to copy-on- \n write filesystems.\nRun Code Online (Sandbox Code Playgroud)\n\n这里的注释man swapon
NOTES\n You should not use swapon on a file with holes. This can be seen in\n the system log as\n\n swapon: swapfile has holes.\n\n The swap file implementation in the kernel expects to be able to write \n to the file directly, without the assistance of the filesystem. This \n is a problem on preallocated files (e.g. fallocate(1)) on filesys\xe2\x80\x90\n tems like XFS or ext4, and on copy-on-write filesystems like btrfs.\n\n It is recommended to use dd(1) and /dev/zero to avoid holes on XFS\n and ext4.\n\n swapon may not work correctly when using a swap file with some \n versions of btrfs. This is due to btrfs being a copy-on-write \n filesystem: the file location may not be static and corruption can \n result. \n Btrfs actively disallows the use of swap files on its filesystems\n by refusing to map the file.\n\n One possible workaround is to map the swap file to a loopback device. \n This will allow the filesystem to determine the mapping properly but \n may come with a performance impact.\n\n Swap over NFS may not work.\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
60786 次 |
| 最近记录: |