User avatar
Super-Nathan
Posts: 67
Joined: Sun Jun 17, 2012 6:56 am
Location: South Korea
Contact: Website

dd to backup image

Sun Sep 23, 2012 9:29 am

I have a 32 gig sd card, but only 3.2 gigs are used. I want to dd to a backup image that can be loaded on a disk as small as 4 gigs (less than the used size) is there a way to do this short of starting over on a 4 gig chip?
echo "Something Useful" > /dev/null

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: dd to backup image

Sun Sep 23, 2012 2:20 pm

Put it in another Linux box, squeeze the partition so it will fit on a 4 Gb card with Gparted. Then copy it onto a 4Gb card (probably the 2 partions seperately to avoid dd making a meal of it). Then dd it to a file. If you do it whilst its still on the 32 Gb card with the cut down partition, dumb ol' dd will make an image 32 Gb long.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
Super-Nathan
Posts: 67
Joined: Sun Jun 17, 2012 6:56 am
Location: South Korea
Contact: Website

Re: dd to backup image

Mon Sep 24, 2012 2:30 am

You just confirmed, what I thought. Thanks!
echo "Something Useful" > /dev/null

tward800
Posts: 1
Joined: Fri Sep 14, 2012 6:26 pm

Re: dd to backup image

Fri Sep 28, 2012 5:24 am

Here's what I did when my new 16GB SD card came up smaller than my original one. Maybe not the best way but it worked. I reduced the image to 4GB. That leaves me with file systems about 50% full.

Based upon info from
http://sirlagz.net/2012/06/20/how-to-re ... mage-file/

Save a copy of the SD in case things don't work out. I used Win32DiskImager, but that image is 16GB.

Before starting to copy the SD card I reduced the file system on partition two and then reduced partition two.

First reduce file system to something close to what you want from another system. I used another Linux system with the SD card in a SD to USB adapter. I think this could done on the rpi with the OS running from another SD. I reduced the root file system to a bit below 4GB

Code: Select all

resize2fs -p /dev/sda2 3891M
Then I used fdisk to remove partition 2 and recreate it as 4GB

Then expanded file system to use new partition

Code: Select all

resize2fs -p /dev/sda2
Create an empty image file the size you need. This holds the copy from the SD card. Again on another Linux system.

Code: Select all

[root@box]# dd bs=1M count=3850 if=/dev/zero of=4Grpi.img
3850+0 records in
3850+0 records out
4037017600 bytes (4.0 GB) copied, 80.3155 seconds, 50.3 MB/s
[root@box]# ls -l 4Grpi.img
-rw-r--r-- 1 root root 4037017600 Sep 14 13:36 4Grpi.img
Mount it as loopback

Code: Select all

[root@box]# losetup /dev/loop0 4Grpi.img
[root@box]# losetup /dev/loop0
/dev/loop0: [fd00]:1737392 (4Grpi.img)
dd the SD card to the image file through the loop back

Code: Select all

[root@box]# dd bs=1024 if=/dev/sda of=/dev/loop0
dd: writing `/dev/loop0': No space left on device
3942401+0 records in
3942400+0 records out
4037017600 bytes (4.0 GB) copied, 313.95 seconds, 12.9 MB/s
If you dd direct to the file it will grow to the size of the SD card.
Dumping the SD card to the 4GB image file though the loopback limits the size to that of the file.

I copied the image file, 4Grpi.img, over to my Windows PC and used Win32DiskImager to write it to the new SD card. Hope this helps someone.

User avatar
jojopi
Posts: 3233
Joined: Tue Oct 11, 2011 8:38 pm

Re: dd to backup image

Fri Sep 28, 2012 10:48 am

tward800 wrote:[root@box]# dd bs=1024 if=/dev/sda of=/dev/loop0
dd: writing `/dev/loop0': No space left on device
3942401+0 records in
3942400+0 records out
4037017600 bytes (4.0 GB) copied, 313.95 seconds, 12.9 MB/s
The problem with this step is that we expect it to end with the "no space" error, because the new device is smaller than the old one. So it is hard to tell whether the important data has fitted or not.

You have not quoted all the figures you used. But you say that you originally shrunk the second filesystem to 3891MiB, and later grew it back slightly. And this filesystem normally starts 60MiB into the device. Your new device is 3850MiB, so I believe that it is at least 101MiB too small.

Make sure that the biggest "End" value in the output of "fdisk -lu /dev/sda", plus 1, times 512, is not greater than the number of bytes copied.

Also, 3850MiB will fit on some 4GB cards, but not all. To be more conservative, I would recommend aiming for 925MiB per nominal GB, that is 3700MiB total size giving a 3640MiB second partition.

Return to “Beginners”