DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

[SOLVED] Does anyone know how to extract a partition

Mon Mar 23, 2020 6:49 pm

Hello everyone,

So I'm using the USBimager by bzt works a treat. Now I have this raw image and I want mount it to access the files. How do I do this? Ideally I would like to extract the partitions.

The image I have seems to be a noobs image so I only need partitions 6 and 7

I tried

Code: Select all

fdisk -lu [image-file]
Then

Code: Select all

dd if=[image-name] of=part6.img bs=512 skip=# count=#
Of course I used real values but I doubt they are helpful how I figured them out was
skip = Start - 1
Count = End - Start + 1
When I try to mount anything it doesn't work.
Last edited by DarkElvenAngel on Mon Mar 23, 2020 7:15 pm, edited 1 time in total.

User avatar
dickon
Posts: 1168
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: Does anyone know how to extract a partition

Mon Mar 23, 2020 6:53 pm

If you've got a partition table, just use losetup -P, and dd the required partitions off the loop devices. Simple.

jahboater
Posts: 5414
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Does anyone know how to extract a partition

Mon Mar 23, 2020 7:01 pm

Can you just do "dd if=/dev/sda6" of=part6.img ?
"We are in the beginning of a mass extinction, and all you can talk
about is money and fairy tales of eternal economic growth."
- Greta Thunberg

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

Re: Does anyone know how to extract a partition

Mon Mar 23, 2020 7:04 pm

