문서의 이전 판입니다!


tmpfs와 /tmp

이강우 2024/10/17 07:36

tmpfs란 간단히 말해 메모리 기반 파일시스템이다. 메모리의 여유영역을 램드라이브처럼 활용하는 기능이라고 보면 된다.
관련링크 : https://www.kernel.org/doc/html/v6.6/filesystems/tmpfs.html

당연하게도 메모리 영역이므로 데이터가 영구적으로 보관되지 않으며 실제로는 파일시스템으로 마운트 하여 사용하는동안에만 데이터가 유지되고 마운트를 해제하는 즉시 내용이 소실되는 형태로 동작한다.

그래서 /tmp 같은 임시 영역을 tmpfs로 구성하여 사용하는 방식이 나왔다.

/usr/lib/systemd/system/tmp.mount 파일의 내용을 보면 아래와 같다.

#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Temporary Directory /tmp
Documentation=https://systemd.io/TEMPORARY_DIRECTORIES
Documentation=man:file-hierarchy(7)
Documentation=https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
ConditionPathIsSymbolicLink=!/tmp
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target

[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime,nosuid,nodev,size=50%%,nr_inodes=1m

# Make 'systemctl enable tmp.mount' work:
[Install]
WantedBy=local-fs.target

위와 같이 해당 systemd 설정에 /tmptmpfs타입으로 마운트 하여 쓰게 설정되어있다.

사용하려면

$ systemctl enable tmp.mount

하게 되면 바로 /tmp 경로가 tmpfs타입으로 변경된다.

하지만 이것은 권장되는 방식이 아니며 시스템에 이미 자동으로 사용할 수 있도록 구성되어있다.

/usr/lib/systemd/system/basic.target 내용을 보게되면 아래와 같다.

#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Basic System
Documentation=man:systemd.special(7)
Requires=sysinit.target
Wants=sockets.target timers.target paths.target slices.target
After=sysinit.target sockets.target paths.target slices.target tmp.mount

# We support /var, /tmp, /var/tmp, being on NFS, but we don't pull in
# remote-fs.target by default, hence pull them in explicitly here. Note that we
# require /var and /var/tmp, but only add a Wants= type dependency on /tmp, as
# we support that unit being masked, and this should not be considered an error.
RequiresMountsFor=/var /var/tmp
# RHEL-only: Disable /tmp on tmpfs.
#Wants=tmp.mount

맨 아래쪽을 보면

# RHEL-only: Disable /tmp on tmpfs.
#Wants=tmp.mount

와 같이 설정되어있다. RHEL은 기본적으로 /tmptmpfs타입을 사용하지 않도록 되어있다.
Wants=tmp.mount 부분의 주석만 해제하면 자동으로 /tmptmpfs타입으로 사용할 수 있다.

이런경우 관리자가 수동으로 umount /tmp를 하거나 systemctl stop tmp.mount를 한다 하여도 일정 시간 뒤에 systemd가 자동으로 감지하여 다시 tmp.mount를 수행하게 되므로 주의하기 바란다.

로그인하면 댓글을 남길 수 있습니다.
  • tmpfs와_tmp.1729151485.txt.gz
  • 마지막으로 수정됨: 2024/10/17 07:51
  • 저자 koov