2017-04-16

Linux - how to format multiple file systems within one file?

source : Linux - how to format multiple file systems within one file?

psihodelia:

Linux - how to format multiple file systems within one file?
I need to create a disk image with two empty file systems. I have created it using

dd if=/dev/zero of=./disk.img bs=1MiB count=1024

Next, I have created 2 primary partitions using fdisk disk.img; one is FAT32 and other is EXT3. Now, I have to format both partitions in order to create file-systems to be mounted as -o loop devices. But I can't understand how to format them? I can't use mkfs.vfat on disk.img. So I am totally confused.
SOLUTION: Thanks to answer from @pjc50 I found very simple solution:

sudo aptitude install multipath-tools
sudo kpartx -a disk.img   #it maps (mounts) found partitions to /dev/mapper/loop...
sudo mkfs.vfat -F 32 -n boot /dev/mapper/loop0p1
sudo mkfs.ext3 -L rootfs /dev/mapper/loop0p2

A drawback of this solution is requirement of superuser rights.

-----------------------------------------------------------------------
karatedog:
I would go with the tools I have in mind:
create a new VM in Virtualbox with one disk, that would usually be /dev/sda
boot into the VM with a GParted Live CD
partition and format the disk in the VM to your needs (2 partitions, different file systems, etc.)
then use dd to export /dev/sda to a file
With an educated guess it would take about 15 minutes.

-----------------------------------------------------------------------
Shawn Chin:
You can do so by first mounting your partitions to /dev/loop? using losetup with the -o option to specify a suitable offset to your partition. The offset can be calculated based on the output of fdisk -l disk.img (start_sector * sector_size).
For example:

losetup -o32256 /dev/loop1 ./disk.img   # mount first partition

Once mounted, you can then proceed to format the partition using mkfs.*:

mkfs.vfat -F32 /dev/loop1

For more details and examples, see the following articles:
http://wiki.osdev.org/Loopback_Device#Mounting
http://web2.clarkson.edu/projects/itl/honeypot/ddtutorial.txt
http://wiki.eeeuser.com/howtocustomrestoreimage:pt2mkcustomimage

-----------------------------------------------------------------------
pjc50
It appears you can use the kpartx tools: http://robert.penz.name/73/kpartx-a-tool-for-mounting-partitions-within-an-image-file/
Kpartx can be used to set up device mappings for the partitions of any partitioned block device. It is part of the Linux multipath-tools. With kpartx -l imagefile you get an overview of the partitions in the image file and with kpartx -a imagefile the partitions will accessible via /dev/mapper/loop0pX (X is the number of the partition). You can mount it now with mount /dev/mapper/loop0pX /mnt/ -o loop,ro. After unmounting you can disconnect the mapper devices with kpartx -d imagefile.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.