На этот вопрос уже есть ответ здесь:
У меня ноутбук с 8 ГБ ОЗУ и 1 ТБ HDD. У меня есть файл подкачки размером 2 ГБ (Ubuntu 18.04 по умолчанию использует файл подкачки вместо отдельного раздела подкачки), и я хочу увеличить его, чтобы использовать спящий режим.
Хочу увеличить с 2 ГБ до 16 ГБ. Вот скриншот GParted:
Я попытался увеличить его с помощью fallocate -l 16G
, но это не сработало.
Также есть изображение из free -m
:
Из Ubuntu 18.04 вперед, своп-файла, а не специализированного раздела подкачки используется. Файл подкачки называют "своп-файлом". Изменить размер этого файла подкачки:
Отключите файл подкачки и удалите его (не действительно необходимый, поскольку Вы перезапишете его),
sudo swapoff /swapfile
sudo rm /swapfile
Создайте новый файл подкачки желаемого размера.
Определите размер своего файла подкачки. Если Вы захотите сделать файл подкачки на 4 ГБ, то необходимо будет записать 4 * 1 024 блока 10 242 байтов (= 1 мебибайт). Это проведет Ваш подсчет, равный 4 * 1024 = 4096. Создайте файл этого размера с командой
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
Присвойте ему полномочия чтения-записи для корня только (не строго необходимый, но это сжимает безопасность),
sudo chmod 600 /swapfile
Отформатируйте файл как подкачку:
sudo mkswap /swapfile
Файл будет активирован на следующей перезагрузке. Если Вы хотите активировать его для текущей сессии:
sudo swapon /swapfile
Можно проверить подкачку, которая доступна с командой swapon -s
(никакие корневые необходимые полномочия).
От man mkswap
рекомендуется использовать dd
управляйте, как продемонстрировано в сообщении @vanadium.
If you don't know the page size that your machine uses,
you may be able to look it up with
"cat /proc/cpuinfo"
(or you may not – the contents of this file depend on architecture and kernel version).
To set up a swap file, it is necessary to create that file before
initializing it with mkswap, e.g. using a command like
# fallocate --length 8GiB swapfile
Note that a swap file must not contain any holes. Using cp(1) to
create the file is not acceptable.
Neither is use of fallocate(1) on file systems that support preallocated
files, such as XFS or ext4, or on copy-on-write filesystems like btrfs.
It is recommended to use dd(1) and /dev/zero in these cases.
Please read notes from swapon(8) before adding a swap file to copy-on-
write filesystems.
И здесь примечания man swapon
NOTES
You should not use swapon on a file with holes. This can be seen in
the system log as
swapon: swapfile has holes.
The swap file implementation in the kernel expects to be able to write
to the file directly, without the assistance of the filesystem. This
is a problem on preallocated files (e.g. fallocate(1)) on filesys‐
tems like XFS or ext4, and on copy-on-write filesystems like btrfs.
It is recommended to use dd(1) and /dev/zero to avoid holes on XFS
and ext4.
swapon may not work correctly when using a swap file with some
versions of btrfs. This is due to btrfs being a copy-on-write
filesystem: the file location may not be static and corruption can
result.
Btrfs actively disallows the use of swap files on its filesystems
by refusing to map the file.
One possible workaround is to map the swap file to a loopback device.
This will allow the filesystem to determine the mapping properly but
may come with a performance impact.
Swap over NFS may not work.