0

Photo of SD card using <code>diskutil list</code>

Do you know how I could easily and portably (ie: work in windows and mac) take a raspbian disk image from an sd card and create an image that I could send to my colleague via dropbox? I need just the boot partition and the linux partition. It needs to be no more than 2gb if possible.

I would prefer to not use a crazy work around. Just a simple shell script would be nice. Or even an rsync workflow that is understandable lol!

Please help me with this problem so I can go to sleep! lol.

THANK YOU!!!

danielbh
  • 1
  • 3
  • The question is a duplicate because what you really want to do is mount the partitions in the image, recreate them (using a smaller second partition) and `rsync` the contents from the original into the new smaller version. You'll need to use `dd` and `fdisk` to create the new versions, [see here](http://raspberrypi.stackexchange.com/a/29952/5538). Beware that Milliway's scripted version is likely to loose data if the system has been used much since it does not copy the contents, it just copies raw blocks. – goldilocks Mar 26 '16 at 16:03
  • WRT caveats and details about rsync'ing the contents, see: http://raspberrypi.stackexchange.com/a/5492/5538 – goldilocks Mar 26 '16 at 16:07
  • @goldilocks I cannot agree that this " is likely to loose data". Yes `dd` copies/moves blocks but the mapping of these blocks to physical storage is a function of the SD Card firmware (as I am sure you pointed out to me in the past when I raised the issue of data loss with another questioner) – Milliways Mar 26 '16 at 22:43
  • It doesn't matter. The SD card firmware has no concept of what the content of the blocks is, and neither does `dd`. However, the filesystem is structured using them, and *does* assign them specific content. Whether they are real or virtual on the device is irrelevant. So if you have a filesystem 20 blocks in size and you've only used 7, *those 7 may be dispersed*. They are not necessarily 1-7. – goldilocks Mar 26 '16 at 22:51
  • ...However, when you use `dd` and copy half the blocks, you copy 1-10. Again, it does not matter whether those are "real" or "virtual" numbers with respect to the physical device. They are real to filesystem. So if 13, 14, and 19 were 3 of the 7 blocks used, **you just lost 3/7 of the data in the filesystem**. – goldilocks Mar 26 '16 at 22:51
  • @goldilocks Rubbish! both the filesystem and `dd` use the firmware to map blocks to physical storage. No normal userland software has access to the SD internal mapping. Produce some evidence for your outrageous statement. – Milliways Apr 01 '16 at 09:59

2 Answers2

0

you can create a new 2GB ext4 image, mount it, and copy all the linux partition files over. The boot partition probably is as small as it should be. Or if you put the sd card into a ubuntu computer for example, then use gparted, and see if you can resize the linux partition.

el3ien
  • 126
  • 3
  • Im trying to avoid running a vm, I feel thats too much for something that should be so simple. How do i create a ext4 image? Are you able to point me towards a shell script I could use? Thanks! – danielbh Mar 25 '16 at 13:14
  • @danielbh what distro/os are you on? – el3ien Mar 25 '16 at 13:15
  • I use mac or windows. The pi has raspbian. Not sure which os you mean lol – danielbh Mar 25 '16 at 13:16
  • @danielbh you could do it on the raspberry if you have a usb-sdcard adapter, and an extra raspbian sd card. I suppose you can do it on mac too. But I have not tried mac yet. – el3ien Mar 25 '16 at 13:18
  • @danielbh Actually you can do it on raspberry without an extra sdcard. Do you have 2GB+ free space on the linuxpartition? – el3ien Mar 25 '16 at 13:21
  • See above. I would prefer to eject the sd card and put it in one of my computers. But Im open to anything. Not sure how to tell with your quesiton but I posted a picture of my sd card using diskutil list – danielbh Mar 25 '16 at 13:27
  • @danielbh it seems like the image is only 1.3GB + 62.9MB. Try on your mac, type "dd if=/dev/disk2 of=my_new.img bs=4M" and see how big the image is, when you are finished – el3ien Mar 25 '16 at 13:30
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/37516/discussion-between-danielbh-and-el3phanten). – danielbh Mar 25 '16 at 13:37
-1

I just realised that provided you don't need to resize any partitions you can actually do it on the Mac.

WARNING Make doubly sure that you don't accidentally write in the wrong place.

The following script I wrote to rearrange Ubuntu MATE partitions and you should be able to adapt this to copy your data.

NOTE All care BUT no responsibility, and you really need to understand partitioning!

#!/bin/bash
# script to create a Ubunutu MATE image with properly aligned partitions
# 2015-11-06

echo
#INPUT_IMG="ubuntu-mate-15.10-desktop-armhf-raspberry-pi-2.img"
INPUT_IMG="PiMate1510back20151106_small.img"
OUTPUT_IMG="ubuntu-mate-15.10.img"

# Partition details of input image
P1START=2048
P1SIZE=131072

P2START=133120
P2SIZE=7546880

# Partition details of output image
P1NEW=8192
P2NEW=139264

# End of user configuration
#let IMG_END=$P2NEW+$P2SIZE+140000
let IMG_END=$P2NEW+$P2SIZE
echo -e $P1START - $P1NEW $P1SIZE
echo -e $P2START - $P2NEW $P2SIZE
echo -e $IMG_END

# Create an empty image file
dd if=/dev/zero of=$OUTPUT_IMG count=$IMG_END

# Create partitions
echo -e "
e 1
0C
n
$P1NEW
$P1SIZE
e 2
83
n
$P2NEW
$P2SIZE
quit" | fdisk -e  $OUTPUT_IMG

echo "Finished Create partitions"
# Copy partitions
dd if=$INPUT_IMG skip=$P1START of=$OUTPUT_IMG seek=$P1NEW count=$P1SIZE
dd if=$INPUT_IMG skip=$P2START of=$OUTPUT_IMG seek=$P2NEW count=$P2SIZE

#fdisk $OUTPUT_IMG
Milliways
  • 54,718
  • 26
  • 92
  • 182
  • Ok thanks. It's late here. I'll try this tomorrow. Your help is much appreciated! – danielbh Mar 26 '16 at 00:25
  • I ended up caving and just installing ubuntu. It seems like the smarter choice when working with the raspbi. Thanks for your help though! – danielbh Mar 26 '16 at 14:59