Device-mapper

Normally, users rarely use dmsetup directly. The dmsetup is a very low level, and difficult tool to use. LVM, mdtool or dmsetup is generally the preferred way to do it, as it takes care of saving the metadata and issuing the dmsetup commands for you. However, sometimes one want to deal with it directly: sometimes for recovery purposes, or because LVM doesn't yet support what you want.

Create
The create command activates a new device mapper device. It appears in /dev/mapper. In addition, if the target has metadata, it reads it, or if this its first use, it initializes the metadata devices. Note the prior device mapper devices can be passed as paramters (if the target takes a device), thus it is possible to "stack" them. The syntax is:

Remove
The remove command deactivates a device mapper device. It removes it from /dev/mapper. Syntax is Note is not possible to remove a device that's in use. The -f option may be passed the replace the target with one that fails all I/O, hopefully allowing the reference count to drop to 0.

Message
The message command send a message to the device. What message are supported depend on the target Syntax is: The tends not to be used and is almost always 0.

Suspend
The suspend' command stops any NEW I/O. Existing I/O will still be completed. This can be used to quiesce a device. Syntax is:

Resume
The resume command allows I/O to be submitted to a previously suspended device. Syntax is:

Zero
See Documentation/device-mapper/zero.txt. This device has no device-specific parameters.

The "zero" target create that functions similarly to /dev/zero: All reads return binary zero, and all writes are discarded> Normaly used in tests, but also useful in recovering linear and raid-type targets, when combined with the 'snapshot' target: a "zero" target of the same size as the missing piece(s) is created, a (writable) snapshot created (usually a loop device backed by a large sparse file, but it can be far smaller than the missing piece since it only has to the hold the changes). Then the snapshot can be mounted, fsck'd, or recover tools run against it.

This creates a 1GB (1953125-sector) zero target:

Mirror and Raid1
See Documentation/device-mapper/dm-raid.txt. Note that  is unused for RAID1, but a value is still required, therefore is value should be set to 0.

If /dev/loop1 and /dev/loop2 are each 1GB (1953125 sectors), to create a simple 1 GB raid1 with no metadata:

Note that because there's no metadata device, the array but be re-mirrored each time it is created. So normally, a metadata device is desired. Each "leg" needs it own metadata device (TODO: How does one compute how big?}}. If /dev/loop2 and /dev/loop3 are small metadata devices (4 MiB), then to create a 1G RAID1 would be:

Striped and Raid 4/5/6/10
See Documentation/device-mapper/striped.txt and Documentation/device-mapper/dm-raid.txt.

Note that the length (in sectors) MUST be a multiple of the *<# of non-parity disks>, otherwise the target will give the error "Array size does not match requested target length". The must also be both a power a two and a least a large as a memory page (for x86/x64 processors, pages are 4 KiB, so must be at least 8). So for 3 1GB (1953125-sector) devices, with a 32KiB (64 sector) chunk size, 1953125 must be rounded down to the nearest multiple for 64, which can be done according the following template (chunk_size and dev_size in sectors) echo 'scale=0; x= / ; x*=; x*<# of non-parity disks> ' | bc -l So for RAID 5,3 disk (in Raid 5, one disk is lost for parity), 1953125-sector devices, and a 64-sector chunk size: