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 2019-03-06 "Linux" "Linux Programmer's Manual"
28 loop, loop-control \- loop devices
30 #include <linux/loop.h>
32 The loop device is a block device that maps its data blocks not to a
33 physical device such as a hard disk or optical disk drive,
35 a regular file in a filesystem or to another block device.
36 This can be useful for example to provide a block device for a filesystem
37 image stored in a file, so that it can be mounted with the
44 $ \fBdd if=/dev/zero of=file.img bs=1MiB count=10\fP
45 $ \fBsudo losetup /dev/loop4 file.img \fP
46 $ \fBsudo mkfs -t ext4 /dev/loop4\fP
47 $ \fBsudo mkdir /myloopdev\fP
48 $ \fBsudo mount /dev/loop4 /myloopdev\fP
56 A transfer function can be specified for each loop device for
57 encryption and decryption purposes.
61 operations are provided by the loop block device:
64 Associate the loop device with the open file whose file descriptor is
70 Disassociate the loop device from any file descriptor.
73 Set the status of the loop device using the (third)
76 This argument is a pointer to
85 int lo_number; /* ioctl r/o */
86 dev_t lo_device; /* ioctl r/o */
87 unsigned long lo_inode; /* ioctl r/o */
88 dev_t lo_rdevice; /* ioctl r/o */
91 int lo_encrypt_key_size; /* ioctl w/o */
92 int lo_flags; /* ioctl r/o */
93 char lo_name[LO_NAME_SIZE];
94 unsigned char lo_encrypt_key[LO_KEY_SIZE];
96 unsigned long lo_init[2];
103 .RI ( lo_encrypt_type )
110 .BR LO_CRYPT_CAST128 ,
113 .BR LO_CRYPT_SKIPJACK ,
114 or (since Linux 2.6.0)
115 .BR LO_CRYPT_CRYPTOAPI .
119 field is a bit mask that can include zero or more of the following:
122 .BR LO_FLAGS_READ_ONLY
123 The loopback device is read-only.
125 .BR LO_FLAGS_AUTOCLEAR " (since Linux 2.6.25)"
126 .\" commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
127 The loopback device will autodestruct on last close.
129 .BR LO_FLAGS_PARTSCAN " (since Linux 3.2)"
130 .\" commit e03c8dd14915fabc101aa495828d58598dc5af98
131 Allow automatic partition scanning.
135 Get the status of the loop device.
138 argument must be a pointer to a
139 .IR "struct loop_info" .
141 .BR LOOP_CHANGE_FD " (since Linux 2.6.5)"
142 Switch the backing store of the loop device to the new file identified
143 file descriptor specified in the (third)
145 argument, which is an integer.
146 This operation is possible only if the loop device is read-only and
147 the new backing store is the same size and type as the old backing store.
149 .BR LOOP_SET_CAPACITY " (since Linux 2.6.30)"
150 .\" commit 53d6660836f233df66490707365ab177e5fb2bb4
151 Resize a live loop device.
152 One can change the size of the underlying backing store and then use this
153 operation so that the loop driver learns about the new size.
154 This operation takes no argument.
156 Since Linux 2.6, there are two new
160 .BR LOOP_SET_STATUS64 ", " LOOP_GET_STATUS64
162 .BR LOOP_SET_STATUS " and " LOOP_GET_STATUS
163 described above but use the
166 which has some additional fields and a larger range for some other fields:
171 uint64_t lo_device; /* ioctl r/o */
172 uint64_t lo_inode; /* ioctl r/o */
173 uint64_t lo_rdevice; /* ioctl r/o */
175 uint64_t lo_sizelimit;/* bytes, 0 == max available */
176 uint32_t lo_number; /* ioctl r/o */
177 uint32_t lo_encrypt_type;
178 uint32_t lo_encrypt_key_size; /* ioctl w/o */
179 uint32_t lo_flags; /* ioctl r/o */
180 uint8_t lo_file_name[LO_NAME_SIZE];
181 uint8_t lo_crypt_name[LO_NAME_SIZE];
182 uint8_t lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
187 .SS /dev/loop-control
189 .\" commit 770fe30a46a12b6fb6b63fbe1737654d28e84844
190 the kernel provides the
192 device, which permits an application to dynamically find a free device,
193 and to add and remove loop devices from the system.
194 To perform these operations, one first opens
195 .IR /dev/loop-control
196 and then employs one of the following
201 Allocate or find a free loop device for use.
202 On success, the device number is returned as the result of the call.
203 This operation takes no argument.
206 Add the new loop device whose device number is specified
207 as a long integer in the third
210 On success, the device index is returned as the result of the call.
211 If the device is already allocated, the call fails with the error
215 Remove the loop device whose device number is specified
216 as a long integer in the third
219 On success, the device number is returned as the result of the call.
220 If the device is in use, the call fails with the error
225 The loop block special device files.
227 The program below uses the
229 device to find a free loop device, opens the loop device,
230 opens a file to be used as the underlying storage for the device,
231 and then associates the loop device with the backing store.
232 The following shell session demonstrates the use of the program:
236 $ \fBdd if=/dev/zero of=file.img bs=1MiB count=10\fP
239 10485760 bytes (10 MB) copied, 0.00609385 s, 1.7 GB/s
240 $ \fBsudo ./mnt_loop file.img\fP
241 loopname = /dev/loop5
248 #include <linux/loop.h>
249 #include <sys/ioctl.h>
254 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
258 main(int argc, char *argv[])
260 int loopctlfd, loopfd, backingfile;
265 fprintf(stderr, "Usage: %s backing\-file\en", argv[0]);
269 loopctlfd = open("/dev/loop\-control", O_RDWR);
270 if (loopctlfd == \-1)
271 errExit("open: /dev/loop\-control");
273 devnr = ioctl(loopctlfd, LOOP_CTL_GET_FREE);
275 errExit("ioctl\-LOOP_CTL_GET_FREE");
277 sprintf(loopname, "/dev/loop%ld", devnr);
278 printf("loopname = %s\en", loopname);
280 loopfd = open(loopname, O_RDWR);
282 errExit("open: loopname");
284 backingfile = open(argv[1], O_RDWR);
285 if (backingfile == \-1)
286 errExit("open: backing\-file");
288 if (ioctl(loopfd, LOOP_SET_FD, backingfile) == \-1)
289 errExit("ioctl\-LOOP_SET_FD");