User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 1:24 pm

Here's what I did:

mount -o loop,offset=62914560 raspbian_image_file_here.img /myfolder

It works but why?
Antikythera

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 1:33 pm

Maybe superblock should be something you google. If a filesystem is to be error redundant, then you have to accept errors and perform a best case algorithm to correct them. I haven't tried this, so maybe it's a grand joke.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 1:52 pm

the mount command above gives me partition 2, the linux partition. I can then look at the files.
I have guesses as to what it is doing, but not sure really.

It gives me what I want. But why does it work?
Antikythera

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 2:00 pm

It is mounting a binary image (bits, called image because people looked at them, and saw), from an offset (not the start of the image file), and the image has a binary copy/backup of the partition table.

It's just a series of blocks (a number of bits of a fixed size is a block of bits), and just as a disk has bits stored in a sequence of blocks on physical hardware, an image file is more abstract, but can and does represent a sequence of blocks, and so can be mounted as a binary "block device" with a little abstraction help from the vfs (virtual file system). Virtual has a definite meaning, or maybe two, but both are exactly defined by code.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

User avatar
jojopi
Posts: 3085
Joined: Tue Oct 11, 2011 8:38 pm

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 2:10 pm

If you run "fdisk -lu" on the image you will see that the second partition starts at sector 122880:

Code: Select all

Disk raspbian_image_file_here.img: 1939 MB, 1939865600 bytes
255 heads, 63 sectors/track, 235 cylinders, total 3788800 sectors
Units = sectors of 1 * 512 = 512 bytes

                       Device Boot      Start         End      Blocks   Id  System
raspbian_image_file_here.img1            8192      122879       57344    c  W95 FAT32 (LBA)
raspbian_image_file_here.img2          122880     3788799     1832960   83  Linux
A slightly clearer version of the mount command is:

Code: Select all

mount -o loop,offset=$((122880*512)) raspbian_image_file_here.img /myfolder

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 2:18 pm

I'm totally amazed at some of the things that Linux machines do.

Like, when I type "cat file", it displays the contents of the file on the terminal.

When I type "ls", it gives me a list of all my files.

When I type "who" (or, amazingly enough, also "finger"), it gives me a list of who is logged on the system.

What is up with all of that???? What's going on here?
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 2:38 pm

Thanks.
So from what jack and jojopi are saying, I gather my previous mount command will not work in the next release of raspbian.

Now in fdisk -l 2013...raspbian.img
I get raspbian.img1 and raspbian.img2.
Is there a way to mount those the way you use /dev/sda1 and sdb3?
There obviously isn't a /dev/img1 and img2.
Antikythera

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 5:21 pm

solar3000 wrote:Thanks.
So from what jack and jojopi are saying, I gather my previous mount command will not work in the next release of raspbian.

Now in fdisk -l 2013...raspbian.img
I get raspbian.img1 and raspbian.img2.
Is there a way to mount those the way you use /dev/sda1 and sdb3?
There obviously isn't a /dev/img1 and img2.
It should still work as long as the partitions don't change positions.

you can mount them with a loopback device, though there's no real easy way to do it as they both involve offsets

Code: Select all

losetup -f --show -o <offset> <path-to-image>
That command will create a lopoback e.g. /dev/loop0 which you will then be able to mount / unmount like /dev/sda1
Then once you're done with it, you delete the loopback device

Code: Select all

losetup -d /dev/loop0
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 5:41 pm

Excellent. If you're a "madman" who "rants and raves", I must be so too because I find your answer useful.

After playing with this a while, I find that its not really a problem at all.
I just mounted the vfat with:
mount -oloop,offset=$((8192*512)) rasp...img /dest/mnt
Antikythera

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 5:41 pm

I did write a script at one point that automates this process. I.e., parses the output of 'file' to get the offset values, then stuffs that into the mount commands.

Oh, here it is. The operative lines are:

Code: Select all

for i in $(file 2012-12-16-wheezy-raspbian.img|gawk 'BEGIN {RS="startsector"} NR > 1 {print $0*512}');do
    mount -o ro,offset=$i 2012-12-16-wheezy-raspbian.img /mnt
...
    done
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

gsh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1442
Joined: Sat Sep 10, 2011 11:43 am

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 6:07 pm

This is how I do it...

Code: Select all

$ sudo losetup /dev/loop0 raspbian.img
$ sudo kpartx /dev/loop0
$ sudo mount /dev/mapper/loop0p1 /mnt/part1
$ sudo mount /dev/mapper/loop0p5 /mnt/part5
$ sudo mount /dev/mapper/loop0p6 /mnt/part6
Think you may need to apt-get install kpartx

Basically the problem is, loopback normally only works with a single partition image (i.e. an image of a bit of the disk!) whereas what you have is an image of the whole disk, so you need to create a loopback using losetup from /dev/loop0 to the image and then use kpartx. This understands partitions and creates a bunch of new special function devices in /dev/mapper/... just go have a look...

Finally now you have the /dev/ entries for all the partitions then you can individually mount them

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 6:16 pm

if there are multiple partitions can tell mount where to start, but how does it know where the partition ends?

so If I have five partitions and I mount partition 3 and it starts at [arbitrary number] 3000 and it 5000.
How does mount know where it ends?
If offset=<start position> what about where it ends?
Antikythera

User avatar
rpdom
Posts: 15393
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: mount raspbian.img, offset works, but why?

Tue Jul 02, 2013 7:30 pm

solar3000 wrote:if there are multiple partitions can tell mount where to start, but how does it know where the partition ends?

so If I have five partitions and I mount partition 3 and it starts at [arbitrary number] 3000 and it 5000.
How does mount know where it ends?
If offset=<start position> what about where it ends?
The end isn't too important for partitions with filesystems on, as it will only use the filesystem sized area. But you can specify a "sizelimit=" option to tell it how much of the file to use. You can use the "blocks" parameter from fdisk -l and put a "K" on the end for this value.

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: mount raspbian.img, offset works, but why?

Wed Jul 03, 2013 12:18 am

thanks
Antikythera

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: mount raspbian.img, offset works, but why?

Wed Jul 03, 2013 1:00 am

solar3000 wrote:Excellent. If you're a "madman" who "rants and raves", I must be so too because I find your answer useful.

After playing with this a while, I find that its not really a problem at all.
I just mounted the vfat with:
mount -oloop,offset=$((8192*512)) rasp...img /dest/mnt
I actually built a script that would show you the available partitions in an image file and allow you to mount / unmount them individually.
It's on my blog somewhere, but last time I checked it wasn't quite working properly anymore and I haven't had time to fix it yet
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

User avatar
jackokring
Posts: 816
Joined: Tue Jul 31, 2012 8:27 am
Location: London, UK
Contact: ICQ

Re: mount raspbian.img, offset works, but why?

Wed Jul 03, 2013 11:26 am

The reason you do not need to specify an end is because a filesystem includes information about the size of the filesystem. So start+size=end. Simple really.
Pi[NFA]=B256R0USB CL4SD8GB Raspbian Stock.
Pi[Work]=A+256 CL4SD8GB Raspbian Stock.
My favourite constant 1.65056745028

Return to “Advanced users”