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

From eLinux.org
Jump to: navigation, search
m (Corrections)
m (Category changed to common "RaspberryPi". Tidied a bit)
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
[[Category:Raspberry Pi]]
+
[[Category:RaspberryPi]]
 
[[Category:USB disk]]
 
[[Category:USB disk]]
 
[[Category:SD card]]
 
[[Category:SD card]]
Systems like [[Raspberry Pi]] can only boot from an [[SD card]].
+
 
The initial [[operating system]] is entirely installed on an SD card.
+
Systems like [[RPi_Hub|Raspberry Pi]] can only boot from an SD card.
 +
The initial [[RPi_Distributions|operating system]] is entirely installed on an SD card.
 
This adds to simplicity, but has some limiting disadvantages:
 
This adds to simplicity, but has some limiting disadvantages:
 
*an SD card is slow
 
*an SD card is slow
*it wears out and frequently results in [[I/O error]]s and [[file system]] corruptions.
+
*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
 
*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.
We will transfer the complete system disk 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.
  
 
==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 system... possibly booting from a live DVD.
  
 
==Copy the partition==
 
==Copy the partition==
 
Imagine the system disk is on /dev/mmcblk0p2.
 
Imagine the system disk is on /dev/mmcblk0p2.
 
You can verify the partition sizes as follows:
 
You can verify the partition sizes as follows:
  [[fdisk ]]-l /dev/mmcblk0
+
  fdisk -l /dev/mmcblk0
 
*Make sure all partitions are unmounted
 
*Make sure all partitions are unmounted
*Create an empty (unformatted) partition big enough on an [[USB hard disk]]; suppose /dev/sda4 with 20 or 30 GB -- You can use [[GParted]]
+
*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:
 
*Transfer the system disk:
  [[dd]] bs=4M conv=noerror if=/dev/mmcblk0p2 of=/dev/sda4
+
  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 loose data if you do not use the right command!
+
'''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!
 
Known what you are doing...
 
Known what you are doing...
  
Line 32: Line 34:
  
 
==Configure the new partition==
 
==Configure the new partition==
  [[gparted]]
+
  gparted
 
Right click on the new partition, and ''Check''.
 
Right click on the new partition, and ''Check''.
 
This will:
 
This will:
 
*Correct any disk errors from the original SD card partition
 
*Correct any disk errors from the original SD card partition
*Resize the partition to fit its new size ([[resize2fs]])
+
*Resize the partition to fit its new size (resize2fs)
 
The above dd command duplicated the UUID from the original partition.
 
The above dd command duplicated the UUID from the original partition.
 
Every disk UUID must be unique.
 
Every disk UUID must be unique.
Line 45: Line 47:
 
==Get the new UUID==
 
==Get the new UUID==
 
You can possibly assign a new partition name:
 
You can possibly assign a new partition name:
  [[e2label]] /dev/sda4 Raspbian
+
  e2label /dev/sda4 Raspbian
 
You can obtain the new UUID as follows:
 
You can obtain the new UUID as follows:
  [[blkid]]
+
  blkid
 
   
 
   
 
  /dev/sda4: UUID="11ed1dc5-0507-48f1-b1c6-d5926df1ee88" TYPE="ext4" LABEL="Raspbian"
 
  /dev/sda4: UUID="11ed1dc5-0507-48f1-b1c6-d5926df1ee88" TYPE="ext4" LABEL="Raspbian"
Line 53: Line 55:
 
==Change the fstab file==
 
==Change the fstab file==
 
Now prepare the new mount command:
 
Now prepare the new mount command:
  [[vi]] /etc/fstab
+
  vi /etc/fstab
 
  [before]
 
  [before]
  #/dev/mmcblk0p2  /              ext4    defaults,noatime  0      1
+
  '''/dev/mmcblk0p2''' /              ext4    defaults,noatime  0      1
 
   
 
   
 
  [after]
 
  [after]
  UUID=11ed1dc5-0507-48f1-b1c6-d5926df1ee88      /              ext4    defaults        0 1
+
  '''UUID=11ed1dc5-0507-48f1-b1c6-d5926df1ee88'''       /              ext4    defaults        0 1
  
 
==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 /boot/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 72:
 
  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/'''sda4'''
 
  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...
  
 
==Reboot==
 
==Reboot==
 
Now everything is OK and you can reboot:
 
Now everything is OK and you can reboot:
  [[reboot]]
+
  reboot
 
or
 
or
  [[shutdown]] -r now
+
  shutdown -r now
  
 
==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 64 or 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...
 +
 +
==Known problems==
 +
===SD Card reading error===
 +
SD cards are sensitive to I/O errors, especially due to underpower conditions:
 +
dd: reading '/dev/sdb4': 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

Revision as of 15:06, 12 November 2015


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. We will transfer the complete system disk partition from the SD card to a (USB) hard disk.

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 system... possibly booting from a live DVD.

Copy the partition

Imagine the system disk is on /dev/mmcblk0p2. You can verify the partition sizes as follows:

fdisk -l /dev/mmcblk0
  • Make sure all partitions are unmounted
  • Create an empty (unformatted) partition big enough on an 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! Known what you are doing...

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

Configure the new partition

gparted

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.

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

Now prepare the new mount command:

vi /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 /boot/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/sda4
rootfstype=ext4 elevator=deadline rootwait

Note that the UUID technique does not work for the Raspbian boot loader...

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 64 or 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...

Known problems

SD Card reading error

SD cards are sensitive to I/O errors, especially due to underpower conditions:

dd: reading '/dev/sdb4': 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