本文首發(fā)于 https://blog.kangxuanpeng.com/post/ubuntu-add-swap-size
背景
在 apt install
的時(shí)候遇到了錯(cuò)誤 FATAL -> Failed to fork.
[root@Ubuntu:~]# apt install xxx
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
FATAL -> Failed to fork.
經(jīng)過(guò)排查發(fā)現(xiàn)是交換空間不足生闲,所以本文是在 Ubuntu 環(huán)境下增加 swap 交換空間裳瘪。
解決步驟
查看當(dāng)前系統(tǒng) swap 大小
如下所示,當(dāng)前的系統(tǒng)交換空間已用完:
[root@Ubuntu:~]# free -m
total used free shared buff/cache available
Mem: 481 339 9 15 131 114
Swap: 0 0 0
[root@Ubuntu:~]# df -h
Filesystem Size Used Avail Use% Mounted on
udev 210M 0 210M 0% /dev
tmpfs 49M 656K 48M 2% /run
/dev/vda1 9.8G 3.1G 6.3G 34% /
tmpfs 241M 0 241M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 241M 0 241M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/1000
指定 swap 文件
/* 必須用 root 賬號(hào)
[root@Ubuntu:~]# mkdir /swap
[root@Ubuntu:~]# cd /swap/
[root@Ubuntu:swap]# ls
[root@Ubuntu:swap]# dd if=/dev/zero of=swapfile bs=100M count=50
50+0 records in
50+0 records out
5242880000 bytes (5.2 GB, 4.9 GiB) copied, 43.7988 s, 120 MB/s
命令解釋:
1 if=文件名:輸入文件名昼弟,缺省為標(biāo)準(zhǔn)輸入。即指定源文件奕筐。< if=input file >
2 of=文件名:輸出文件名舱痘,缺省為標(biāo)準(zhǔn)輸出。即指定目的文件离赫。< of=output file >
3 ibs=bytes:一次讀入bytes個(gè)字節(jié)芭逝,即指定一個(gè)塊大小為bytes個(gè)字節(jié)。
obs=bytes:一次輸出bytes個(gè)字節(jié)渊胸,即指定一個(gè)塊大小為bytes個(gè)字節(jié)旬盯。
bs=bytes:同時(shí)設(shè)置讀入/輸出的塊大小為bytes個(gè)字節(jié)。
4 cbs=bytes:一次轉(zhuǎn)換bytes個(gè)字節(jié)翎猛,即指定轉(zhuǎn)換緩沖區(qū)大小胖翰。
5 skip=blocks:從輸入文件開(kāi)頭跳過(guò)blocks個(gè)塊后再開(kāi)始復(fù)制。
6 seek=blocks:從輸出文件開(kāi)頭跳過(guò)blocks個(gè)塊后再開(kāi)始復(fù)制切厘。
注意:通常只用當(dāng)輸出文件是磁盤(pán)或磁帶時(shí)才有效萨咳,即備份到磁盤(pán)或磁帶時(shí)才有效。
7 count=blocks:僅拷貝blocks個(gè)塊疫稿,塊大小等于ibs指定的字節(jié)數(shù)培他。
轉(zhuǎn)換 swap 文件
[root@Ubuntu:swap]# mkswap -f swapfile
mkswap: swapfile: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 4.9 GiB (5242875904 bytes)
no label, UUID=18adba41-e214-48d6-a8d7-eb06d7e7137c
[root@Ubuntu:swap]# chmod 0600 /swap/swapfile
[root@Ubuntu:swap]# mkswap -f swapfile
mkswap: swapfile: warning: wiping old swap signature.
Setting up swapspace version 1, size = 4.9 GiB (5242875904 bytes)
no label, UUID=148b286b-a2c6-4ae2-beff-d7d3e0403df0
激活 swap 文件
用 swapon
命令來(lái)激活,可以看到激活前后的交換空間大小變化
[root@Ubuntu:swap]# free -m
total used free shared buff/cache available
Mem: 481 326 17 15 137 127
Swap: 0 0 0
[root@Ubuntu:swap]# swapon /swap/swapfile
[root@Ubuntu:swap]# free -m
total used free shared buff/cache available
Mem: 481 328 15 15 137 125
Swap: 4999 0 4999
/* 取消掛載則是 swapoff swap
至此遗座,成功增加了交換空間舀凛。又可以愉快地 apt install
了。