1 losetupコマンドとは?
ファイルをループバックデバイスとして使えるようにするためのコマンドです。
ループバックデバイスの作成、削除等ができます。
2 環境
VMware Workstation 14 Playerでゲストマシンを作成しました。
ゲストマシンのOS版数、コマンド版数は以下のとおりです。
OS版数
[root@centos74 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@centos74 ~]# uname -r
3.10.0-693.el7.x86_64
コマンド版数
[root@centos74 ~]# losetup -V
losetup from util-linux 2.23.2
3 オプション一覧
[root@centos74 ~]# losetup -h
Usage:
losetup [options] [<loopdev>]
losetup [options] -f | <loopdev> <file>
オプション:
-a, --all list all used devices
-d, --detach <loopdev> [...] detach one or more devices
-D, --detach-all detach all used devices
-f, --find find first unused device
-c, --set-capacity <loopdev> resize device
-j, --associated <file> list all devices associated with <file>
-l, --list list info about all or specified
-o, --offset <num> start at offset <num> into file
-O, --output <cols> specify columns to output for --list
--sizelimit <num> device limited to <num> bytes of the file
-P, --partscan create partitioned loop device
-r, --read-only setup read-only loop device
--show print device name after setup (with -f)
-v, --verbose verbose mode
-h, --help display this help and exit
-V, --version output version information and exit
Available --list columns:
NAME loop device name
AUTOCLEAR autoclear flag set
BACK-FILE device backing file
BACK-INO backing file inode number
BACK-MAJ:MIN backing file major:minor device number
MAJ:MIN loop device major:minor number
OFFSET offset from the beginning
PARTSCAN partscan flag set
RO read-only device
SIZELIMIT size limit of the file in bytes
For more details see losetup(8).
4 ループバックデバイスを作成する方法(-f)
ループバックデバイスを2つ作成してみます。
-fオプションを指定すると、利用可能なループバックデバイスが自動的に選択されます。
そして、そのループバックデバイスにファイルが関連付けられます。
1つ目のループバックデバイス作成
1Gサイズのファイルを作成する。このファイルをループバックデバイスとして使います。
[root@centos74 ~]# fallocate -l 1G disk1.img
[root@centos74 ~]# ls -l disk1.img
-rw-r--r--. 1 root root 1073741824 4月 16 19:10 disk1.img
disk1.imgをループバックデバイスに割り当てる。
[root@centos74 ~]# losetup -f /root/disk1.img
ループバックデバイス一覧を表示する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
2つ目のループバックデバイス作成
[root@centos74 ~]# fallocate -l 1G disk2.img
[root@centos74 ~]# ls -l disk2.img
-rw-r--r--. 1 root root 1073741824 4月 16 19:26 disk2.img
disk2.imgをループバックデバイスに割り当てる。
[root@centos74 ~]# losetup -f /root/disk2.img
ループバックデバイス一覧を表示する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
/dev/loop1 0 0 0 0 /root/disk2.img
5 ループバックデバイスを削除する方法(-d)
ループバックデバイスの一覧を表示する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
/dev/loop1 0 0 0 0 /root/disk2.img
/dev/loop0を削除する。
[root@centos74 ~]# losetup -d /dev/loop0
ループバックデバイスの一覧を表示する。/dev/loop0が削除されたことがわかる。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop1 0 0 0 0 /root/disk2.img
/dev/loop1を削除する。
[root@centos74 ~]# losetup -d /dev/loop1
ループバックデバイスの一覧を表示する。/dev/loop1が削除されたことがわかる。
[root@centos74 ~]# losetup -l
[root@centos74 ~]#
6 ループバックデバイスを一括削除する方法(-D)
ループバックデバイスの一覧を表示する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
/dev/loop1 0 0 0 0 /root/disk2.img
ループバックデバイスを一括削除する。
[root@centos74 ~]# losetup -D
ループバックデバイスの一覧を表示する。全てのループバックデバイスが削除されたことがわかる。
[root@centos74 ~]# losetup -l
[root@centos74 ~]#
7 使用中のループバックデバイスを表示する方法(-a)
[root@centos74 ~]# losetup -a
/dev/loop0: [2051]:35477848 (/root/disk1.img)
/dev/loop1: [2051]:35477849 (/root/disk2.img)
8 特定のループバックデバイスを表示する方法(-j)
ループバックデバイス一覧を表示する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
/dev/loop1 0 0 0 0 /root/disk2.img
disk1.imgに割り当てたループバックデバイスを表示する。
[root@centos74 ~]# losetup -j /root/disk1.img
/dev/loop0: [2051]:35477848 (/root/disk1.img)
disk2.imgに割り当てたループバックデバイスを表示する。
[root@centos74 ~]# losetup -j /root/disk2.img
/dev/loop1: [2051]:35477849 (/root/disk2.img)
9 特定の情報を表示する方法(-O)
9.1 指定可能なオプション一覧
Available --list columns:
NAME loop device name
AUTOCLEAR autoclear flag set
BACK-FILE device backing file
BACK-INO backing file inode number
BACK-MAJ:MIN backing file major:minor device number
MAJ:MIN loop device major:minor number
OFFSET offset from the beginning
PARTSCAN partscan flag set
RO read-only device
SIZELIMIT size limit of the file in bytes
9.2 ループバックデバイスとして使用しているファイルのinode番号の表示方法(BACK-INO)
[root@centos74 ~]# losetup -l -O NAME,BACK-INO
NAME BACK-INO
/dev/loop0 35477848
/dev/loop2 35477849
disk1.imgファイルのinode番号は35477848であることがわかる。
[root@centos74 ~]# ls -i disk1.img
35477848 disk1.img
disk2.imgファイルのinode番号は35477849であることがわかる。
[root@centos74 ~]# ls -i disk2.img
35477849 disk2.img
9.3 ループバックデバイスのメジャー番号、マイナー番号の表示方法(MAJ:MIN)
[root@centos74 ~]# losetup -l -O NAME,MAJ:MIN
NAME MAJ:MIN
/dev/loop0 7:0
/dev/loop2 7:2
10 ループバックデバイスのサイズを変更する方法(--set-capacity)
ループバックデバイスに使うファイルサイズを確認する。1Gであることがわかる。
[root@centos74 ~]# ls -l disk1.img
-rw-r--r--. 1 root root 1073741824 4月 16 20:39 disk1.img
利用可能なループバックデバイスを調べる。/dev/loop0が使えることがわかる。
[root@centos74 ~]# losetup -f
/dev/loop0
disk1.imgをループバックデバイスとして割り当てる。
[root@centos74 ~]# losetup /dev/loop0 /root/disk1.img
ループバックデバイス一覧を表示する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
ループバックデバイスのサイズを確認する。1Gであることがわかる。
[root@centos74 ~]# blockdev --getsize64 /dev/loop0
1073741824
ループバックデバイスに使うファイルサイズを2Gに変更する。
[root@centos74 ~]# truncate -s 2147483648 disk1.img
ループバックデバイスのサイズを確認する。1Gであることがわかる。
[root@centos74 ~]# blockdev --getsize64 /dev/loop0
1073741824
ループバックデバイスのサイズを2Gに変更する。
[root@centos74 ~]# losetup --set-capacity /dev/loop0
ループバックデバイスのサイズを確認する。2Gに変更されたことがわかる。
[root@centos74 ~]# blockdev --getsize64 /dev/loop0
2147483648
11 オフセットを指定する方法(-o)
ループバックデバイスに使用するファイルを確認する。
[root@centos74 ~]# ls -l disk1.img
-rw-r--r--. 1 root root 1073741824 4月 16 22:14 disk1.img
ファイルをループバックデバイスに割り当てる。オフセットは512*1024(524288)を指定する。
[root@centos74 ~]# losetup -o $((512*1024)) -f /root/disk1.img
[root@centos74 ~]# echo $((512*1024))
524288
ループバックデバイスを確認する。
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 524288 0 0 /root/disk1.img
12 その他
ループバックデバイスをマウントする手順を示します。
ループバックデバイスとして使用するファイルの作成
[root@centos74 ~]# fallocate -l 1G disk1.img
[root@centos74 ~]# fallocate -l 1G disk2.img
[root@centos74 ~]# ls -l disk*
-rw-r--r--. 1 root root 1073741824 4月 16 21:34 disk1.img
-rw-r--r--. 1 root root 1073741824 4月 16 21:35 disk2.img
ファイルをループバックデバイスに割り当てる
[root@centos74 ~]# losetup -f /root/disk1.img
[root@centos74 ~]# losetup -f /root/disk2.img
[root@centos74 ~]# losetup -l
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 0 0 0 /root/disk1.img
/dev/loop1 0 0 0 0 /root/disk2.img
ファイルシステムの作成
[root@centos74 ~]# mkfs.xfs /dev/loop0
[root@centos74 ~]# mkfs.btrfs /dev/loop1
マウント
[root@centos74 ~]# mount -t xfs /dev/loop0 /mnt0/
[root@centos74 ~]# mount -t btrfs /dev/loop1 /mnt1/
[root@centos74 ~]# df -hT
ファイルシス タイプ サイズ 使用 残り 使用% マウント位置
/dev/sda3 xfs 18G 3.6G 15G 20% /
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 8.7M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 119M 896M 12% /boot
tmpfs tmpfs 394M 0 394M 0% /run/user/0
/dev/loop0 xfs 1014M 33M 982M 4% /mnt0
/dev/loop1 btrfs 1.0G 17M 905M 2% /mnt1
X 参考情報
Linuxでrawディスクのimageファイルを扱うあれこれ(空ファイル作成、パーティション作成、マウントなど)
Why is my loop device not 600G in size?
【truncate】コマンド――ファイルを指定したサイズに変える
Arch Linux ARMのディスクイメージを作成する(losetup、kpartx)
How to use loop devices
How to format a partition inside of an img file?