Copy Your Linux Install to a Different Partition or Drive

 in

If you need to move your Linux installation to a different hard drive or partition (and keep it working) and your distro uses grub this tech tip is what you need.

To start, get a live CD and boot into it. I prefer Ubuntu for things like this. It has Gparted. Now follow the steps outlined below.

Copying

  • Mount both your source and destination partitions.
  • Run this command from a terminal:
      $ sudo cp -afv /path/to/source/* /path/to/destination
    
    Don’t forget the asterisk after the source path.
  • After the command finishes copying, shut down, remove the source drive, and boot the live CD again.

Configuration

  • Mount your destination drive (or partition).
  • Run the command “gksu gedit” (or use nano or vi).
  • Edit the file /etc/fstab. Change the UUID or device entry with the mount point / (the root partition) to your new drive. You can find your new drive’s (or partition’s) UUID with this command:
      $ ls -l /dev/disk/by-uuid/
    
  • Edit the file /boot/grub/menu.lst. Change the UUID of the appropriate entries at the bottom of the file to the new one.

Install Grub

  • Run sudo grub.
  • At the Grub prompt, type:
      find /boot/grub/menu.lst
    
    This will tell you what your new drive and partition’s number is. (Something like hd(0,0))
  • Type:
      root hd(0,0)
    
    but replace "hd(0,0)" with your partition’s number from above.
  • Type:
      setup hd(0)
    
    but replace "hd(0)" with your drive's number from above. (Omit the comma and the number after it).

That’s it! You should now have a bootable working copy of your source drive on your destination drive! You can use this to move to a different drive, partition, or filesystem.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Would it be possible to backup the system for later restore?

jenriquez's picture

Instead of copying the system to another hard drive, would it be possible to compress it and keep for later restore?

single HD to raid?

Dan Fekete's picture

Would this method also work for transfering a single HD to a raid setup?

I've been wanting to go back to a RAID 0 setup, but don't want to reinstall...

Thanks,
Dan

Correction to grub drive identification syntax

Anonymous's picture

grub uses the form (hd0,0) rather than hd(0,0) to identify a disk partition, and (hd0), not hd(0) to identify a disk.

It did work with WIndows......

Anonymous's picture

Actually, I did manage to do a Windows copy and was pleasantly surprised that it worked.

I typically do Linux FS copies all the time, which is where the idea came from - my Live distro of choice is Knoppix. I was doing a clone of a Windows hard drive (20GB to a new 300+GB) using disk cloning tools, but could not get the newly cloned drive to boot - it seemed that the boot sector or MBR was getting hosed in the copy. This happened with different drives, and on different machines used for the copy so it was not a hardware issue.

I tried a CD install of Windows, which worked and the system booted fine. I then did a disk copy of the old (20GB) windows drive over the new install (which was deleted first) all within the Live session, and lo & behold, the copy booted fine and worked with no issues.

Note though, I was not changing hardware, nor partitions. However, I think if changing partitions, then a simple boot.ini edit may fix it.

Note: when using Knoppix as a Live distro and creating/formatting the partitions, ensure a reboot between formatting (mk*fs) and copying to it, to ensure the new file systems are properly initialized (and synced) before use.

Also, I prefer to use the "tar/cd/tar" combination to ensure that all my permissions come across intact. I think I once read somewhere that it was also more efficient.

After mounting the entire filesystem tree on, say, /mnt/Source (/boot / /home /var /whatever) and the destination on /mnt/Dest (with the new filesystem structure, if needed and possibly additional volumes), I then do the following:
cd /mnt/Source
tar --preserve --sparse --totals -cf - | (cd /mnt/Dest; tar --preserve --totals -xvf -)

Explanation: create a tar archive, preserving permissions, dates times to standard output; pipe it to a tar extraction from standard output, also preserving the permissions, times and dates, but first changing directories to the destination target.
The use of --totals will print a final for both the source copy (tar -cf) and the destination extraction (tar -xvf) for easy comparison that all of the data was copied.
Note the brackets around the entire group of commands after the pipe

Why?

Anonymous's picture

Why not just use dd to copy and then resize the partition using gparted? Works on windows partitions too.

defragmentation

Lars H's picture

Hi, Think this implies getting a very good defrag of partition at the same time. Right??

you are assuming

Anonymous's picture

All the files are in / directory. You need to ask also if say /home or /usr or /var is mounted separately

Parted Magic

John and Dagny Galt's picture

We've really enjoyed Parted Magic and thought it should be mentioned here.

http://www.partedmagic.com/

Sincerely,

John and Dagny Galt

.

Steps I use:

Ridgeland's picture

I clone Linux OS from partition to partition to have a sandbox to test risky software, like testing GRUB2. I was glad it was on a throw-away clone. I keep grub setup pointing to /dev/sda1 and chainlink to all but Slitaz.
Cloning Partitions:
1. Boot a neutal partition of same inode size (128 or 256, tune2fs to see)
2. Gparted --- unmount target partition, format as ext3, and remount (could shred target but I just use format)
3. sudo cp -a /source/* /target/
4. sudo gedit /target/etc/fstab --- 3 lines to change
edit /target/boot/grub/menu.lst - change partition of first stanza
edit sda1's /boot/grub/menu.lst --- copy in the new first stanza - add root (hdx,y)
5. boot into the target partition. no chainlinking yet $ df - to verify correct partition.
6. sudo grub-install /dev/target --- also change the background as a flag.
7. sudo gedit /UserHome/boot/grub/menu.lst --- set up chainlink to /mnt/target.

Thanks for the ls command - I saved a copy of that.

seriously?

zcat's picture

Yes you CAN move a windows install to another drive. Just use ghost, partimage, clonezilla.. But I don't think you can copy it at the filesystem level like this, and you can't move it to a different machine unless they happen to be pretty close to identical without some serious repair work afterward.

Try that with Windows...

Anonymous's picture

It's so easy. Yet it's probably not even possible to move a Windows installation to a new drive.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix