4

I created a mapper device with dmsetup and created a partition table with parted:

$ fdisk -l /dev/mapper/vdisk Disk /dev/mapper/vdisk: 511.57 GiB, 549295737344 bytes, 1072843237 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: gpt Disk identifier: OMITTED Device Start End Sectors Size Type /dev/mapper/vdisk-part1 2048 204799 202752 99M EFI System /dev/mapper/vdisk-part2 204800 1072841188 1072636389 511.5G Microsoft basic data 

Now how do I manipulate the partitions, say format the first partition to FAT? /dev/mapper/vdisk-part1 or /dev/mapper/vdisk1 don't seem to exist.

PS: I do remember /dev/mapper/vdisk1 or something similar appeared after creating the partition table with parted, but disappeared after a reboot.

2
  • May you share the dmsetup command you used ? May you share the result of ls -lhiar /dev/mapper/ ? thanks Commented Sep 11, 2024 at 11:03
  • 1
    Just a general observation, but unless you are using the mapper device as part of a multipath setup or as storage for a VM disk image, it’s almost always preferable to partition the underlying devices because essentially the entire storage stack on Linux expects things to be done that way, so it will largely ‘just work’ without any need for special handling on your part. Commented Sep 11, 2024 at 20:23

2 Answers 2

8

Kernel by default doesn't look for partitions on DM devices so you need to tell it to do that with a tool like partprobe (from parted project) or kpartx (from multipath project; kpartx creates DM linear devices for the partitions).

$ sudo partprobe /dev/mapper/vdisk 

or

$ sudo kpartx -a /dev/mapper/vdisk 
$ lsblk /dev/mapper/vdisk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS vdisk 253:2 0 2G 0 dm $ sudo kpartx -a /dev/mapper/vdisk $ lsblk /dev/mapper/vdisk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS vdisk 253:2 0 2G 0 dm └─vdisk1 253:3 0 2G 0 part 
1

You can create offset loop devices that refer to individual partitions and operate on them. For example, to map the first partition:

losetup --offset 1024576 --sizelimit 103809024 /dev/loop0 /dev/mapper/vdisk 

Now, /dev/loop0 is your partition, it's where you do mkfs and whatever. After use, do losetup -d /dev/loop0 to detach.

The numbers are calculated as follows: offset = start * 512, sizelimit = (end + 1 - start) * 512 = sectors * 512, where 512 is a sector size. (Not all versions of fdisk show partition size in sectors, some show in blocks, typically of 1K; to resolve ambiguity I use the fields that are definitely always know.)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.