losetup - set up and control loop devices

losetup loop_device
losetup -a
losetup -d loop_device
losetup -f Print name of first unused loop device:
losetup [{-e|-E} encryption] [-o offset] [-p passfd] [-r] {-f|loop_device} file
losetup is used to associate loop devices with regular files or block devices, to detach loop devices and to query the status of a loop device.

a loop device, vnd (vnode disk), or lofi (loopback file interface) is a pseudo-device that makes a file accessible as a block device.

Before use, a loop device must be connected to an existing file in the filesystem that allows the file to be used in place of a block special file (cf. device file system).

For example if the file contains an entire file system, the file may then be mounted as if it were a disk device.

Used for CD ISO and floppy disc images. Mounting a file containing a filesystem via such a loop mount makes the files within that filesystem accessible. They appear in the mount point directory.

Encryption

It is possible to specify transfer functions (for encryption/decryption or other purposes) using of -E or -e.
There are two mechanisms to specify the desired encryption: by number and by name.
If an encryption is specified by number then make sure that the Linux kernel knows about the encryption with that number, probably by patching the kernel.
Standard numbers that are always present are 0 (no encryption) and 1 (XOR encryption).
When the cryptoloop module is loaded (or compiled in), it uses number 18. wil take the name of an arbitrary encryption type and finds the module that performs that encryption. (use a number different from 18 with -E , or a name with -e .)
-a Show status of all loop devices.
losetup -a
/dev/loop0: [0011]:8839 (/boot/sysrcd.dat)
-d Detach the file or device associated with the specified loop device.
-E encryption_num Enable data encryption with specified number.
-e encryption_name Enable data encryption with specified name.
-f Find the first unused loop device. If a file argument is present, use this device. Otherwise, print its name.
losetup -f
/dev/loop1
-o offset The data start is moved offset bytes into the specified file or device.
-p num Read the passphrase from file descriptor with number num instead of from the terminal.
-r Setup read-only loop device.

Return Value

0 on success, nonzero on failure.
when displaying status it returns
1 if the device is not configured and
2 if an error occurred which prevented losetup from determining the status of the device.
usage:
  losetup [options] loop_device file        # setup
  losetup -F [options] loop_device [file]   # setup, read /etc/fstab
  losetup loop_device                       # give info
  losetup -a                                # give info of all loops
  losetup -f                                # show next free loop device
  losetup -d loop_device                    # delete
  losetup -R loop_device                    # resize
options:  -e encryption  -o offset  -s sizelimit  -p passwdfd  -T  -S pseed
          -H phash  -I loinit  -K gpgkey  -G gpghome  -C itercountk  -v  -r
          -P cleartextkey

FILES

/dev/loop0, /dev/loop1, ... loop devices (major=7)

EXAMPLE

For the loadable module, install it with:

insmod loop.o

encryption modules may be needed.

insmod des.o
insmod cryptoloop.o

Using the loop device:


               dd if=/dev/zero of=/STUFF bs=1k count=100 # make a 100k file of zeros
               losetup -e des /dev/loop0 /STUFF           # encrypt it (?)
              Password:
              Init (up to 16 hex digits):
               mkfs  -t ext2 /dev/loop0 100               # make an ext2 file system there
               mount -t ext2 /dev/loop0 /mnt              # mount it so it can be used for  ???
               ...
               umount /dev/loop0                          # unmount it since we are done with it
               losetup -d /dev/loop0                      # detach loop0 from it

loadable module may be remove with
rmmod loop