Tuesday, November 25, 2014

filesystems - How to create USB flash ".bin" image in Linux without physical drive


I have some files in Linux, which I have to copy to multiple SD-cards in Windows periodically (not bootable, but with file permissions, filesystem ext3). Now the procedure is: I copy files to one flash drive under Linux, then create a .bin image from this drive and clone this .bin to other USB-drives under Windows. Files sometimes change, so I need to create .bin file again. The idea is to create .bin files without actually using physical drive.


Is there a solution to create USB-drive images virtually? Or is there a tool to burn ext3 USB-flash drives from tag.bz2 file under Windows?


Answer



The obvious solution is to mount the image directly in Linux and make the changes but there is a small problem. These media are partitioned a similar way as a hard drive. The solution is to mount the right partition from the image.


Check that the image is indeed partitioned:


$ file -k OpenELEC-RPi.arm-4.95.1.img
OpenELEC-RPi.arm-4.95.1.img: x86 boot sector; partition 1: ID=0xc, active, starthead 32, startsector 2048, 262145 sectors; partition 2: ID=0x83, starthead 146, startsector 266240, 65537 sectors

Working with the image


Map the partitions from the image to loop devices:


$ sudo kpartx -av OpenELEC-RPi.arm-4.95.1.img
add map loop0p1 (252:3): 0 262145 linear /dev/loop0 2048
add map loop0p2 (252:4): 0 65537 linear /dev/loop0 266240

On Ubuntu kpartx is not installed by default. Do sudo apt-get install kpartx.


Mount the partition:


$ sudo mount /dev/mapper/loop0p1 /mnt/tmp1

Now make your changes in the /mnt/tmp1 directory.


Umount the partition and delete the mapping:


$ sudo umount /dev/mapper/loop0p1
$ sudo kpartx -dv OpenELEC-RPi.arm-4.95.1.img
del devmap : loop0p2
del devmap : loop0p1
loop deleted : /dev/loop0

Other options


If kpartx is not available you can determine the partition offset using for example fdisk (It works on images too so you can create the image without having a physical drive at all.) and map them using losetup or mount -o loop,offset=x or even the new version of losetup (from util-linux 2.21) can map the partitions directly using the option --partscan.


There are descriptions in other questions:


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...