Transfer system disk from SD card to hard disk

Systems like Raspberry Pi can only boot from an SD card. The initial operating system is entirely installed on an SD card. This adds to simplicity, but has some limiting disadvantages:
 * an SD card is slow
 * it wears out and frequently results in I/O errors and file system corruptions.
 * it can get corrupt after power-failure or underpower conditions
 * the SD card has limited capacity

Therefore we want to use the SD card only for the W95 FAT32 partition containing the initial boot configuration (/boot). We will transfer the complete system disk partition from the SD card to a (USB) hard disk &rarr; faster, more stable and fewer risk for SD card corruption.

Note:
 * 1) As of the Raspberry Pi 3, the system can boot straight from an USB disk without using an SD card.
 * 2) In this migration script device names are only fictional; they will vary on your system.

Considerations
At the end, it is not too complicated, if you follow carefully the below script... You might do the conversion on a running system... If you would be completely sure that the system disk is transferred without error, you might temporarily mount the source and target disk on another (Linux) system... possibly booting from a live DVD.

Configure the new partition
gparted Create an empty partition on an USB hard disk big enough to contain the system disk that is currently on the SD card; suppose /dev/sdx4 with 20 or 30 GB.

Copy the partition
You can verify the source partition size as follows: fdisk -l /dev/mmcblk0 Suppose the system disk is on /dev/mmcblk0p2.

Transfer the system disk: cd /mnt/mmcblk0p2

time tar -S -cf -. |tar -C /mnt/sdx4 -xf -

Important: Please note sdx4 is just an example! Verify your own partitions... The tar command is unforgiving for errors; it can accidentally overwrite any good partition -- you might lose data if you do not use the right command! Known what you are doing...

Note: You could use the same technique to move any other partition from one device to another...

Get the partition ID
blkid

/dev/sdx4: UUID="11ed1dc5-0507-48f1-b1c6-d5926df1ee88" TYPE="ext4" LABEL="Raspbian"

Change the fstab file
Now prepare the new mount command: vi /mnt/sdx4/etc/fstab

[before] /dev/mmcblk0p2 /               ext4    defaults,noatime  0       1 [after] UUID=11ed1dc5-0507-48f1-b1c6-d5926df1ee88      /               ext4    defaults        0 1

Change the boot configuration
You must set the new root device: vi /mnt/mmcblk0p1/cmdline.txt

[before] dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait [after] dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sdx4 rootfstype=ext4 elevator=deadline rootwait Note that the UUID technique does not work for the Raspbian boot loader... use the physical name. You can use blkid to get the real device name. Device names can change depending on the USB port used; or the sequence of mounting. On new Raspbian systems you can better use PARTUUID, to be independent from the (dynamic) device naming.

Remark that you can always switch back and forth between the system disk on the SD card or the USB disk drive by editing the root device in /boot/cmdline.txt.

Reboot
Now everything is OK and you can reboot: reboot or shutdown -r now

Reuse the previous SD card partition
Once the system is running fine, you could reuse the old partition to store data, or create a new (small 128 MB SD card) just bearing the VFAT boot partition. Then you can reuse the previous 4, 8, 16, or 32 GB card for other purposes...

Transfer the boot sector
First format the SD card on a Windows system. You might use a Linux system as well. Then transfer the boot sector (the first 446 bytes of the boot block); so you do not overwrite the partition table. Use a working boot SD card to get the right boot loader.

Pay attention not to loose data. You might work in 2 steps; first copy the boot sector to an intermediate file. dd bs=446 count=1 if=/dev/mmcblk0 of=/dev/sdx

Copy the boot configuration
cd /mnt/sdx1 cp -a /boot/*.

SD Card reading error
SD cards are sensitive to I/O errors, especially due to underpower conditions: dd: reading '/dev/mmcblk0': Input/Output error
 * A file system error occurred
 * Make sure to use the dd conv=noerror option
 * Verify that the output size equals the input size
 * Always check your target partition with GParted after duplicating