Backup, shrink backup and modify MBR of backup
Hi all,
after introducing me, this is my first post. I am experimenting with RaspberryPi, installed Raspbian on a 16Gb SD and expanded to use all SD Card. I use this device, but i think this question is related to General Linux, not for these type of device. Now that all my Rasp is configured (Deluge, noip, vnc server, Wifi SSID,...) i would like to backup the SD card and i would like to be able to restore it to small card (4 Gb, 8 Gb, ...) this backup and i would like to use only Raspberry, not other computer in linux or windows, and then fully or partially automate this process. Have read very much posts in Raspberry forums and in other places (dd, losetup, e2fsck, resize2fs, extract MBR and modify it, etc.) but i don't understand image structure of SD generated with dd tool or from original SD Card. Tested this on Raspberry: a) Dump of all 16Gb SD card to external usb drive: Code:
$ sudo dd bs=1M if=/dev/mmcblk0 of=/media/TOSHIBA/backupRaspRaw.img Code:
$ sudo losetup -f --show /media/TOSHIBA/backupRaspRaw.img Code:
$ sudo fdisk -l /dev/mmcblk0 Code:
$ sudo fdisk -l /dev/loop0 Code:
sudo dd if=/dev/mmcblk0 of=/media/TOSHIBA/originalSD.mbr bs=512 count=1 Code:
sudo dd if=/dev/loop0 of=/media/TOSHIBA/backupRaspRaw.mbr bs=512 count=1 http://www.raspberrypi.org/phpBB3/vi...p?f=91&t=19468 http://sirlagz.net/2012/06/20/how-to...an-image-file/ http://en.wikipedia.org/wiki/Master_boot_record#PTE I understood how to resize second partition of raspbian in loop device (e2fsck, resize2fs) and how to calculate MBR structure and ready to modify it and overwrite in .img file with dd, but i don't understand why information of extracted MBRs is not correct, see following analisys of first 4 bytes of MBR structure of second partition: Code:
Using formula from indicated links: Code:
000001C0 00 00 ....... ........ Tested the same process mounting as a loopback device image of raspbian installation (2013-09-25-wheezy-raspbian.img), studied its MBR and it is correct: Code:
sudo fdisk -l /dev/loop0 My Raspbian is working perfect and its disk structure is not modified by me, only expanded using raspi-config to use all SD Card. What do you think that is wrong in MBR of used SD Card or backup .img file? Should I have something else in mind when i do the backup with dd? Thanks in advance and sorry for this long post! |
Found steps to solve problem
Hello,
after more testing in a Linux VM creating and managing .img disks found steps to solve problem: a) Complete backup of 16 Gb SD Card: Code:
$ sudo dd bs=4M if=/dev/mmcblk0 of=/media/TOSHIBA/16Gb.img Code:
$ sudo losetup -f --show /media/TOSHIBA/16Gb.img Code:
$ sudo fdisk -l /dev/loop0 Byte position 122880 * 512 = 62914560 Code:
$ sudo losetup -f --show -o 62914560 /media/TOSHIBA/16Gb.img Check Disk : Code:
$ sudo e2fsck -p -y -f /dev/loop1 Code:
$ sudo resize2fs -p /dev/loop1 3G Code:
$ sudo losetup -d /dev/loop0 /dev/loop1 Code:
$ sudo fdisk -l /media/TOSHIBA/16Gb.img We will create one image disk with 4 MBytes (at beginning) + 56 MBytes (Fat32 partition) + 3 GBytes (3.072 Mb system partition) that it 3.132 MBytes or 3.284.140.032 bytes, we divide it by bytes per cylinder and obtain number of cylinders to create on disk: 3.284.140.032 / 8.225.280 = 399,27 cylinders We add 1 and we will use 400 that will be 3.137 MBytes approximately Code:
$ dd if=/dev/zero of=/media/TOSHIBA/3Gb.img bs=8225280c count=400 Code:
$ fdisk -u -C400 -S63 -H255 /media/TOSHIBA/3Gb.img Code:
Command (m for help): n Code:
Command (m for help): n Code:
Command (m for help): p Code:
Command (m for help): w h) We will use dd to "copy" first partition from 16Gb image file to 3Gb image file skipping from input file 4 MBytes (8192 sectors of 512 bytes) and seeking 4 MBytes (8192 sectors of 512 bytes) on output file to copy 56 MBytes (114688 sectors of 512 bytes) of first partition: Code:
$ dd if=/media/TOSHIBA/16Gb.img of=/media/TOSHIBA/3Gb.img bs=512 count=114688 skip=8192 seek=8192 Code:
$ dd if=/media/TOSHIBA/16Gb.img of=/media/TOSHIBA/3Gb.img bs=512 count=6291456 skip=122880 seek=122880 k) Copied 3Gb file with ftp/scp to windows and used win32diskimager to restore image to 4 Gb SD Card, Raspbian boots and works perfect! |
All times are GMT -5. The time now is 07:08 PM. |