본문 바로가기
OS/Linux

[RHEL] 스왑 메모리(swap memory) 증설 테스트

by beomzh 2025. 6. 11.
728x90
반응형

스왑메모리의 증설을 테스트하고 기록하기 위해 남긴다.

내가 알기로는 일반적으로 kubernetes환경 과 오픈마루는 swap 메모리 사용을 권장하지않는다. 그러나 고객이 하라면 해야지?

 

swap 메모리는 메모리의 사용량이 부족할때, 디스크의 일부를 메모리 처럼 사용하는것으로 메모리로 사용할시 효율은 좋지않다.

# 스왑 메모리 사용 하지않을때
swapoff -a

증설 테스트의 앞서 현재 swap을 사용하지않기에 생성

현재 disk상황

root@bastion /proc# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0  100G  0 disk 
├─sda1          8:1    0  600M  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
└─sda3          8:3    0 98.4G  0 part 
  └─rhel-root 253:0    0 98.4G  0 lvm  /
sdb             8:16   0   30G  0 disk 
sr0            11:0    1 10.3G  0 rom  
root@bastion /proc# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               4.0M     0  4.0M   0% /dev
tmpfs                  7.7G   84K  7.7G   1% /dev/shm
tmpfs                  3.1G  8.8M  3.1G   1% /run
/dev/mapper/rhel-root   99G   59G   40G  60% /
/dev/sda2              960M  257M  704M  27% /boot
/dev/sda1              599M  7.1M  592M   2% /boot/efi
tmpfs                  1.6G  4.0K  1.6G   1% /run/user/0

swap 파일을 사용하지 않고 생성

/dev/sdb 의 파티션 생성

root@bastion /proc# fdisk /dev/sdb
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-206381055, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-206381055, default 206381055): +4G

Created a new partition 1 of type 'Linux' and of size 4 GiB.

Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

/dev/sdb1 생성

# 결과 출력
root@bastion ~# fdisk -l
Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: B6CA6054-2FFA-4969-9226-D702374B28E0

Device       Start       End   Sectors  Size Type
/dev/sda1     2048   1230847   1228800  600M EFI System
/dev/sda2  1230848   3327999   2097152    1G Linux filesystem
/dev/sda3  3328000 209713151 206385152 98.4G Linux LVM

Disk /dev/sdb: 30 GiB, 32212254720 bytes, 62914560 sectors
Disk model: Virtual disk    
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x4122fe1a

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8390655 8388608   4G 82 Linux swap / Solaris

Disk /dev/mapper/rhel-root: 98.41 GiB, 105667100672 bytes, 206381056 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9998e3c5

root@bastion ~# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0  100G  0 disk 
├─sda1          8:1    0  600M  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
└─sda3          8:3    0 98.4G  0 part 
  └─rhel-root 253:0    0 98.4G  0 lvm  /
sdb             8:16   0   30G  0 disk 
└─sdb1          8:17   0    4G  0 part 
sr0            11:0    1 10.3G  0 rom 

swap 적용

root@bastion ~# mkswap  /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old swap signature.
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=bfeb3de1-1485-4473-b880-9916bc85ecfd

root@bastion ~# swapon /dev/sdb1

swap 메모리 생성 확인

# 적용 완료
root@bastion ~# free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi       1.1Gi        10Gi       9.0Mi       3.9Gi        14Gi
Swap:          4.0Gi          0B       4.0Gi

신규 파티션 생성 및 swap 증설

# 파티션 생성
root@bastion ~# fdisk /dev/sdb
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (8390656-62914559, default 8390656): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (8390656-62914559, default 62914559): +12G

Created a new partition 2 of type 'Linux' and of size 12 GiB.

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 82

Changed type of partition 'Empty' to 'Linux swap / Solaris'.

Command (m for help): w
The partition table has been altered.
Syncing disks.

# 파티션 증설 확인 
**root@bastion ~# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0  100G  0 disk 
├─sda1          8:1    0  600M  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
└─sda3          8:3    0 98.4G  0 part 
  └─rhel-root 253:0    0 98.4G  0 lvm  /
