1 .\" Copyright 2002 Urs Thuermann (urs@isnogud.escape.de)
2 .\" and Copyright 2015 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, write to the Free
22 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
26 .TH LOOP 4 2021-03-22 "Linux" "Linux Programmer's Manual"
28 loop, loop-control \- loop devices
31 #include <linux/loop.h>
34 The loop device is a block device that maps its data blocks not to a
35 physical device such as a hard disk or optical disk drive,
37 a regular file in a filesystem or to another block device.
38 This can be useful for example to provide a block device for a filesystem
39 image stored in a file, so that it can be mounted with the
46 $ \fBdd if=/dev/zero of=file.img bs=1MiB count=10\fP
47 $ \fBsudo losetup /dev/loop4 file.img\fP
48 $ \fBsudo mkfs \-t ext4 /dev/loop4\fP
49 $ \fBsudo mkdir /myloopdev\fP
50 $ \fBsudo mount /dev/loop4 /myloopdev\fP
58 A transfer function can be specified for each loop device for
59 encryption and decryption purposes.
63 operations are provided by the loop block device:
66 Associate the loop device with the open file whose file descriptor is
72 Disassociate the loop device from any file descriptor.
75 Set the status of the loop device using the (third)
78 This argument is a pointer to a
87 int lo_number; /* ioctl r/o */
88 dev_t lo_device; /* ioctl r/o */
89 unsigned long lo_inode; /* ioctl r/o */
90 dev_t lo_rdevice; /* ioctl r/o */
93 int lo_encrypt_key_size; /* ioctl w/o */
94 int lo_flags; /* ioctl r/w (r/o before
96 char lo_name[LO_NAME_SIZE];
97 unsigned char lo_encrypt_key[LO_KEY_SIZE];
99 unsigned long lo_init[2];
106 .RI ( lo_encrypt_type )
113 .BR LO_CRYPT_CAST128 ,
116 .BR LO_CRYPT_SKIPJACK ,
117 or (since Linux 2.6.0)
118 .BR LO_CRYPT_CRYPTOAPI .
122 field is a bit mask that can include zero or more of the following:
125 .BR LO_FLAGS_READ_ONLY
126 The loopback device is read-only.
128 .BR LO_FLAGS_AUTOCLEAR " (since Linux 2.6.25)"
129 .\" commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
130 The loopback device will autodestruct on last close.
132 .BR LO_FLAGS_PARTSCAN " (since Linux 3.2)"
133 .\" commit e03c8dd14915fabc101aa495828d58598dc5af98
134 Allow automatic partition scanning.
136 .BR LO_FLAGS_DIRECT_IO " (since Linux 4.10)"
137 .\" commit 2e5ab5f379f96a6207c45be40c357ebb1beb8ef3
138 Use direct I/O mode to access the backing file.
143 that can be modified by
146 .BR LO_FLAGS_AUTOCLEAR
148 .BR LO_FLAGS_PARTSCAN .
151 Get the status of the loop device.
154 argument must be a pointer to a
155 .IR "struct loop_info" .
157 .BR LOOP_CHANGE_FD " (since Linux 2.6.5)"
158 Switch the backing store of the loop device to the new file identified
159 file descriptor specified in the (third)
161 argument, which is an integer.
162 This operation is possible only if the loop device is read-only and
163 the new backing store is the same size and type as the old backing store.
165 .BR LOOP_SET_CAPACITY " (since Linux 2.6.30)"
166 .\" commit 53d6660836f233df66490707365ab177e5fb2bb4
167 Resize a live loop device.
168 One can change the size of the underlying backing store and then use this
169 operation so that the loop driver learns about the new size.
170 This operation takes no argument.
172 .BR LOOP_SET_DIRECT_IO " (since Linux 4.10)"
173 .\" commit ab1cb278bc7027663adbfb0b81404f8398437e11
174 Set DIRECT I/O mode on the loop device, so that
175 it can be used to open backing file.
178 argument is an unsigned long value.
179 A nonzero represents direct I/O mode.
181 .BR LOOP_SET_BLOCK_SIZE " (since Linux 4.14)"
182 .\" commit 89e4fdecb51cf5535867026274bc97de9480ade5
183 Set the block size of the loop device.
186 argument is an unsigned long value.
187 This value must be a power of two in the range
193 .BR LOOP_CONFIGURE " (since Linux 5.8)"
194 .\" commit 3448914e8cc550ba792d4ccc74471d1ca4293aae
195 Setup and configure all loop device parameters in a single step using
199 This argument is a pointer to a
201 structure, defined in
210 struct loop_info64 info;
216 In addition to doing what
220 can also be used to do the following:
223 set the correct block size immediately by setting
224 .IR loop_config.block_size ;
226 explicitly request direct I/O mode by setting
227 .BR LO_FLAGS_DIRECT_IO
229 .IR loop_config.info.lo_flags ;
232 explicitly request read-only mode by setting
233 .BR LO_FLAGS_READ_ONLY
235 .IR loop_config.info.lo_flags .
238 Since Linux 2.6, there are two new
242 .BR LOOP_SET_STATUS64 ", " LOOP_GET_STATUS64
244 .BR LOOP_SET_STATUS " and " LOOP_GET_STATUS
245 described above but use the
248 which has some additional fields and a larger range for some other fields:
253 uint64_t lo_device; /* ioctl r/o */
254 uint64_t lo_inode; /* ioctl r/o */
255 uint64_t lo_rdevice; /* ioctl r/o */
257 uint64_t lo_sizelimit; /* bytes, 0 == max available */
258 uint32_t lo_number; /* ioctl r/o */
259 uint32_t lo_encrypt_type;
260 uint32_t lo_encrypt_key_size; /* ioctl w/o */
261 uint32_t lo_flags; i /* ioctl r/w (r/o before
263 uint8_t lo_file_name[LO_NAME_SIZE];
264 uint8_t lo_crypt_name[LO_NAME_SIZE];
265 uint8_t lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
270 .SS /dev/loop-control
272 .\" commit 770fe30a46a12b6fb6b63fbe1737654d28e84844
273 the kernel provides the
274 .I /dev/loop\-control
275 device, which permits an application to dynamically find a free device,
276 and to add and remove loop devices from the system.
277 To perform these operations, one first opens
278 .IR /dev/loop\-control
279 and then employs one of the following
284 Allocate or find a free loop device for use.
285 On success, the device number is returned as the result of the call.
286 This operation takes no argument.
289 Add the new loop device whose device number is specified
290 as a long integer in the third
293 On success, the device index is returned as the result of the call.
294 If the device is already allocated, the call fails with the error
298 Remove the loop device whose device number is specified
299 as a long integer in the third
302 On success, the device number is returned as the result of the call.
303 If the device is in use, the call fails with the error
308 The loop block special device files.
310 The program below uses the
311 .I /dev/loop\-control
312 device to find a free loop device, opens the loop device,
313 opens a file to be used as the underlying storage for the device,
314 and then associates the loop device with the backing store.
315 The following shell session demonstrates the use of the program:
319 $ \fBdd if=/dev/zero of=file.img bs=1MiB count=10\fP
322 10485760 bytes (10 MB) copied, 0.00609385 s, 1.7 GB/s
323 $ \fBsudo ./mnt_loop file.img\fP
324 loopname = /dev/loop5
331 #include <linux/loop.h>
332 #include <sys/ioctl.h>
337 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
341 main(int argc, char *argv[])
343 int loopctlfd, loopfd, backingfile;
348 fprintf(stderr, "Usage: %s backing\-file\en", argv[0]);
352 loopctlfd = open("/dev/loop\-control", O_RDWR);
353 if (loopctlfd == \-1)
354 errExit("open: /dev/loop\-control");
356 devnr = ioctl(loopctlfd, LOOP_CTL_GET_FREE);
358 errExit("ioctl\-LOOP_CTL_GET_FREE");
360 sprintf(loopname, "/dev/loop%ld", devnr);
361 printf("loopname = %s\en", loopname);
363 loopfd = open(loopname, O_RDWR);
365 errExit("open: loopname");
367 backingfile = open(argv[1], O_RDWR);
368 if (backingfile == \-1)
369 errExit("open: backing\-file");
371 if (ioctl(loopfd, LOOP_SET_FD, backingfile) == \-1)
372 errExit("ioctl\-LOOP_SET_FD");