Technique for synthesizing a partition table on a “naked” filesystem

Henning Rohde posted this technique to synthesize a partition table onto a filesystem using device mapper:

# dd if=/dev/zero of=/tmp/zero count=63
# losetup -fv /tmp/zero
# sda1size=$( blockdev --getsz /dev/sda1 )
# echo -e "unit: sectors\n\n/dev/sda1 : start=63, size=$sda1size, Id=7" |
    sfdisk --force /dev/loop0
# echo -e "0 63 linear /dev/loop0 0\n63 $sda1size linear /dev/sda1 0" |
    dmsetup -v create virtdisk
# fdisk -lu /dev/mapper/virtdisk


  1. The dd command creates a file of all zeroes which is 63 sectors long (default blocksize for dd is 512 which happens to be the same as the traditional size of a sector).
  2. losetup and sfdisk are used to create a partition table on it. The partition table is configured to have a single partition which starts at sector 63 and has the same size as the (real) /dev/sda1. Of course this just creates the partition table on our empty file. If it existed, the partition would follow the end of the file, but the partition is not really being created by these commands.
  3. Use dmsetup to create a device mapper disk which consists of two linear maps: The first is the fake partition table file. The second is the real /dev/sda1. Effectively what we have created is a device mapper disk called /dev/mapper/virtdisk where you can read sectors 0..62 from the fake p.t. file, followed by sectors 63 onwards from the real /dev/sda1. /dev/mapper/virtdisk is, therefore, a lot like a virtual block device containing a partition table and a partition.

This is all risky, requires root, could seriously corrupt your data, and AFAIK no one has tried to boot a VM yet, so as Henning said, YMMV.

Previously …


1 Comment

Filed under Uncategorized

One response to “Technique for synthesizing a partition table on a “naked” filesystem

  1. Gabriel

    I’m using a similar technique in blocks. One annoyance is that it isn’t possible to make dm devices with read-only portions that really error out on writes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.