andrew R
Posts: 2
Joined: Tue Jan 08, 2013 2:07 pm

how do i open/edit a rasberry image?

Tue Jan 08, 2013 2:27 pm

i have googled the heck out of this so please forgive me if this is a no brainer...

i have downloaded rasberry images from various distributions and would like to
open an image on my laptop to make adjustments before i boot the rasberry itself.

i get the following error when i try to mount an image on my laptop:

mount 2012-08-08-wheezy-armel.img /mnt -o loop
mount: unknown filesystem type '(null)'
mount 2012-08-08-wheezy-armel.img /mnt
mount: unknown filesystem type '(null)'

i get this error across the board with every distribution.

i cant make my edits on a running rasberry because the change i need is to set up the
rasberry serial console from the boot line (i have no keyboard, screen or mouse) so no
rasberry until i get this working.

maybe my kernel needs an option i don’t have? here is my laptop environment:-

cat /etc/issue
Welcome to openSUSE 12.2 "Mantis" - Kernel \r (\l).

uname -a
Linux linux-ywym.site 3.4.6-2.10-desktop #1 SMP PREEMPT Thu Jul 26 09:36:26 UTC 2012 (641c197) i686 i686 i386 GNU/Linux

how do i mount a rasberry image (either on the sd card or as an image) on my laptop??

thanks

andrew.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: how do i open/edit a rasberry image?

Tue Jan 08, 2013 3:10 pm

Does this help?? - http://en.wikibooks.org/wiki/QEMU/Image ... n_the_host

If that doesn't work then I guess you'll have to flash an SD Card and mount it on the Raspberry PI via a USB memory card reader.
Once you have edited what it required you extract an image file back from the SD Card.

It maybe possible to avoid flashing if you use the image file as a secondary hard drive with Win32 Qemu running Raspberry Pi or a Live Linux distro like Mint.

Richard S.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4248
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: how do i open/edit a rasberry image?

Tue Jan 08, 2013 3:15 pm

@redhawk, the problem with that is that you would need to know the offset to the second partition. Theoretically that is not impossible, but I don't know how to do it.

I think the problem you are seeing is that the image includes the partition table and two partitions, whereas the filesystem drivers expect to see single partitions.

Why can you not write the SD card and then mount the filesystems from that on your laptop? (It hot-plugs fine on Linux Mint.) There's the extra step of dd'ing the card back into an image file, but it's not a hardship.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: how do i open/edit a rasberry image?

Tue Jan 08, 2013 5:09 pm

You could DD the file to a USB memory stick or even a hard drive if you don't have the technology to do an SD card
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

pksato
Posts: 295
Joined: Fri Aug 03, 2012 5:25 pm
Location: Brazil

Re: how do i open/edit a rasberry image?

Tue Jan 08, 2013 5:52 pm

Hi
After few attempts, I could mount two of partition os raspbian image.
Problem is how to determine offset byte of partitions begins?
Fdisk can show some information, file command also.

Code: Select all

$ /sbin/fdisk -lu 2012-12-16-wheezy-raspbian.img 
Disk 2012-12-16-wheezy-raspbian.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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 identifier: 0x00017b69

                         Device Boot      Start         End      Blocks   Id  System
2012-12-16-wheezy-raspbian.img1            8192      122879       57344    c  W95 FAT32 (LBA)
2012-12-16-wheezy-raspbian.img2          122880     3788799     1832960   83  Linux
From above fdisk information, first partition start on sector 8182, and second on 122880. And each have 512 bytes.

Code: Select all

$ file 2012-12-16-wheezy-raspbian.img 
2012-12-16-wheezy-raspbian.img: x86 boot sector; partition 1: ID=0xc, starthead 130, startsector 8192, 114688 sectors; partition 2: ID=0x83, starthead 165, startsector 122880, 3665920 sectors, code offset 0xb8
File command show same information.

To get offset in bytes, multiply start sector by 512.
First partition have a offset of 4194304 bytes,
And, second, 62914560 bytes.

To mount,

Code: Select all

mkdir /mnt/img1
mount -t vfat -o loop,offset=4194304 /tmp/rpi/2012-12-16-wheezy-raspbian.img /mnt/img1/
mkdir /mnt/img2
mount -t ext4 -o loop,offset=62914560 /tmp/rpi/2012-12-16-wheezy-raspbian.img /mnt/img2/
Make modifications, umount and dump to SD card.

Max

Re: how do i open/edit a rasberry image?

Tue Jan 08, 2013 6:12 pm

For people that are too lazy to do offset calculations (like me):

Code: Select all

$ sudo kpartx -av your-image.img 
add map loop0p1 (252:5): 0 117187 linear /dev/loop0 1
add map loop0p2 (252:6): 0 3493888 linear /dev/loop0 118784
$ sudo mount /dev/mapper/loop0p2 /mnt
"sudo umount /mnt" and "sudo kpartx -d your-image.img" when done.

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

