0xfff1
Posts: 21
Joined: Sun Jan 12, 2014 7:01 pm

Write Raspbian img to external drive partition

Wed Jun 13, 2018 8:58 am

Hi,
I would like to boot my Raspberry Pi 3B+ from USB external drive. I alredy have a drive with these partitions:

Code: Select all

lsblk /dev/sda
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk
├─sda1   8:1    0     8G  0 part
├─sda2   8:2    0     1G  0 part
└─sda3   8:3    0 456.8G  0 part
The first partition is for OS, the second for swap and third for data.

So, I downloaded the raspbian image and I wrote the img file to the first partition with the command, in order to preserve existing partitions:

Code: Select all

sudo dd bs=4M if=2018-04-18-raspbian-stretch-lite.img of=/dev/sda1 conv=fsync
At the end I expected to find 4 partition on the drive:

  • boot
  • rootfs
  • swap
  • data

But I still have the same 3 partitions, where am I doing wrong?

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: Write Raspbian img to external drive partition

Wed Jun 13, 2018 9:04 am

The image file is a "whole disk" image that includes a partition table with two partitions (for boot and root filesystems).
It will not work if you try and write it into an existing partition.

See the instructions here : https://www.raspberrypi.org/documentati ... des/msd.md

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

0xfff1
Posts: 21
Joined: Sun Jan 12, 2014 7:01 pm

Re: Write Raspbian img to external drive partition

Wed Jun 13, 2018 9:11 am

PeterO wrote:
Wed Jun 13, 2018 9:04 am
The image file is a "whole disk" image that includes a partition table with two partitions (for boot and root filesystems).
It will not work if you try and write it into an existing partition.

See the instructions here : https://www.raspberrypi.org/documentati ... des/msd.md

PeterO
I already read the instructions on the page but i don't want to erase the data partition on my drive... :(

feelslikeautumn
Posts: 307
Joined: Wed Aug 09, 2017 9:51 pm

Re: Write Raspbian img to external drive partition

Wed Jun 13, 2018 9:35 am

It is possible to write a partition from an image file to a partition on a drive. Or you could just use the cp -a command to transfer the files.

The complicated bit is booting the pi. If you want to just boot with the USB drive then you need the bootloader files on partition number 1 (it can be anywhere on the disk though).

User avatar
PeterO
Posts: 4867
Joined: Sun Jul 22, 2012 4:14 pm

Re: Write Raspbian img to external drive partition

Wed Jun 13, 2018 9:54 am

0xfff1 wrote:
Wed Jun 13, 2018 9:11 am
PeterO wrote:
Wed Jun 13, 2018 9:04 am
The image file is a "whole disk" image that includes a partition table with two partitions (for boot and root filesystems).
It will not work if you try and write it into an existing partition.

See the instructions here : https://www.raspberrypi.org/documentati ... des/msd.md

PeterO
I already read the instructions on the page but i don't want to erase the data partition on my drive... :(
The image file is not intended to be used like that, but it can be done with a bit of extra work.

The way I use dto do it was like this....

1) Uncompress the image file to a plain ".img" file
2) Use kpartx to access the image file "sudo kpartx -a -v uncompressed_raspbian_image_file_name" which will create block devices in /dev/mapper

Code: Select all

$ sudo kpartx -a -v 2018-04-18-raspbian-stretch.img 
add map loop0p1 (252:0): 0 88262 linear /dev/loop0 8192
add map loop0p2 (252:1): 0 9576448 linear /dev/loop0 98304

$ sudo mount /dev/mapper/loop0p2 /mnt
$ ls /mnt
bin   dev  home  lost+found  mnt  proc  run   srv  tmp  var
boot  etc  lib   media       opt  root  sbin  sys  usr
$
You can then copy the files from there into the existing partitions for boot and root on your disk. You will then have to fix the config.txt and fstab files to make them mount the correct partitions.

N.B. I've not had to do it this way myself for some time but it should still work.

HTH

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

0xfff1
Posts: 21
Joined: Sun Jan 12, 2014 7:01 pm

Re: Write Raspbian img to external drive partition

Wed Jun 13, 2018 1:04 pm

Ok, I'll give it a try ;)

Return to “Advanced users”