The Device Mapper

mapping devices to devices

© 2010, 2011, 2012 Dennis Leeuw dleeuw at made-it dot com
License: GPLv2 or later

Index

    1. Introduction
    2. A linear map
    3. Making snapshots

Introduction

The device mapper is a lowlevel library with a kernel counter part. In a graphical overview it would look like this:
User Spacedmsetup
libdevmapper
KernelDevice Mapper

Before we can play around with the device mapper we need to make sure it is loaded. Check to see if /dev/mapper/control is present. If so the kernel supports de device mapper if it is not present try loading the dm_mod kernel module:

modprobe dm_mod

Within this section we assume two loop back devices called /dev/loop1 and /dev/loop2. Make sure they are 2MB of size or bigger for testing purposes.

The Device Mapper creates a little bit of overhead, meaning if you do not need to use the device mapper, do not use it. Only when you want to do some fancy stuff like we are going to do in this chapter, use the device mapper. A little example of a useless use case:

echo "0 $(blockdev --getsize /dev/loop1) linear /dev/loop1 0" | \
	dmsetup create new_name
This creates a device /dev/mapper/new_name, while the exact same device is already perfectly accessable through /dev/loop1. So this is what is called useless.

Now onto some useful use cases..., correction let me first explain what we did in the above command. By using echo we sent a line into dmsetup which we told to create a device with the name new_name. The problem will probably not be with the understanding of the dmsetup command, but with the table line that we sent into dmsetup. The line consists of a couple of fields which are:

  1. Start sector of the map: This is the sector offset where the sectors of this line will be added to, for the first line of a table this is always 0
  2. Number of sectors in the map: number of sectors that will be added to the device in this map
  3. Type of map

As you can see there are two types of maps to be created with dmsetup. There is the linear and the striped type. Each will be discussed in its own section.

A linear map

The linear setting is meant to indicate a "normal" drive which is read and written in the standard way.

A linear map is defined as: start length linear device offset

The simplest form is creating a single mapper device from two devices. Before we can do that we will create a file (a table) that will hold everything that we will feed to dmsetup:

echo 0 $(blockdev --getsize /dev/loop1) linear /dev/loop1 0 > /tmp/onedisk
SIZE1=$(blockdev --getsize /dev/loop1)
SIZE2=$(blockdev --getsize /dev/loop2)
echo ${SIZE1} ${SIZE2} linear /dev/loop2 0 >> /tmp/onedisk
dmsetup create twoasone /tmp/onedisk
to check that the new device is the sum of both devices do:
blockdev --getsize /dev/mapper/twoasone

The removal of the mapping is done by issueing:

dmsetup remove twoasone

A more funny thing to do is to create device that covers half a disk and one that covers one and a half disk. To accomplish this use (we assume the previously used variables SIZE1 and SIZE2 are still available):

echo 0 $((${SIZE1}/2)) linear /dev/loop1 0 > /tmp/halfdisk
echo 0 $((${SIZE1}-${SIZE1}/2)) linear /dev/loop1 $((${SIZE1}/2)) > /tmp/oneahalf
echo $((${SIZE1}-${SIZE1}/2 )) ${SIZE2} linear /dev/loop2 0 >> /tmp/oneahalf
dmsetup create halfdisk /tmp/halfdisk
dmsetup create oneahalf /tmp/oneahalf

dmsetup remove can be used to remove the maps.

Making snapshots

A snapshot-origin map is defined as: start length snapshot-origin origin

A snapshot-target map is defined as: start length snapshot-target origin COW-device P|N chunksize