5

I made a backup of an SD card with DD. the SD card was 16Gb in size, the backup was completed using the following command:

sudo bs=4M dd if=/dev/disk2 | gzip > pi_sd_prayertime.gz

pi_sd_prayertime.gz size is : 1GB

I would like to restore this backup to a smaller 4GB SD card. I have tried numerous attempts but the restored sd card is not recognized and is unreadable at the end.

I do understand that the size difference is the issue, here is what I have attempted so far:

sudo dd bs=1m if=pi_sd_prayertime.gz of=/dev/disk2 count=31116288

Note that 31116288 is (the sector end + 1) of the existing disk where the original backup is created from

6

After reading all of this and many other opinions, I tried rpi-clone by Bill Wilson. It worked perfectly first try and was actually much easier than the instructions made it sound. Thanks myhd for recommending it.

  • 1
    Er, why not accepting myhd's answer then, rather than posting your own? – Arjan Feb 25 '18 at 14:00
  • @arjan agreed tldr :( – myhd Mar 21 '18 at 20:17
10

It indeed is possible, with rpi-clone by Bill Wilson. You will need an USB CF card reader to attach to your RPi. Obviously, the actual memory used on the original SD card must be smaller that the space on the target SD card.

The basic idea is to download rpi-clone and use it from a booted RPi to (prepare and) copy to the target SD card in the reader. One genius aspect is that all required partitions are created on the target SD card if necessary.

After quick discouragement by the other answers on this page, I came back here to report that I have tested this myself, and cloned a 16 GB SD card (2.2 GB used!) to a 4 GB card, which actually boots and works like a charm.

2

You'll have to resize the partition inside the image, as long as the amount of real data is less than 4GB.

You could do that on a different linux machine. Another Pi would be slightly difficult, as you need enough disk space to extract the entire image. Though you could use an external disk or large usb-stick.

First mount the extracted file. Then shrink the ext4 filesystem on the second partition using e2resize. This will move all the files to the beginning of the partition. Next resize the second partition, using e.g. gparted. You'll have to calculate the appropriate values, as 4GB isn't always the same on all SD cards. Once the partition is resized, you could expand the filesystem again to fill the entire (now smaller) partition. Then truncate the the image file, or just copy the first 4GB to the other SD.

  • is there a similar e2resize for osx – Ossama Dec 31 '13 at 20:18
  • Mac doesn't support the ext4 filesystem. I don't think FUSE has tools like e2resize. Sorry. – Gerben Jan 1 '14 at 16:26
1

in short -- you cannot pour 16 liters of water into a quarter bottle.

please, use 16GB SD card to restore the original image and then copy the necessary files off that card to your new 4GB one.

also, you may unpack the image file and try to mount it right off the HDD image, provided you have a working linux box and the knowledge how to do it. still, if you don't, stick with "copy back to 16GB card" method, that's very simple and proven to work well.

  • even though the size of the image is 1G – Ossama Dec 31 '13 at 7:26
  • 1
    @Ossama: the compressed size of the image is 1GB. It will still decompress to 16GB. – me-- Dec 31 '13 at 8:10
  • do I have to decompress the file prior to dd it. is the command above ok – Ossama Dec 31 '13 at 8:27
  • 1
    definitely you have to decompress. writing compressed image to SD card gives you nothing. command is incorrect, you should use gunzip -c your_image.gz | dd of=/dev/disk2 bs=10M or something like that. – lenik Dec 31 '13 at 19:38
0

The point is may not be the total amount of data, is could be what you started with and what you now have (or need). I started with a 32GB SD card. I got it all set up and everything is working perfectly. Looking at the storage, my whole setup will easily fit on an 8GB SD card (with plenty of room to store things). However, I don't want to go through the entire setup process again. I just want to take my image (the the 32GB SD card) and move it to an 8GB card (which will be more than large enough to host my application). So, I'm not trying to put 16 liters into a quart (you swapped units of measure on me) - it is that I wanted to start with an SD card that I knew had more than enough capacity. Once I tested for a while, I found that the application would easily run on a 8GB SD. However, I would rather not go through the entire process of setting everything back up again. The 32GB card has lots of 'open' space. So, I would like to create an 8GB card with the OS and data contained on the 32GB card (rather than going through the whole process of starting from scratch with a bare OS and loading all of my programs and data again).

If that isn't possible, I might just bite the bullet and buy a bunch of 32GB SD cards (a bit more money, but (maybe) less hassle).

0

Use pishrink to squeeze your image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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