如何创建交换文件?

我有500Gib硬盘和2Gib RAM。 我有ubuntu 12.04

当我最初参与磁盘时,我有未分配的空间,因为我有另一个带有Windows的磁盘。 这个磁盘坏了,所以我不得不在这个磁盘上安装Windows,并且必须擦除Swap Partition。

现在,当我处理大量图片,video等时,计算机正在变慢,我认为这是因为我没有交换文件。

我的一个朋友给了我下面的命令,但是计数给了我2gib的swapfile,我希望有4个交换文件的Gib。

请问您能告诉我哪个号码?

sudo dd if=/dev/zero of=/swapfile.swap bs=4096 count=1048576 

将你的计数乘以2。

如果
1048576个块,每个大小为4086bytes = 2GB
然后,
2097152块,每块大小为4086bytes = 4GB

**对于阅读此内容的其他人,上述值不准确。 它们基于OP **给出的值

关于交换的Arch Wiki文章中描述了如何创建交换文件的基本步骤。 我冒昧地将所有这些步骤压缩成一个脚本。基本用法:

用法很简单:

 sudo ./addswap.sh INTEGER LETTER 

要添加1千兆字节,你可以使用sudo ./addswap.sh 1 G 要添加1兆字节,请执行sudo ./addswap.sh 1 M

脚本

我的个人GitHub存储库中也提供了此脚本。

 #!/bin/bash set -e # bail if anything goes wrong is_root(){ if [ "$( id -u )" -ne 0 ] ; then return 1 fi return 0 } get_swap_amount(){ # Obtain amount of swap in Gigabytes awk '/SwapTotal/{printf "%.2f",($2/1048576)}' /proc/meminfo } make_swap_file(){ # This is the function that does the job of creatig swap file # and enabling it. All files are timestamped printf "Current swap ammount: %f\n" "$(get_swap_amount)" printf "Working on creating swap file\n" DATE=$(date +%s) # append date of creation to filename filename="/swapfile.""$DATE" # File will be /swapfile.$DATE dd if=/dev/zero of="$filename" bs=1"$2" count="$1" chmod 600 "$filename" mkswap "$filename" && swapon "$filename" && printf "\nCreated and turned on %s\n" "$filename" printf "Current swap ammount: %f" "$(get_swap_amount)" } ask_to_enable_on_boot(){ # Prompt user to enable this new swap file on boot. If user # enters y, the swap file will be added to /etc/fstab printf "Do you want to turn on this file at boot?[y/n]\n" read ANSWER case "$ANSWER" in [Yy]) printf "\n%s none swap defaults 0 0\n" "$filename" >> /etc/fstab && printf "\n %s added to /etc/fstab successfuly\n" "$filename" exit 0 ;; [Nn]) printf "Exiting\n" && exit 0 ;; *) printf "Wrong input: %s . Exiting. /etc/fstab not altered\n" "$ANSWER" && exit 1 ;; esac } bad_arguments(){ # check if second argument is a character case "$2" in [AZ]) return 1;; *) return 0;; esac # Check if first argument is a digit. # https://stackoverflow.com/a/3951175/3701431 case "$1" in ''|*[!0-9]*) return 0;; *) return 1 ;; esac } main(){ # Check if we're root and if args are OK. If everything is ok, do stuff if is_root then if [ $# -ne 2 ] || bad_arguments "$@" then printf "%s\n" ">>> ERR: $0: bad or insufficient arguments" > /dev/stderr printf "%s\n" ">>> Usage: $0 INTEGER LETTER" > /dev/stderr exit 2 fi make_swap_file "$@" && ask_to_enable_on_boot else printf ">>> ERR: $0 must run as root\n" > /dev/stderr exit 1 fi } main "$@" 

测试运行

 $ sudo ./addswap.sh 1 G [sudo] password for xieerqi: Current swap ammount: 4.000000 Working on creating swap file 1+0 records in 1+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 6.83322 s, 157 MB/s Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes) no label, UUID=8bf1d78d-0a8a-478b-a783-38c5935c362f Created and turned on /swapfile.1498976162 Current swap ammount: 5.000000Do you want to turn on this file at boot?[y/n] Y /swapfile.1498976162 added to /etc/fstab successfuly $ 

要创建4GB的交换文件,您可以运行:

 sudo dd if=/dev/zero of=swapfile bs=1K count=4M 

所以通过使用乘法后缀,它更容易计数(1K * 4M = 4 GiB)。

然后,您需要将该文件转换为交换空间,并启用它,因此:

 sudo mkswap swapfile sudo swapon swapfile 

有关更多详细信息,请检查: 如何添加交换?