먹고 살려고 공부

swap.img와 디스크 사용량 본문

Linux

swap.img와 디스크 사용량

10104 2025. 1. 31. 11:45

PostgreSQL을 백업하려고 보니 사용 공간이 부족하다고 한다.

pg_dump: error: could not write to output file: No space left on device

 

확인 결과 DISK가 FULL나 있다.

postgres@test:/$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              387M  1.6M  386M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   38G   36G     0 100% /
tmpfs                              1.9G  1.1M  1.9G   1% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  182M  1.7G  10% /boot
tmpfs                              387M   12K  387M   1% /run/user/1000

 

일반적으로 ls -lh 커맨드로는 현재 디렉토리 용량을 확인하기 쉽지 않다. 파일의 경우에는 확인하기 용이하다.

swap.img가 3.8G인 것을 확인할 수 있는데 해당 내용은 아래에서 다루겠다.

root@test:/# ls -lh
total 3.8G
lrwxrwxrwx   1 root     root        7 Apr 22  2024 bin -> usr/bin
drwxr-xr-x   2 root     root     4.0K Feb 26  2024 bin.usr-is-merged
drwxr-xr-x   4 root     root     4.0K Jan 28 18:46 boot
dr-xr-xr-x   2 root     root     4.0K Aug 28 00:39 cdrom
...
-rw-------   1 root     root     3.8G Dec 16 11:23 swap.img
dr-xr-xr-x  13 root     root        0 Jan 31 11:11 sys
drwxrwxrwt  13 root     root     4.0K Jan 31 10:57 tmp
drwxr-xr-x  12 root     root     4.0K Aug 27 23:18 usr
drwxr-xr-x  13 root     root     4.0K Dec 16 11:25 var

 

디스크 사용량이 많은 디렉토리를 찾을 경우에는 아래의 커맨드를 이용한다.

root@test:/# du -h --max-depth=1 /
16K     /lost+found
...
22G     /var
4.0K    /cdrom
28K     /pg_archive
29G     /

 

특정 디렉토리 내에서 사용량을 확인하는 경우에는 아래의 커맨드를 사용한다.

root@test:/var# du -sh * | sort -rh
22G     lib
390M    log
131M    cache
7.5M    crash
2.0M    backups
44K     tmp
16K     spool
4.0K    snap
4.0K    opt
4.0K    mail
4.0K    local
0       run
0       lock

 

swap이란?

https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace

 

Chapter 15. Swap Space | Red Hat Product Documentation

FormatMulti-pageSingle-pageView full doc as PDF

docs.redhat.com

리눅스에서 스왑이란 물리적 메모리공간(RAM)이 가득 찼을 때 사용된다. 만약 RAM은 FULL이고 시스템에서 메모리 리소스를 더 필요로 할 경우, 비활성화된 페이지가 스왑 공간으로 이동한다. RAM이 작은 경우 유용하지만, RAM의 대체제로 생각하면 안된다.

스왑 공간을 이용하여 데이터를 디스크로 옮기는 역할이다.

 

실제로 free -h를 통해서 swap.img 사용량을 확인한다.

현재 사용량이 0인 것을 확인할 수 있다.

root@test:/var# free -h
               total        used        free      shared  buff/cache   available
Mem:           3.8Gi       557Mi       181Mi        15Mi       3.4Gi       3.2Gi
Swap:          3.8Gi        12Ki       3.8Gi

 

swap을 비활성화 하는 방법은 다음과 같다.

root@test:/# sudo swapoff /[swap.img의 path]/
root@test:/# sudo swapoff /swap.img

 

swap 삭제

root@test:/# sudo rm /swap.img

 

swap 마운트 비활성화

root@test:/# sudo nano /etc/fstab //(ctrl+x, y, enter)