Did you ever want to flash your root partition straight to USB stick/HDD/SDD instead of first
dd-ing it onto SD Card and then coping it again onto another media?
I got bored of the double work that is provided by RPi geeks. Yeah, it's fine if you're a total beginner (though one might get lost pretty easily) and want to use only the SD Card. But what if you know how to deal with Raspberry Pi already and perform the card setup mechanically, coping the root partition to a USB stick before you run the system? There is an easy way to do it, less time consuming and faster. It involves using loop-mounting partitions from a disk image.
Loop mounting allows us to access the partitions from disk image (all the Rapsberry Pi distributions are in fact disk images) without the need of writing them onto SD card. Let's have a look at the output of fdisk run on Raspbian image:
% sudo fdisk -lu 2013-09-25-wheezy-raspbian.img Disk 2013-09-25-wheezy-raspbian.img: 2962 MB, 2962227200 bytes 255 heads, 63 sectors/track, 360 cylinders, total 5785600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0002c262 Device Boot Start End Blocks Id System 2013-09-25-wheezy-raspbian.img1 8192 122879 57344 c W95 FAT32 (LBA) 2013-09-25-wheezy-raspbian.img2 122880 5785599 2831360 83 Linux
As you can see, the
.img file is nothing but a disk image, containing separate partitions, starting at block
122880 (we'll need those numbers later on). But you wouldn't be able to mount them with using the
<strong>mount</strong> command alone in Linux.
The first important data are the units, which are stated to be
512 bytes per sector. We take note of this value as the factor for use in the next operation.
Now, let's access the second partition which is
122880 sectors into the disk, according to
fdisk output. We know that each sector is 512 bytes:
Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes
so we are going to pass this number to
% sudo losetup /dev/loop0 2013-09-25-wheezy-raspbian.img -o $((122880 * 512)) % sudo file -s /dev/loop0 /dev/loop0: sticky Linux rev 1.0 ext4 filesystem data, UUID=548da502-ebde-45c0-9ab2-de5e2431ee0b (extents) (large files)
file -s command, as you can see, shows us the type of files on the disk image. Now we can use
mount to mount the partition in our filesystem:
% sudo mount /dev/loop0 /mnt
Let's have a look if the image has mounted:
% ls -al /mnt/ total 100 drwxr-xr-x 22 root root 4096 Sep 25 22:19 ./ drwxr-xr-x 6 vivel vivek 4096 Nov 7 23:25 ../ drwxr-xr-x 2 root root 4096 Sep 25 21:56 bin/ drwxr-xr-x 2 root root 4096 Sep 25 21:26 boot/ drwxr-xr-x 3 root root 4096 Sep 25 21:57 dev/ drwxr-xr-x 97 root root 4096 Sep 25 23:08 etc/ drwxr-xr-x 3 root root 4096 Sep 25 21:22 home/ drwxr-xr-x 12 root root 4096 Sep 25 21:46 lib/ drwx------ 2 root root 16384 Sep 25 21:07 lost+found/ drwxr-xr-x 2 root root 4096 Sep 25 21:11 media/ drwxr-xr-x 2 root root 4096 Aug 19 19:38 mnt/ drwxr-xr-x 4 root root 4096 Sep 25 22:14 opt/ drwxr-xr-x 2 root root 4096 Aug 19 19:38 proc/ drwx------ 2 root root 4096 Sep 25 21:11 root/ drwxr-xr-x 7 root root 4096 Sep 25 22:14 run/ drwxr-xr-x 2 root root 4096 Sep 25 21:56 sbin/ drwxr-xr-x 2 root root 4096 Jun 20 2012 selinux/ drwxr-xr-x 2 root root 4096 Sep 25 21:11 srv/ drwxr-xr-x 2 root root 4096 Feb 26 2013 sys/ drwxrwxrwt 2 root root 4096 Aug 19 19:38 tmp/ drwxr-xr-x 10 root root 4096 Sep 25 21:11 usr/ drwxr-xr-x 11 root root 4096 Sep 25 21:11 var/
If the partition holds normal filesystem, as in our case, you can combine the
<strong>mount</strong> set up the loop device, and manage it automatically:
% sudo mount -o loop,offset=$((10860003 * 512)) 2013-09-25-wheezy-raspbian.img /mnt
After performing some operations with the mounted image, to umount it do:
% sudo umount /mnt % sudo losetup -d /dev/loop0
How to copy the contents onto desired storage?
This is really up to you. You can use just any copy command, like
cp (head here for more information: https://aikiwolfie.blogspot.co.uk/2009/09/ubuntu-tip-simple-backup-command-using.html), but I prefer to use
rsync. Remember to prepare the destination disk by formatting it and creating an ext4 filesystem partition (it has to be the same as the image partition). When cloning files, do not overwrite the
rsync, there is no need of excluding
tmp directories, as we're operating on a mounted image, not a live system:
% sudo rsync -aAXv /mnt/* /path/to/USB/folder --exclude=/lost+found
After the operation is finished, the only thing you might want to do is to pre-configure the system (manually configuring network, ssh, etc.),
cmdline.txt on your boot partition to recognise the USB media as root partition (changing the
root=/dev/sda1) as well as modifying
/etc/fstab by adding:
/dev/sda1 / ext4 defaults,noatime 0 0
That's faster and a lot easier.