1

Trying to copy Raspberry Pi OS to image using dd.

$diskutil list /dev/disk2
/dev/disk2 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *15.9 GB    disk2
   1:             Windows_FAT_32 boot                    268.4 MB   disk2s1
   2:                      Linux                         2.7 GB     disk2s2

Running sudo dd bs=1m if=/dev/rdisk2 of=/Users/sunknudsen/tmp/pi-clean.img works but includes unallocated space which is inefficient.

sunknudsen
  • 277
  • 2
  • 9
  • You'll have to shrink the image first, or create one that fits. – goldilocks May 15 '21 at 15:52
  • Thanks for helping out... I removed `init=/usr/lib/raspi-config/init_resize.sh` from `/boot/cmdline.txt` so I believe I don’t need to shrink image first. – sunknudsen May 15 '21 at 15:57
  • 1
    So to clarify, the "unallocated space" you are referring to is the rest of the SD card? Ie., you **do** want the entire 2.7 GB partition but not the rest of the ~16 GB card. You could either copy the two partitions individually and concatenate them, or else create an image and copy in the content. I haven't tried the first method, but it should work if done properly. – goldilocks May 15 '21 at 16:37

2 Answers2

1

dd by default copies the physical data - even if the data is zeros SD clone from menu in Raspberry OS uses "cp" command - I checked this while doing the clone ;-) But SD clone does much more than copying only the data (prepares partition table, modifies the /etc/fstat etc.) - it is binary file, not a script - so kind of difficult to see how it does step by step I did not manage to get anything better and efficient than SD clone yet - neither on Linux nor on MacOS - I would like to know it as well.

1

dd copies blocks (regardless of their contents).

macOS will not read the ext4 filesystem and is thus incapable of determining contents or shrinking the image.

You need a Linux system (which can be the Pi). There are many answers explaining how to do this.

You CAN determine the size of MBR plus boot plus root and instruct dd to copy just that, but this seems rather pointless as an unexpanded image is unusable.

I have written scripts using fdisk on the Mac to manipulate Pi image partitions but this is error prone and tedious and ultimately not particularly useful.

If you want to do this on a one off it is probably easier to manually calculate the size (using the output of fdisk list).

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • Thanks for helping out. What do you mean by “unexpanded image is unusable”? – sunknudsen May 15 '21 at 23:11
  • Would love to get your feedback on https://gist.github.com/sunknudsen/2b684774df8973f866e851b56ddaff11. Btw, I resized `disk2s2` using GParted first so image is actually expanded enough for use case. Trying to create image of microSD card on macOS. – sunknudsen May 16 '21 at 00:19
  • @sunknudsen The image has little free space - just enough to boot. If you tried using it it would rapidly run out of space and lock up. See https://raspberrypi.stackexchange.com/a/44700/8697 for a script sample – Milliways May 16 '21 at 00:24
  • It is rather hard to analyse by inspection. I use [Backup image of SD Card](https://raspberrypi.stackexchange.com/a/103991/8697) to make images, so have no interest in other approaches. – Milliways May 16 '21 at 00:28