Difference between revisions of "Transfer system disk from SD card to hard disk"

From eLinux.org
Jump to: navigation, search
m (Category changed to common "RaspberryPi". Tidied a bit)
(script with tar (less complex, less risks))
 
Line 1: Line 1:
 
[[Category:RaspberryPi]]
 
[[Category:RaspberryPi]]
[[Category:USB disk]]
 
[[Category:SD card]]
 
  
 
Systems like [[RPi_Hub|Raspberry Pi]] can only boot from an SD card.
 
Systems like [[RPi_Hub|Raspberry Pi]] can only boot from an SD card.
Line 11: Line 9:
 
*the SD card has limited capacity
 
*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.
+
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.
+
We will transfer the complete system disk partition from the SD card to a (USB) hard disk → faster, more stable and fewer risk for [[SD card corruption]].
 +
 
 +
Note:
 +
#As of the [[Raspberry Pi 3]], the system can boot straight from an [[USB disk]] without using an [[SD card]].
 +
#In this migration script device names are only fictional; they will vary on your system.
  
 
==Considerations==
 
==Considerations==
 
At the end, it is not too complicated, if you follow carefully the below script...
 
At the end, it is not too complicated, if you follow carefully the below script...
 
You might do the conversion on a running system...
 
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 system... possibly booting from a live DVD.
+
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==
 
==Copy the partition==
Imagine the system disk is on /dev/mmcblk0p2.
+
You can verify the source partition size as follows:
You can verify the partition sizes as follows:
+
  [[fdisk ]]-l /dev/mmcblk0
  fdisk -l /dev/mmcblk0
+
Suppose the system disk is on /dev/mmcblk0p2.
*Make sure all partitions are unmounted
 
*Create an empty (unformatted) partition big enough on an [[RPi_Adding_USB_Drives|USB hard disk]]; suppose /dev/sda4 with 20 or 30 GB -- You can use GParted
 
*Transfer the system disk:
 
dd bs=4M conv=noerror if=/dev/mmcblk0p2 of=/dev/sda4
 
  
'''Important''': Please note sda4 is just an example! Verify your own partition... The dd command is unforgiving for errors; it can accidentally overwrite any good partition -- you might lose data if you do not use the right command!
+
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...
 
Known what you are doing...
  
 
'''Note''': You could use the same technique to move any other partition from one device to another...
 
'''Note''': You could use the same technique to move any other partition from one device to another...
  
==Configure the new partition==
+
==Get the partition ID==
  gparted
+
  blkid
Right click on the new partition, and ''Check''.
 
This will:
 
*Correct any disk errors from the original SD card partition
 
*Resize the partition to fit its new size (resize2fs)
 
The above dd command duplicated the UUID from the original partition.
 
Every disk UUID must be unique.
 
Therefore you must set a new UUID: right click ''New UUID''
 
  
Now quit the ''gparted'' utility.
+
  /dev/sdx4: UUID="11ed1dc5-0507-48f1-b1c6-d5926df1ee88" TYPE="ext4" LABEL="Raspbian"
 
 
==Get the new UUID==
 
You can possibly assign a new partition name:
 
  e2label /dev/sda4 Raspbian
 
You can obtain the new UUID as follows:
 
blkid
 
 
/dev/sda4: UUID="11ed1dc5-0507-48f1-b1c6-d5926df1ee88" TYPE="ext4" LABEL="Raspbian"
 
  
 
==Change the fstab file==
 
==Change the fstab file==
 
Now prepare the new mount command:
 
Now prepare the new mount command:
  vi /etc/fstab
+
  vi /mnt/sdx4/etc/fstab
 +
 
 
  [before]
 
  [before]
 
  '''/dev/mmcblk0p2'''  /              ext4    defaults,noatime  0      1
 
  '''/dev/mmcblk0p2'''  /              ext4    defaults,noatime  0      1
Line 64: Line 57:
 
==Change the boot configuration==
 
==Change the boot configuration==
 
You must set the new root device:
 
You must set the new root device:
  vi /boot/cmdline.txt
+
  vi /mnt/mmcblk0p1/cmdline.txt
 +
 
 
  [before]
 
  [before]
 
  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/'''mmcblk0p2'''
 
  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/'''mmcblk0p2'''
Line 70: Line 64:
 
   
 
   
 
  [after]
 
  [after]
  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/'''sda4'''
+
  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/'''sdx4'''
 
  rootfstype=ext4 elevator=deadline rootwait
 
  rootfstype=ext4 elevator=deadline rootwait
 
Note that the UUID technique does '''not''' work for the Raspbian boot loader...
 
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==
 
==Reboot==
Line 82: Line 83:
 
==Reuse the previous SD card partition==
 
==Reuse the previous SD card partition==
 
Once the system is running fine, you could reuse the old partition to store data,
 
Once the system is running fine, you could reuse the old partition to store data,
or create a new (small 64 or 128 MB SD card) just bearing the VFAT boot partition.
+
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...
 
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 [[Format a FAT32 SD card on Linux|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/* .
  
 
==Known problems==
 
==Known problems==
 
===SD Card reading error===
 
===SD Card reading error===
 
SD cards are sensitive to I/O errors, especially due to underpower conditions:
 
SD cards are sensitive to I/O errors, especially due to underpower conditions:
  dd: reading '/dev/sdb4': Input/Output error
+
  dd: reading '/dev/mmcblk0': Input/Output error
 
*A file system error occurred
 
*A file system error occurred
 
*Make sure to use the ''dd conv=noerror'' option
 
*Make sure to use the ''dd conv=noerror'' option
 
*Verify that the output size equals the input size
 
*Verify that the output size equals the input size
 
*Always check your target partition with GParted after duplicating
 
*Always check your target partition with GParted after duplicating

Latest revision as of 05:49, 2 November 2019


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 → 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/* .

Known problems

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