You can use a loop device to mount the filesystems inside the image file (there is a utility that can do this for you, but I can't remember what it is called).

Here's an example using one of the standard downloads (logged in as root using sudo -i, but you could prefix each command with sudo).

Code: Select all

# ls -lh *.img
-rw-r--r-- 1 pi pi 1.8G Mar 23 18:54 2020-02-13-raspbian-buster-lite.img

# fdisk -lu 2020-02-13-raspbian-buster-lite.img
Disk 2020-02-13-raspbian-buster-lite.img: 1.7 GiB, 1849688064 bytes, 3612672 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: 0x738a4d67

Device                               Boot  Start     End Sectors  Size Id Type
2020-02-13-raspbian-buster-lite.img1        8192  532479  524288  256M  c W95 FAT32 (LBA)
2020-02-13-raspbian-buster-lite.img2      532480 3612671 3080192  1.5G 83 Linux

# mkdir /mnt/boot /mnt/root

# mount -t vfat -o loop,offset=$((8192 * 512)),sizelimit=$((524288 * 512)) 2020-02-13-raspbian-buster-lite.img /mnt/boot

# ls /mnt/boot

# mount -t ext4 -o loop,offset=$((532480 * 512)),sizelimit=$((3080192 * 512)) 2020-02-13-raspbian-buster-lite.img /mnt/root

# ls /mnt/root
bin   dev  home  lost+found  mnt  proc	run   srv  tmp	var
boot  etc  lib	 media	     opt  root	sbin  sys  usr

# umount /mnt/boot

#umount /mnt/root
If you edit anything it will update the contents of the image file, so you can write that to an SD card.
Unreadable squiggle

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: Does anyone know how to extract a partition

Mon Mar 23, 2020 7:13 pm

Okay,

@dickon

Code: Select all

losetup -P image-file.img 
Is a no go it wants a loop device.

@jahboater I'm working with an image back so I that's not going to work.

@rpdom this works thanks!

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: [SOLVED] Does anyone know how to extract a partition

Mon Mar 23, 2020 7:21 pm

@rpdom,
Your solution for mounting the image works perfectly

I think the resource I was reading is incorrect about how calculate the offsets for extracting the partitions.

It should be

Code: Select all

dd if=[image] of=[partition] bs=512 skip=Start count=Sectors
Start and Sectors is retrieved from

Code: Select all

fdisk -lu [image]

User avatar
dickon
Posts: 1168
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: Does anyone know how to extract a partition

Tue Mar 24, 2020 11:03 am

DarkElvenAngel wrote:
Mon Mar 23, 2020 7:13 pm
Okay,

@dickon

Code: Select all

losetup -P image-file.img 
Is a no go it wants a loop device.
losetup sets up a loop device...
Last edited by dickon on Tue Mar 24, 2020 12:43 pm, edited 1 time in total.

epoch1970
Posts: 4757
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 12:32 pm

I usually go with kpartx.
The loop devices are created in /dev/mapper/loopXpY

If you try to kpartx -d a mounted image, but the path to the image is different from the path that was used for the previous kpartx -a invocation (say you changed directory in-between), then I think loop deletion fails. To avoid pile-ups of old loop mounts I always use the absolute path to the image in my commands.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: Does anyone know how to extract a partition

Tue Mar 24, 2020 2:15 pm

dickon wrote:
Tue Mar 24, 2020 11:03 am
DarkElvenAngel wrote:
Mon Mar 23, 2020 7:13 pm
Okay,

@dickon

Code: Select all

losetup -P image-file.img 
Is a no go it wants a loop device.
losetup sets up a loop device...
So how do you use it correctly? I'm interested in this method I've never used this command before.

User avatar
dickon
Posts: 1168
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 2:25 pm

Code: Select all

[email protected]:~# losetup -P --show -f 2019-09-26-raspbian-buster-lite.img
/dev/loop0
[email protected]:~# ls /dev/loop*
loop-control  loop0p2       loop3         loop6         
loop0         loop1         loop4         loop7         
loop0p1       loop2         loop5         
[email protected]:~# losetup -d /dev/loop0
[email protected]:~#
That's just an image I happen to have lying around, and it's on debian/amd64, but it's the same thing. /dev/loop0p0 is /boot within that image; /dev/loop0p1 is /. You can dd the individual partitions off (or on) as you would normally, or mount them, or whatnot. It's extremely handy.

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 2:32 pm

dickon wrote:
Tue Mar 24, 2020 2:25 pm

Code: Select all

[email protected]:~# losetup -P --show -f 2019-09-26-raspbian-buster-lite.img
/dev/loop0
[email protected]:~# ls /dev/loop*
loop-control  loop0p2       loop3         loop6         
loop0         loop1         loop4         loop7         
loop0p1       loop2         loop5         
[email protected]:~# losetup -d /dev/loop0
[email protected]:~#
That's just an image I happen to have lying around, and it's on debian/amd64, but it's the same thing. /dev/loop0p0 is /boot within that image; /dev/loop0p1 is /. You can dd the individual partitions off (or on) as you would normally, or mount them, or whatnot. It's extremely handy.
I say extremely handy is an understatement thanks for clearing that up. This will be very useful, I appreciate you clearing that up.

Thank you

User avatar
dickon
Posts: 1168
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 2:46 pm

It's also useful for creating images. You dd a bit of /dev/zero to a file, losetup that file, partition the resulting loop device with your favourite partitioner, create the filesystem(s) you want within those /dev/loopXpY partitions (which should be automatically created by your partition tool), mount them, copy files into them, umount, losetup -d, and you've now got a handy image that can be written to a SD card or USB stick or whatnot. There are caveats: sizing is difficult, and the approaches used by LibreELEC.tv and like don't always work (they use du to calculate the rough size that should be needed, but this breaks if your filesystem uses transparent compression, as ZFS can).

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 3:03 pm

dickon wrote:
Tue Mar 24, 2020 2:46 pm
It's also useful for creating images. You dd a bit of /dev/zero to a file, losetup that file, partition the resulting loop device with your favourite partitioner, create the filesystem(s) you want within those /dev/loopXpY partitions (which should be automatically created by your partition tool), mount them, copy files into them, umount, losetup -d, and you've now got a handy image that can be written to a SD card or USB stick or whatnot. There are caveats: sizing is difficult, and the approaches used by LibreELEC.tv and like don't always work (they use du to calculate the rough size that should be needed, but this breaks if your filesystem uses transparent compression, as ZFS can).
This was going to come up because I need to make an image with a larger boot partition, Buildroot keeps them tiny and I need a bigger one.

So making partition isn't hard at all would I be able to use dd to merge in rootfs or I need to copy it with rsync or similar tool?

My thought is dd would work if the partitions are the same size.

User avatar
dickon
Posts: 1168
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 3:19 pm

You thought correctly. Unless it's NTFS, which is broken (as it embeds the start sector somewhere in the header -- this is stupid).

epoch1970
Posts: 4757
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 3:30 pm

BR uses genimage to generate SD images, IIRC. At least when you build for ARM/Pi.
Personally I think it is a dreadful Piece of Software, but if it works for you, editing the partitions map/size etc. is pretty easy. There is a file in your BR environment that gets slurped by genimage when the time comes. Don't remember its name, sorry. Look in the default post-image scripts for your environment.

NB: "post-image" means runs after filesystem images (.tar.gz, .ext4 ...) are built by BR.
"Post-image" scripts actually output the SD.img you expect to end-up with when building for Pi. They are pre-sd-image ;)
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 3:32 pm

dickon wrote:
Tue Mar 24, 2020 3:19 pm
You thought correctly. Unless it's NTFS, which is broken (as it embeds the start sector somewhere in the header -- this is stupid).
Thanks again, invaluable information!

I'm not a fan NTFS avoiding at all costs.

DarkElvenAngel
Posts: 574
Joined: Tue Mar 20, 2018 9:53 pm

Re: [SOLVED] Does anyone know how to extract a partition

Tue Mar 24, 2020 3:44 pm

epoch1970 wrote:
Tue Mar 24, 2020 3:30 pm
BR uses genimage to generate SD images, IIRC. At least when you build for ARM/Pi.
Personally I think it is a dreadful Piece of Software, but if it works for you, editing the partitions map/size etc. is pretty easy. There is a file in your BR environment that gets slurped by genimage when the time comes. Don't remember its name, sorry. Look in the default post-image scripts for your environment.

NB: "post-image" means runs after filesystem images (.tar.gz, .ext4 ...) are built by BR.
"Post-image" scripts actually output the SD.img you expect to end-up with when building for Pi. They are pre-sd-image ;)
I'm debugging the post tools now I have a failure to package the boot partition. I just like the idea of being able to adjust and set up the images before I burn them to the SD I have lots of tweaks I need to do before using it.

Plus I can get noobs off this original image and resize the boot partition so I can attempt a Buster upgrade.

Return to “Advanced users”