0

This question already has an answer here:

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!!!

marked as duplicate by goldilocks Mar 26 '16 at 15:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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. 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: 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
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.

  • 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
0

There is no simple way to do what you want. It can be done on a Linux machine (not Windows or Mac) but requires a detailed knowledge of Linux partition management.

I suggest you use the following OS X script to image the card and compress. This is as small as you can get.

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz
  • I know its not simple. I still want to figure it out. I'm aware of your method and its clever, but not what I'm looking for. Thanks though. – danielbh Mar 25 '16 at 23:38
  • Why do you think it can't be done on a windows or mac by the way? :) – danielbh Mar 25 '16 at 23:49
  • @danielbh because neither can support ext4. There is 3rd party software, but even this doesn't seem to work on El Capitan. – Milliways Mar 25 '16 at 23:51
  • Ok so is there a way to do it without installing a VM? What in your mind is the very easiest way to accomplish a 2gb disk image that I can distribute hundreds of times to different pis. I need to send it via internet not with an sd card. – danielbh Mar 25 '16 at 23:55
  • I see your point about ext4 and it seems like good one. – danielbh Mar 25 '16 at 23:56
-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
  • Ok thanks. It's late here. I'll try this tomorrow. Your help is much appreciated! – danielbh Mar 26 '16 at 0: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

Not the answer you're looking for? Browse other questions tagged or ask your own question.