Re: how do i open/edit a rasberry image?

Tue Jan 08, 2013 6:27 pm

kpartx looks interesting. It is not part of the default instal, though.

FWIW (addressing the main thread topic), I think that for most practical purposes, the sequence:

1) Write image to card (using a Linux system other than the Pi)
2) Make changes on the card (using a Linux system other than the Pi)
3) Boot card (on the Pi)

is going to be easiest and best.

You would only need to modify the image itself if you are planning on writing it to multiple cards.
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)

andrew R
Posts: 2
Joined: Tue Jan 08, 2013 2:07 pm

Re: how do i open/edit a rasberry image?

Fri Jan 11, 2013 12:45 pm

rurwin wrote:@redhawk, the problem with that is that you would need to know the offset to the second partition. Theoretically that is not impossible, but I don't know how to do it.

I think the problem you are seeing is that the image includes the partition table and two partitions, whereas the filesystem drivers expect to see single partitions.

Why can you not write the SD card and then mount the filesystems from that on your laptop? (It hot-plugs fine on Linux Mint.) There's the extra step of dd'ing the card back into an image file, but it's not a hardship.
"Why can you not write the SD card and then mount the filesystems from that on your laptop?"

looks like i was over-analyzing everything and the above suggestion did the trick...

burning the image to an sd card and sliding it right back into the laptop makes all partitions visible and editable. dont know why that didnt work when i first tried it.

i am still glad i posted this question-i have received some amazing mount syntaxes that i didnt even know existed....

thanks to all....

andrew.

sonnym
Posts: 3
Joined: Thu Feb 27, 2014 5:16 pm

Re: how do i open/edit a rasberry image?

Thu Feb 27, 2014 5:20 pm

I combined the information in this thread into the following script:

Code: Select all

IMG_FILE=$(find dist/build/raspbian -name '*.img')
SECTOR_OFFSET=$(sudo /sbin/fdisk -l $IMG_FILE | awk '$6 == "Linux" { print $2 }')
BYTE_OFFSET=$(expr 512 \* $SECTOR_OFFSET)

sudo mount -o loop,offset=$BYTE_OFFSET $IMG_FILE dist/build/mnt

sonnym
Posts: 3
Joined: Thu Feb 27, 2014 5:16 pm

Re: how do i open/edit a rasberry image?

Wed Mar 12, 2014 3:54 pm

A better method than manually calculating the offsets for the image file is to map the img partitions as devices. This can be done with the following (run as root):

Code: Select all

/sbin/modprobe loop
/sbin/kpartx -asv 2014-01-07-wheezy-raspbian.img
mount /dev/mapper/loop0p1 /mnt/rpi
You need to load the loop module before mapping devices. kpartx will tell you the name of the device (it may not be loop0), ala:

Code: Select all

add map loop0p1 (253:1): 0 196608 linear /dev/loop0 2048
add map loop0p2 (253:2): 0 849920 linear /dev/loop0 198656

richardash1981
Posts: 4
Joined: Sat Feb 09, 2013 5:02 pm

Re: how do i open/edit a rasberry image?

Sat Apr 05, 2014 3:18 pm

A bit late to this, but you don't need kpartx in order to get the image file partitions accessible via a loopback - you can just use the -P option to losetup. A little more work, but only needs the util-linux package installed, which you almost certainly have.
First, we need a free loopback device to use:

Code: Select all

sudo losetup -f
/dev/loop0
then set it up with the file

Code: Select all

sudo losetup -P /dev/loop0 lighting-pi-20140328.img
fdisk can then show you the partitions on your image

Code: Select all

sudo fdisk -l /dev/loop0
and you can mount whichever partition you want

Code: Select all

sudo mount /dev/loop0p2 /media/spare/
When you are done, you unmount the file system, then detach the file from the loopback device

Code: Select all

sudo umount /media/spare/
sudo losetup -d /dev/loop0

tvjon
Posts: 632
Joined: Mon Jan 07, 2013 9:11 am

Re: how do i open/edit a rasberry image?

Sat Apr 05, 2014 4:31 pm

What version are you using?

losetup: invalid option -- 'P'

richardash1981
Posts: 4
Joined: Sat Feb 09, 2013 5:02 pm

Re: how do i open/edit a rasberry image?

Sun Apr 06, 2014 11:43 am

losetup from util-linux 2.22.2 (Gentoo)

The man page is dated July 2003, so this isn't exactly the bleeding edge.

infogulch
Posts: 1
Joined: Sun Apr 13, 2014 12:05 am

Re: how do i open/edit a rasberry image?

Sun Apr 13, 2014 12:18 am

To expand on sonnym's script, this script mounts the image file passed as an argument to /mnt/FILENAME:

Code: Select all

#!/bin/sh

