-   Linux - Software (
-   -   kpartx fails to mount partition from an image file (

JRHeisey 07-10-2017 07:59 PM

kpartx fails to mount partition from an image file
  • I am using CentOS 7 as a guest in VirtualBox under Windows 7 host.
  • I am trying to create a bootable SD card by creating its image as file to be used in an embedded system.
  • Used dd to create the sdcard.img file and filled with zeros from /dev/zero
  • Used fdisk to create two partitions in the sdcard.img file
  • When I use kpartx with -av only the second 'root' partition is mounted. The first 'boot' partition does not mount.

I have been researching this all day and have not found any references to this type of issue.

Any suggestions?

If not I'll have to just create the partitions directly on the SD card. I've done this before.

- J.R.

syg00 07-10-2017 11:44 PM

You did something wrong - or at least not what you expected. A zeroed partition will not mount - be careful of your terminology. Very careful.
Run it all again and post a screenshot or cut-and-paste - include everything from the initial "dd". Maybe then we can help.

JRHeisey 07-11-2017 12:17 PM

I initially was blindly following instructions from a DornerWorks document. DornerWorks provide support services for the Xen hypervisor.

As I become more familiar with the tools used I am beginning to understand what each tool does.

Here are the steps from the document from DornerWorks.

1. Create a new clean two partition image.
$ dd if=/dev/zero of=$RELEASE_DIR/dist/images/sdcard.img bs=1G count=7
$ echo -e "n\np\n1\n\n+256M\nt\nc\nn\np\n2\n\n\nt\n2\n83\nw\n" | fdisk
2. Install kpartx if it is not already.
$ sudo apt-get install kpartx
3. Mount both partitions of the image using kpartx.
sudo kpartx -av $RELEASE_DIR/dist/images/sdcard.img
sudo mkfs.vfat /dev/mapper/loop1p1
sudo mkfs.ext4 /dev/mapper/loop1p2

I have a couple of issues.
- For dd I changed the count=7 to count=15 for my larger SD card.
- The string piped to fdisk did not work out-of-the-box. I needed to manually run fdisk to create the two partitions.
- The string shows the first partition set to the system ID of 'c' which is 'W95 FAT32 (LBA)'.
- I tried leaving it at the default of '83' for 'Linux' but it did not change the behavior of kpartx.
- Result of kpartx
$ sudo kpartx -av $RELEASE_DIR/dist/images/sdcard.img
[sudo] password for username:
add map loop0p2 (253:2): 0 14680001 linear /dev/loop0 63
- mkfs.*: loop1 part of loop1p1 reference is not appropriate for my system because it is mounting as loop0. Easy to compensate.
- In the two mkfs.vfat command loop0p1 is not mounted at all just loop0p2.

Results from fdisk -l

Disk /media/sf_projects/RDP3/XZD_20161231/dist/images/sdcard.img: 16.1 GB, 16106127360 bytes, 31457280 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
Disk label type: dos
Disk identifier: 0x322d1bed

                                                      Device Boot      Start        End      Blocks  Id  System
/media/sf_projects/RDP3/XZD_20161231/dist/images/sdcard.img1            2048    2099199    1048576  83  Linux
/media/sf_projects/RDP3/XZD_20161231/dist/images/sdcard.img2        2099200    31457279    14679040  83  Linux

syg00 07-11-2017 06:55 PM

My apologies, all that looks ok.
Been too many years since I looked at XEN, so I can't offer any advice - they are doing something under the covers with the loop device(s).
If you have mkfs.vfat, I would expect (in a normal world) you to also be able mount vfat.

This is what I get (and expect to get) from my small test - non-virtualised. Both mount (manually) as expected.

Disk /dev/loop0: 10 MiB, 10485760 bytes, 20480 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: dos
Disk identifier: 0x0eaf30f8

Device      Boot Start  End Sectors Size Id Type
/dev/loop0p1      2048  6143    4096  2M  c W95 FAT32 (LBA)
/dev/loop0p2      6144 20479  14336  7M 83 Linux

All times are GMT -5. The time now is 04:54 AM.