Blame man4/loop.4

Packit 7cfc04
.\" Copyright 2002 Urs Thuermann (urs@isnogud.escape.de)
Packit 7cfc04
.\" and Copyright 2015 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\"
Packit 7cfc04
.\" The GNU General Public License's references to "object code"
Packit 7cfc04
.\" and "executables" are to be interpreted as the output of any
Packit 7cfc04
.\" document formatting or typesetting system, including
Packit 7cfc04
.\" intermediate and printed output.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This manual is distributed in the hope that it will be useful,
Packit 7cfc04
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7cfc04
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7cfc04
.\" GNU General Public License for more details.
Packit 7cfc04
.\"
Packit 7cfc04
.\" You should have received a copy of the GNU General Public
Packit 7cfc04
.\" License along with this manual; if not, write to the Free
Packit 7cfc04
.\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
Packit 7cfc04
.\" USA.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH LOOP 4 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
loop, loop-control \- loop devices
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
#include <linux/loop.h>
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The loop device is a block device that maps its data blocks not to a
Packit 7cfc04
physical device such as a hard disk or optical disk drive,
Packit 7cfc04
but to the blocks of
Packit 7cfc04
a regular file in a filesystem or to another block device.
Packit 7cfc04
This can be useful for example to provide a block device for a filesystem
Packit 7cfc04
image stored in a file, so that it can be mounted with the
Packit 7cfc04
.BR mount (8)
Packit 7cfc04
command.
Packit 7cfc04
You could do
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fBdd if=/dev/zero of=file.img bs=1MiB count=10\fP
Packit 7cfc04
$ \fBsudo losetup /dev/loop4 file.img \fP
Packit 7cfc04
$ \fBsudo mkfs -t ext4 /dev/loop4\fP
Packit 7cfc04
$ \fBsudo mkdir /myloopdev\fP
Packit 7cfc04
$ \fBsudo mount /dev/loop4 /myloopdev\fP
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
See
Packit 7cfc04
.BR losetup (8)
Packit 7cfc04
for another example.
Packit 7cfc04
.PP
Packit 7cfc04
A transfer function can be specified for each loop device for
Packit 7cfc04
encryption and decryption purposes.
Packit 7cfc04
.PP
Packit 7cfc04
The following
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
operations are provided by the loop block device:
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_SET_FD
Packit 7cfc04
Associate the loop device with the open file whose file descriptor is
Packit 7cfc04
passed as the (third)
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_CLR_FD
Packit 7cfc04
Disassociate the loop device from any file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_SET_STATUS
Packit 7cfc04
Set the status of the loop device using the (third)
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
This argument is a pointer to
Packit 7cfc04
.I loop_info
Packit 7cfc04
structure, defined in
Packit 7cfc04
.I <linux/loop.h>
Packit 7cfc04
as:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct loop_info {
Packit 7cfc04
    int           lo_number;            /* ioctl r/o */
Packit 7cfc04
    dev_t         lo_device;            /* ioctl r/o */
Packit 7cfc04
    unsigned long lo_inode;             /* ioctl r/o */
Packit 7cfc04
    dev_t         lo_rdevice;           /* ioctl r/o */
Packit 7cfc04
    int           lo_offset;
Packit 7cfc04
    int           lo_encrypt_type;
Packit 7cfc04
    int           lo_encrypt_key_size;  /* ioctl w/o */
Packit 7cfc04
    int           lo_flags;             /* ioctl r/o */
Packit 7cfc04
    char          lo_name[LO_NAME_SIZE];
Packit 7cfc04
    unsigned char lo_encrypt_key[LO_KEY_SIZE];
Packit 7cfc04
                                        /* ioctl w/o */
Packit 7cfc04
    unsigned long lo_init[2];
Packit 7cfc04
    char          reserved[4];
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The encryption type
Packit 7cfc04
.RI ( lo_encrypt_type )
Packit 7cfc04
should be one of
Packit 7cfc04
.BR LO_CRYPT_NONE ,
Packit 7cfc04
.BR LO_CRYPT_XOR ,
Packit 7cfc04
.BR LO_CRYPT_DES ,
Packit 7cfc04
.BR LO_CRYPT_FISH2 ,
Packit 7cfc04
.BR LO_CRYPT_BLOW ,
Packit 7cfc04
.BR LO_CRYPT_CAST128 ,
Packit 7cfc04
.BR LO_CRYPT_IDEA ,
Packit 7cfc04
.BR LO_CRYPT_DUMMY ,
Packit 7cfc04
.BR LO_CRYPT_SKIPJACK ,
Packit 7cfc04
or (since Linux 2.6.0)
Packit 7cfc04
.BR LO_CRYPT_CRYPTOAPI .
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I lo_flags
Packit 7cfc04
field is a bit mask that can include zero or more of the following:
Packit 7cfc04
.RS
Packit 7cfc04
.TP
Packit 7cfc04
.BR LO_FLAGS_READ_ONLY
Packit 7cfc04
The loopback device is read-only.
Packit 7cfc04
.TP
Packit 7cfc04
.BR LO_FLAGS_AUTOCLEAR " (since Linux 2.6.25)"
Packit 7cfc04
.\" commit 96c5865559cee0f9cbc5173f3c949f6ce3525581
Packit 7cfc04
The loopback device will autodestruct on last close.
Packit 7cfc04
.TP
Packit 7cfc04
.BR LO_FLAGS_PARTSCAN " (since Linux 3.2)"
Packit 7cfc04
.\" commit e03c8dd14915fabc101aa495828d58598dc5af98
Packit 7cfc04
Allow automatic partition scanning.
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_GET_STATUS
Packit 7cfc04
Get the status of the loop device.
Packit 7cfc04
The (third)
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument must be a pointer to a
Packit 7cfc04
.IR "struct loop_info" .
Packit 7cfc04
.TP
Packit 7cfc04
.BR LOOP_CHANGE_FD " (since Linux 2.6.5)"
Packit 7cfc04
Switch the backing store of the loop device to the new file identified
Packit 7cfc04
file descriptor specified in the (third)
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument, which is an integer.
Packit 7cfc04
This operation is possible only if the loop device is read-only and
Packit 7cfc04
the new backing store is the same size and type as the old backing store.
Packit 7cfc04
.TP
Packit 7cfc04
.BR LOOP_SET_CAPACITY " (since Linux 2.6.30)"
Packit 7cfc04
.\" commit 53d6660836f233df66490707365ab177e5fb2bb4
Packit 7cfc04
Resize a live loop device.
Packit 7cfc04
One can change the size of the underlying backing store and then use this
Packit 7cfc04
operation so that the loop driver learns about the new size.
Packit 7cfc04
This operation takes no argument.
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 2.6, there are two new
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
operations:
Packit 7cfc04
.TP
Packit 7cfc04
.BR LOOP_SET_STATUS64 ", " LOOP_GET_STATUS64
Packit 7cfc04
These are similar to
Packit 7cfc04
.BR LOOP_SET_STATUS " and " LOOP_GET_STATUS
Packit 7cfc04
described above but use the
Packit 7cfc04
.I loop_info64
Packit 7cfc04
structure,
Packit 7cfc04
which has some additional fields and a larger range for some other fields:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct loop_info64 {
Packit 7cfc04
    uint64_t lo_device;                   /* ioctl r/o */
Packit 7cfc04
    uint64_t lo_inode;                    /* ioctl r/o */
Packit 7cfc04
    uint64_t lo_rdevice;                  /* ioctl r/o */
Packit 7cfc04
    uint64_t lo_offset;
Packit 7cfc04
    uint64_t lo_sizelimit;/* bytes, 0 == max available */
Packit 7cfc04
    uint32_t lo_number;                   /* ioctl r/o */
Packit 7cfc04
    uint32_t lo_encrypt_type;
Packit 7cfc04
    uint32_t lo_encrypt_key_size;         /* ioctl w/o */
Packit 7cfc04
    uint32_t lo_flags;                    /* ioctl r/o */
Packit 7cfc04
    uint8_t  lo_file_name[LO_NAME_SIZE];
Packit 7cfc04
    uint8_t  lo_crypt_name[LO_NAME_SIZE];
Packit 7cfc04
    uint8_t  lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
Packit 7cfc04
    uint64_t lo_init[2];
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS /dev/loop-control
Packit 7cfc04
Since Linux 3.1,
Packit 7cfc04
.\" commit 770fe30a46a12b6fb6b63fbe1737654d28e84844
Packit 7cfc04
the kernel provides the
Packit 7cfc04
.I /dev/loop-control
Packit 7cfc04
device, which permits an application to dynamically find a free device,
Packit 7cfc04
and to add and remove loop devices from the system.
Packit 7cfc04
To perform these operations, one first opens
Packit 7cfc04
.IR /dev/loop-control
Packit 7cfc04
and then employs one of the following
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
operations:
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_CTL_GET_FREE
Packit 7cfc04
Allocate or find a free loop device for use.
Packit 7cfc04
On success, the device number is returned as the result of the call.
Packit 7cfc04
This operation takes no argument.
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_CTL_ADD
Packit 7cfc04
Add the new loop device whose device number is specified
Packit 7cfc04
as a long integer in the third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
On success, the device index is returned as the result of the call.
Packit 7cfc04
If the device is already allocated, the call fails with the error
Packit 7cfc04
.BR EEXIST .
Packit 7cfc04
.TP
Packit 7cfc04
.B LOOP_CTL_REMOVE
Packit 7cfc04
Remove the loop device whose device number is specified
Packit 7cfc04
as a long integer in the third
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
argument.
Packit 7cfc04
On success, the device number is returned as the result of the call.
Packit 7cfc04
If the device is in use, the call fails with the error
Packit 7cfc04
.BR EBUSY .
Packit 7cfc04
.SH FILES
Packit 7cfc04
.TP
Packit 7cfc04
.IR /dev/loop*
Packit 7cfc04
The loop block special device files.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The program below uses the
Packit 7cfc04
.I /dev/loop-control
Packit 7cfc04
device to find a free loop device, opens the loop device,
Packit 7cfc04
opens a file to be used as the underlying storage for the device,
Packit 7cfc04
and then associates the loop device with the backing store.
Packit 7cfc04
The following shell session demonstrates the use of the program:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fBdd if=/dev/zero of=file.img bs=1MiB count=10\fP
Packit 7cfc04
10+0 records in
Packit 7cfc04
10+0 records out
Packit 7cfc04
10485760 bytes (10 MB) copied, 0.00609385 s, 1.7 GB/s
Packit 7cfc04
$ \fBsudo ./mnt_loop file.img\fP
Packit 7cfc04
loopname = /dev/loop5
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#include <fcntl.h>
Packit 7cfc04
#include <linux/loop.h>
Packit 7cfc04
#include <sys/ioctl.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
Packit 7cfc04
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \\
Packit 7cfc04
                        } while (0)
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    int loopctlfd, loopfd, backingfile;
Packit 7cfc04
    long devnr;
Packit 7cfc04
    char loopname[4096];
Packit 7cfc04
Packit 7cfc04
    if (argc != 2) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s backing\-file\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    loopctlfd = open("/dev/loop\-control", O_RDWR);
Packit 7cfc04
    if (loopctlfd == \-1)
Packit 7cfc04
        errExit("open: /dev/loop\-control");
Packit 7cfc04
Packit 7cfc04
    devnr = ioctl(loopctlfd, LOOP_CTL_GET_FREE);
Packit 7cfc04
    if (devnr == \-1)
Packit 7cfc04
        errExit("ioctl\-LOOP_CTL_GET_FREE");
Packit 7cfc04
Packit 7cfc04
    sprintf(loopname, "/dev/loop%ld", devnr);
Packit 7cfc04
    printf("loopname = %s\\n", loopname);
Packit 7cfc04
Packit 7cfc04
    loopfd = open(loopname, O_RDWR);
Packit 7cfc04
    if (loopfd == \-1)
Packit 7cfc04
        errExit("open: loopname");
Packit 7cfc04
Packit 7cfc04
    backingfile = open(argv[1], O_RDWR);
Packit 7cfc04
    if (backingfile == \-1)
Packit 7cfc04
        errExit("open: backing\-file");
Packit 7cfc04
Packit 7cfc04
    if (ioctl(loopfd, LOOP_SET_FD, backingfile) == \-1)
Packit 7cfc04
        errExit("ioctl\-LOOP_SET_FD");
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR losetup (8),
Packit 7cfc04
.BR mount (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.