IMG_FILE=$1
SECTOR_OFFSET=$(sudo /sbin/fdisk -l $IMG_FILE | awk '$6 == "Linux" { print $2 }')
BYTE_OFFSET=$(expr 512 \* $SECTOR_OFFSET)

IMG_DIR=$(basename "$IMG_FILE")
IMG_DIR="${IMG_DIR%.*}"

echo Mounting at /mnt/$IMG_DIR

sudo mkdir -p /mnt/$IMG_DIR
sudo mount -o loop,offset=$BYTE_OFFSET $IMG_FILE /mnt/$IMG_DIR
Put this in my home dir named mount-image.sh, it works for me:

Code: Select all

[email protected]:~$ chmod 755 mount-image.sh 
[email protected]:~$ ./mount-image.sh 2013-09-10-wheezy-raspbian.img 
[sudo] password for infogulch: 
Mounting at /mnt/2013-09-10-wheezy-raspbian
[email protected]:~$ ls /mnt/2013-09-10-wheezy-raspbian/
bin   dev  home  lost+found  mnt  proc  run   selinux  sys  usr
boot  etc  lib   media       opt  root  sbin  srv      tmp  var
[email protected]:~$

dpenezic
Posts: 99
Joined: Sat Dec 22, 2012 2:36 pm

Re: how do i open/edit a rasberry image?

Wed Feb 11, 2015 11:36 am

Hi all,
in last post script need to use fdisk -lu instead fdisk -l .

I create script for mount and umount /boot and / from disk image. Code is follow:

Code: Select all

#!/bin/sh                                          
# Author: Dubravko Penezic, [email protected]     
#                                                  
# Credit to valuable answer from few users on Raspberry Pi Forum , http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=28860
#

OPTION=$1
IMG_FILE=$2
MOUNT_POINT=/datacopy/RPI-raspbian-image/Work/Img

IMG_DIR=$(basename "$IMG_FILE")
IMG_DIR="${IMG_DIR%.*}"

if [[ "$#" -lt 2 || ("$1" != "m" && "$1" != "u") ]]
  then
    echo "Please use script as follow:"
    echo ""
    echo '> $0 m|u <disk_img_file>'
    echo "     m - mount"
    echo "     u - umount"
    exit 1
fi

if [[ "$1" == "m" ]]
  then
    SECTOR_OFFSET=$(sudo /sbin/fdisk -lu $IMG_FILE | awk '$6 == "Linux" { print $2 }')
    BYTE_OFFSET=$(expr 512 \* $SECTOR_OFFSET)
    SECTOR_OFFSET_BOOT=$(sudo /sbin/fdisk -lu $IMG_FILE | awk '$6 == "W95" { print $2 }')
    BYTE_OFFSET_BOOT=$(expr 512 \* $SECTOR_OFFSET_BOOT)

    echo Mounting image / at $MOUNT_POINT/$IMG_DIR
    echo Sector offset $SECTOR_OFFSET - Byte offset $BYTE_OFFSET

    sudo mkdir -p $MOUNT_POINT/$IMG_DIR
    sudo mount -t ext4 -o loop,offset=$BYTE_OFFSET $IMG_FILE $MOUNT_POINT/$IMG_DIR

    echo Sector offset $SECTOR_OFFSET_BOOT - Byte offset $BYTE_OFFSET_BOOT
    echo Mounting image /boot at $MOUNT_POINT/${IMG_DIR}_boot

    sudo mkdir -p $MOUNT_POINT/${IMG_DIR}_boot
    sudo mount -t vfat -o loop,offset=$BYTE_OFFSET_BOOT $IMG_FILE $MOUNT_POINT/${IMG_DIR}_boot
fi

if [[ "$1" == "u" ]]
  then

    echo Unmounting image / at $MOUNT_POINT/$IMG_DIR
    sudo umount $MOUNT_POINT/$IMG_DIR
    sudo rmdir $MOUNT_POINT/$IMG_DIR

    echo Unmounting image /boot at $MOUNT_POINT/${IMG_DIR}_boot
    sudo umount $MOUNT_POINT/${IMG_DIR}_boot
    sudo rmdir $MOUNT_POINT/${IMG_DIR}_boot

fi

rumjar86
Posts: 1
Joined: Sun Jul 12, 2015 12:06 am

Re: how do i open/edit a rasberry image?

Sun Jul 12, 2015 12:12 am

sorry if the is an old thread, but I am having a similar issue. to start off I am trying to get a bitcoin miner running but the img file I have is for a model B and I have a B+ and the img file has files I need to get it to run correctly. I can't do the usb or card reader as with the img in question doesn't enable the usb and or lan so I'm stuck something bad.
I am still waiting for support on the bitcoin forums atm but info on this thing is rare as and so hoping I can just extract the files needed to the newer weezy and away i go or ssh into the new weezy to copy the files over into the right directories etc.
please any ideas would be greatly appreciated.
Josh

Return to “General discussion”