메뉴 건너뛰기

Korea Oracle User Group

OS

Linux Swap File 생성하기

명품관 2019.04.15 11:34 조회 수 : 653

Swap File 생성하기

 

1.현재의 Swap 정보 확인

[root@testora /]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       4194300 1576020 -1
[root@testora /]# free -h
             total       used       free     shared    buffers     cached
Mem:          2.9G       2.7G       190M       715M       216M       1.0G
-/+ buffers/cache:       1.4G       1.4G 
Swap:         4.0G       1.5G       2.5G 

 

위 정보와 같이 Swap 공간이 모자르지는 않지만 테스트를 위해 Swap File을 추가 해 보기로 한다.

 

 

2.Swap File로 사용할 File을 생성

[root@testora /]# ls -al /imsi_swapfile
ls: cannot access /imsi_swapfile: 그런 파일이나 디렉터리가 없습니다
[root@testora /]# dd if=/dev/zero of=/imsi_swapfile bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.826755 s, 634 MB/s
[root@testora /]# ls -lh /imsi_swapfile
-rw-r--r--. 1 root root 500M 2019-04-15 11:07 /imsi_swapfile

 

 

3.적절한 파일 권한으로 변경

[root@testora /]# chmod 600 /imsi_swapfile
[root@testora /]# ls -lh /imsi_swapfile   
-rw-------. 1 root root 500M 2019-04-15 11:07 /imsi_swapfile

 

 

4.Swap으로 등록

[root@testora /]# mkswap /imsi_swapfile
mkswap: /imsi_swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=8c632ca0-3a21-425f-8aa6-79c0a90e3db5
[root@testora /]# swapon /imsi_swapfile

 

재부팅 후에도 추가한 Swap 공간이 적용되어 사용되기를 원한다면 /ect/fstab 파일에 아래 항목을 추가해야 한다.

 

/imsi_swapfile swap swap defaults 0 0

 


5.추가된 Swap 정보 확인

[root@testora /]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       4194300 1576000 -1
/imsi_swapfile                          file            511996  0       -2
[root@testora /]# free -h
             total       used       free     shared    buffers     cached
Mem:          2.9G       2.8G       100M       715M        90M       1.3G
-/+ buffers/cache:       1.4G       1.4G 
Swap:         4.5G       1.5G       3.0G 
[root@testora /]# 

 

위와 같이 추가된 정보를 확인할 수 있다.

 

 

6.추가적인 정보

 

swappiness 설정이 존재한다. 시스템이 Swap 사용에 대한 선호도를 1-100 까지의 값으로 설정할 수 있다.
작은 값일수록 커널이 Swap 사용을 줄이려고 하며 큰 값일수록 커널이 Swap을 적극적으로 사용할려고 하는 경향이 커진다.

 

[root@testora /]# cat /proc/sys/vm/swappiness
60
[root@testora /]# sysctl vm.swappiness=50
vm.swappiness = 50
[root@testora /]# cat /proc/sys/vm/swappiness
50

 

이 파라미터 값을 재부팅 후에도 적용하고 싶다면 /etc/sysctl.conf 파일에 아래 항목을 추가한다.

vm.swappiness=50

 

# swappiness value
vm.swappiness=50
"/etc/sysctl.conf" 52L, 1420C written

 

 

3.Swap File 삭제

 

[root@testora /]# swapoff -v /imsi_swapfile
swapoff on /imsi_swapfile
[root@testora /]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       4194300 1575988 -1
[root@testora /]# free -h
             total       used       free     shared    buffers     cached
Mem:          2.9G       2.8G        98M       715M        91M       1.3G
-/+ buffers/cache:       1.4G       1.4G
Swap:         4.0G       1.5G       2.5G
[root@testora /]# ls -al /imsi_swapfile
-rw-------. 1 root root 524288000 2019-04-15 11:09 /imsi_swapfile
[root@testora /]# rm /imsi_swapfile
rm: remove 일반 파일 `/imsi_swapfile'? y
[root@testora /]# ls -al /imsi_swapfile
ls: cannot access /imsi_swapfile: 그런 파일이나 디렉터리가 없습니다
[root@testora /]#

 

/etc/fstab에 적용해 놓았다면 해당 항목을 제거한다.

위로