ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [리눅스] 루프백 디바이스(/dev/loopXX)
    Linux 2023. 4. 25. 18:59

    글의 전제

    - 내가 글을 쓰다가 궁금한 점은 파란색 볼드체로 표현했다. 나도 모르기 때문에 나중에 알아봐야 할 내용이라는 뜻이다.

    - 밑줄로 작성된 글은 좀 더 긴 설명이 필요해서 친 것이다. 그러므로, 밑 줄 처친 글이 이해가 안간다면 링크를 따라서 관련 내용을 공부하자.

    - `글의 참조`에서 빨간색 볼드체로 체크된 링크는 이 글을 작성하면 가장 많이 참조한 링크다.


    글의 내용

     

    - 루프백 디바이스

    - Loopback device를 왜 쓸까? Loopback device는 일반 파일을 마치 block device처럼 다루게 해준다는 것이다. block device는 storage device(Hard disk, CD, Floppy, Flash Memory, SD Card, SSD 등등)를 생각하면 된다. 아래의 예시를 루프백 디바이스 왜 쓰인지 알 수 있을 것이다.

     

    In Linux, the loopback device (/dev/loop0) is a block device that allows a file to be treated as a block device, which can then be mounted like any other block device. This can be useful for a variety of purposes, such as creating disk images or mounting disk images containing filesystems.

    Here's an example of how to use the loopback device:

    1. Create a file to act as the disk image, using the dd command:
    $ dd if=/dev/zero of=mydisk.img bs=1M count=100​

    This creates a 100MB file called "mydisk.img" filled with zeroes.

    2. Use the losetup command to associate the loopback device (/dev/loop0) with the file:
    $ sudo losetup /dev/loop0 mydisk.img​

    This associates /dev/loop0 with the "mydisk.img" file.

    3. Create a filesystem on the loopback device using the mkfs command:

    $ sudo mkfs.ext4 /dev/loop0

    This creates an ext4 filesystem on the loopback device.

    4. Create a directory to mount the filesystem:

    $ mkdir mymountpoint


    5. Mount the loopback device to the directory:

    $ sudo mount /dev/loop0 mymountpoint

    This mounts the filesystem on the loopback device to the "mymountpoint" directory.

    Now, any files written to the "mymountpoint" directory will be stored on the "mydisk.img" file, and can be accessed like any other filesystem. When finished, the loopback device can be disconnected using the losetup command:

    $ sudo losetup -d /dev/loop0

    This will detach the loopback device from the file.

     

    위의 내용에서 3가지를 기억해야 한다.

    1) mydisk.img 파일을 루프백 디바이스에 연결한다. 이렇게 하면 mydisk.img 파일이 마치 block device처럼 동작하게 된다. 왜 block device처럼 만들까? 여기에 뭔가를 쓰고 싶기 때문이다. 대개는 이 mydisk.img를 booting image로 만들기 위해서다. 

     

    2) 해당 파일을 읽고 쓰기 위해서는 파일 시스템으로 포맷이 되어야 한다. 즉, 파일 시스템으로 포맷이 되지 않으면 해당 파일에 read/write가 불가능하다. 이미지 파일을 읽고 쓰기가 가능하려면 파일 시스템으로 포맷해야 한다. 파일 시스템으로 포맷하지 않으면, 운영체제에서 해당 파일의 내용을 알 수가 없다. 그래서 포맷을 해야한다. 루프백 디바이스를 FAT32 혹은 EXT2 파일 시스템으로 포맷하면, 루프백 다비이스와 연결된 mydisk.img 파일이 포맷된다. 

     

    3) 자 이제 mydisk.img 파일을 파일 시스템으로 포맷이 됬으니 작업을 해보자. 근데 어떻게 작업을 할까? 아마 해보지는 않았지만, mydisk.img는 이미지는 루프백 디바이스 덕분에 마치 블락 디바이스처럼 동작하고 포맷도 됬겠다 Shell이나 일반 어플리케이션에서 FIle system API를 통해서 read/write 가 가능할 것이다. 그런데 개발자가 아니라면? 일반 사용자들은 사실 Shell 이나 어플리케이션을 작성할 주 모른다. 그러면, 저 mydisk.img에 read/write 작업을 할 수 없는 걸까? 이 때, 리눅스에서 제공해주는 기능이 mount이다. mymountpount 라는 폴더를 하나 만들고 그 폴더와 mydisk.img를 연결(마운트) 시키면, 해당 폴더에 파일을 만드는 작업을 하면 그대로 mydisk.img에 동일하게 쓰인다. 그리고 mount는 block device와 폴더를 매핑해주는 명령어이다. 즉, `mount uefi.img mymountpoint/` 라는 명령어는 에러가 발생한다. uefi.img를 loopback device와 연결시켜 block device처럼 동작시킬 수 있기 때문에, mount 명령이 가능한 것이다.

     

    4) 근데 위의 짓을 왜 할까? 대개는 처음에 말한 것처럼 booting image를 만들기 위해서 쓰인다. 그런데 이런 경우도 있을 수 있다. 내가 가진 SSD 용량이 256GB이고 위에서 mydisk.img를 200GB로 설정하고 파일 시스템을 ext4로 만들었다고 치자. 그리고 나는 mydisk.img에 열심히 작업을 했다. 1년뒤, mydisk.img가 거의 꽉 찼다. 이걸 지우기도 아깝고 클라우드에 모두 올리기에도 좀 양이 크다. 그럼 이렇게 하면 된다. 새로운 SSD 256G 를 하나 더 산다. 그리고 그 SSD를 ext4로 포맷한 뒤 mydisk.img를 마운트한 폴더(mymountpoint)를 새로 구매한 SSD에 마운트하면 된다. 

     

     

    - 그런데 이런 경우도 있을 것이다. 내가 해킹을 해서 uefi.img 라는 파일을 얻어냈다. 아는 내용은 이 이미지의 포맷이 FAT32 파일 시스템을 사용한다고 한다. 이 파일의 내용을 어떻게 확인해야 할까? 이때도 loopback device를 이용해서 내용을 확인해볼 수 있다. 

     

    losetup /dev/loop29 ./uefi.img

     

    mount /dev/loop29 ./esp/

     

    이렇게 하면 uefi.img가 esp/ 폴더와 매핑되어 esp/ 폴더를 통해 uefi.img의 자료를 볼 수가 있다.

     

     

    umount ./esp/
    $ losetup -d /dev/loop29

    위의 2개의 명령어후에 아래와 같이 폴더가 원래대로 돌아온 것을 볼 수 있다.

     

     

     

    - losetup(/dev/loopX)

    : 루프백 디바이스를 다루는 명령어를 의미한다. 옵션들은 다음과 같다.

     

    : losetup --help

    " 도움말

     

    : losetup -a

    " 현재 사용중인 모든 루프백 디바이스를 보여준ㄷ.

     

    : losetup -f

    " 사용가능한 가장 첫 번째 루프백 다비이스를 알려준다.

     

    : losetup -d <loopdev>

    " 해당 루프백디바이스를 해제한다.

    'Linux' 카테고리의 다른 글

    [리눅스] boot process overview  (0) 2023.08.03
    [리눅스] - !!${변수} 의미  (0) 2023.08.03
Designed by Tistory.