sdb             8:16   0   30G  0 disk 
├─sdb1          8:17   0    4G  0 part [SWAP]
└─sdb2          8:18   0   12G  0 part 
sr0            11:0    1 10.3G  0 rom**  

# swap 적용
root@bastion ~# mkswap  /dev/sdb2 
Setting up swapspace version 1, size = 12 GiB (12884897792 bytes)
no label, UUID=28228b39-ab7a-4cab-8d46-00b07cbc96e9
root@bastion ~# swapon /dev/sdb2
root@bastion ~# free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi       1.1Gi        10Gi       9.0Mi       3.9Gi        14Gi
Swap:           15Gi          0B        15Gi

swap 영구 적용 방법

# vi 편집기로 편집 혹은 sed 명령어  혹은 echo 사용
# /etc/fstab 안에 하위 두개만 포함되면 된다.
root@bastion ~# vi /etc/fstab
/dev/sdb1            none    swap  defaults 0 0
/dev/sdb2            none    swap  defaults 0 0

swap 파일을 만들어서 적용

swap 파일 생성

root@bastion ~# mkdir /swap
root@bastion ~# fallocate -l 2G /swap/swapfile
root@bastion ~# ll /swap/
-rw-r--r-- 1 root root 2.0G 12월  4 14:17 swapfile

swap 적용

root@bastion ~# mkswap /swap/swapfile 
mkswap: /swap/swapfile: insecure permissions 0644, fix with: chmod 0600 /swap/swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=9d66d9b7-2499-407b-96df-42af5d63e892
root@bastion ~# swapon /swap/swapfile 
swapon: /swap/swapfile: insecure permissions 0644, 0600 suggested.
  • swapfile에 권한을 주지않으면 해당 메시지 발생으로 600 권한 추가
  • $ chmod 600 /swap/swapfile
# 확인
root@bastion ~ [255]# free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi       1.1Gi        10Gi       9.0Mi       3.9Gi        14Gi
Swap:           17Gi          0B        17Gi

영구 적용

# vi /etc/fstab의 하위에 swap파일의 경로입력
/swap/swapfile       none    swap  defaults 0 0

swap 삭제시 reboot이 안될때

나의 경우 부팅 중 아래 에러발생가 발생해 확인

dracut-initqueue failed to find logical volume : **rhel/swap**

grubby를 사용해서 부팅 옵션 확인 및 제거

root@bastion ~# grubby --info DEFAULT
index=0
kernel="/boot/vmlinuz-5.14.0-427.13.1.el9_4.x86_64"
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-5.14.0-427.13.1.el9_4.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (5.14.0-427.13.1.el9_4.x86_64) 9.4 (Plow)"
id="24eb30a7c46d4856a12ad53beb956ce8-5.14.0-427.13.1.el9_4.x86_64"
root@bastion ~# grubby --update-kernel=ALL --remove-args="rd.lvm.lv=rhel/swap"
root@bastion ~# grubby --update-kernel=ALL --remove-args="resume=/dev/mapper/rhel-swap"
root@bastion ~# grubby --info DEFAULT
index=0
kernel="/boot/vmlinuz-5.14.0-427.13.1.el9_4.x86_64"
args="ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M rd.lvm.lv=rhel/root rhgb quiet"
root="/dev/mapper/rhel-root"
initrd="/boot/initramfs-5.14.0-427.13.1.el9_4.x86_64.img $tuned_initrd"
title="Red Hat Enterprise Linux (5.14.0-427.13.1.el9_4.x86_64) 9.4 (Plow)"
id="24eb30a7c46d4856a12ad53beb956ce8-5.14.0-427.13.1.el9_4.x86_64"
728x90
반응형

'OS > Linux' 카테고리의 다른 글

[Linux] cp 복사 명령어  (1) 2025.06.11
[RHEL] 스왑 메모리 (Swap Memory)  (1) 2024.12.03
[Linux] 7일 전 로그 삭제  (0) 2024.11.29
[Linux] LVM  (0) 2024.11.06
[linux] rhel8 버전 간소화 보안취약점 스크립트  (0) 2024.10.31