DEV

Everything is a File (How a storage device is treated as a file)

행운개발자 2025. 4. 13. 14:45
728x90

파일을 생성하거나 파일을 전송하면 이 파일은 physical disk에서 특정 영역을 차지한다. 그리고 이 파일을 특정한 file type을 가진다

Type Symbol Description
Normal - Normal file
Directory d Normal directory
Hard link - Additional name for existing file
Symbolic link l Shortcut to a file or directory
Socket s Pass data between 2 processes
Named pipe p Like sockets, user can't work directly with it
Character device c Processes character hardware communication
Block device b Major and minor numbers for controlling device

 

linux 시스템은 file과 directory를 서로 다르게 다루지 않는다. 다만 directory는 file을 계층의 구조로 다른 file을 저장하는 역할을 한다. 모든 H/W 구성요소들도 모두 file로 표현된다. 이에 따라 키보드, 마우스, 프로세스 간의 통신, 네트워크 통신이 이루어지는 모든input/ouput은 모두 file system space에서 streams of bytes로 다루어진다.

Normal (Ordinary / Regular) Files

  • Readable files
  • Binary files
  • Image files
  • Compressed files and so on

Directory Files

파일의 계층 구조를 제공한다.

Block device

시스템 H/W 컴포넌트에게 buffered access를 제공하는 device file이 있다. device file들은 file system을 통해서 device driver와 커뮤니케이션하는 다양한 방법들을 제공한다. Block device 타입의 file은 a large block of data and information을 주어진 시간에 전송할 수 있다. HDD, SSD와 같은 block 저장소에 사용된다.

// Listing block files sockets in a directory
hwanseok@hwanseokjangs-MacBook-Pro ~ % ls -al /dev | grep ^b
brw-------   1 root      operator   0x2000000  4 13 12:42 apfs-raw-device.2.0
brw-r-----   1 root      operator   0x1000000  4  8 11:28 disk0
brw-r-----   1 root      operator   0x1000001  4  8 11:28 disk0s1
brw-r-----   1 root      operator   0x1000003  4  8 11:28 disk0s2

Character device

시스템 H/W 컴포넌트에게 unbuffered serial access를 제공하는 device file도 있다. Character device 타입의 file은 one character를 주어지 시간에 전송할 수 있다. 키보드, 마우스, 터미널 등에 사용된다.

// Listing character files sockets in a directory
hwanseok@hwanseokjangs-MacBook-Pro ~ % ls -al /dev | grep ^c
crw-rw-rw-   1 root      wheel     0x16000000  4  8 11:28 aes_0
crw-------   1 root      wheel     0x15000001  4  9 14:03 afsc_type5
crw-------   1 root      wheel      0xb000000  4  8 11:28 auditpipe
crw-r--r--   1 root      wheel      0xa000002  4  8 11:28 auditsessions
crw-------   1 root      wheel     0x1d000000  4  8 11:28 autofs
crw-------   1 root      wheel     0x21000000  4  8 11:28 autofs_control
crw-rw-rw-   1 root      wheel     0x200052e4  4  8 11:28 autofs_homedirmounter

Block Device Mount

아래와 같이 block device가 존재하는 상황에서

$ ls -al /dev | grep ^b
brw-rw----  1 root disk    259,   0  9월  6  2023 nvme0n1
brw-rw----  1 root disk    259,   1  9월  6  2023 nvme0n1p1
brw-rw----  1 root disk    259,   2  9월  6  2023 nvme0n1p128

lsblk 명령어를 사용해서 List information about block devices 를 할 수 있다.

// 기본 명령어
$ lsblk
NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1       259:0    0   8G  0 disk
├─nvme0n1p1   259:1    0   8G  0 part /
└─nvme0n1p128 259:2    0   1M  0 part

File System Type을 추가로 확인하기 위해서는 FSTYPE을 추가하면 된다.

// 
$ lsblk --output NAME,RM,SIZE,RO,TYPE,FSTYPE,MOUNTPOINT
NAME          RM SIZE RO TYPE FSTYPE MOUNTPOINT
nvme0n1        0   8G  0 disk
├─nvme0n1p1    0   8G  0 part xfs    /
└─nvme0n1p128  0   1M  0 part

lsblk은 block device가 attach는 되었지만 mount되지 않은 경우를 확인할 때 도움이 될 수 있다.

$ lsblk
NAME          MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1       259:0    0  50G  0 disk
nvme0n1       259:1    0   8G  0 disk
├─nvme0n1p1   259:2    0   8G  0 part /
└─nvme0n1p128 259:3    0 1M  0 part

Symbolic Link Files

symbolic link는 시스템에 존재하는 다른 파일에 대한 reference이다.
symbolic link가 참조하는 다른 파일은 directory이거나 regular file일 수 있다.

hwanseok@hwanseokjangs-MacBook-Pro study-linux % touch file1.txt
hwanseok@hwanseokjangs-MacBook-Pro study-linux % ln -s ./file1.txt file2.txt
hwanseok@hwanseokjangs-MacBook-Pro study-linux % ls -al
total 0
drwxr-xr-x@  4 hwanseok  staff   128  4 13 13:49 .
drwxr-xr-x@ 32 hwanseok  staff  1024  4 13 13:47 ..
-rw-r--r--@  1 hwanseok  staff     0  4 13 13:47 file1.txt
lrwxr-xr-x@  1 hwanseok  staff    11  4 13 13:49 file2.txt -> ./file1.txt

Pipes or Named Pipes

pipe는 프로세스 사이에 커뮤니케이션을 가능하게해주는 file이다. 한 프로세스의 output을 다른 프로세스의 input으로 연결시켜준다.

// terminal 1
hwanseok@hwanseokjangs-MacBook-Pro study-linux % mkfifo pipe-test
hwanseok@hwanseokjangs-MacBook-Pro study-linux % echo "test data" > pipe-test
hwanseok@hwanseokjangs-MacBook-Pro study-linux %

// terminal 2 
hwanseok@hwanseokjangs-MacBook-Pro study-linux % while read line ;do echo "This was passed-'$line' "; done < pipe-test
This was passed-'test data'

Socket Files

socket은 다른 환경에 있는 프로세스 사이에서 커뮤니케이션을 할 수 있게 해주는 file이다.

int socket_desc= socket(AF_INET, SOCK_STREAM, 0 );

위 코드에서 socket_desc는 file descriptor와 동일한 값이고, read() write()를 사용해서 socket에 system call을 요청할 수 있다.

Reference

728x90