Copy Your Linux Install to a Different Partition or Drive
July 9th, 2009 by Ningappa Koneri 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.
__________________________
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Nov-04-09
- Oct-29-09
- Oct-26-09
Recently Popular
From the Magazine
December 2009, #188
If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.
Delicious
Digg
StumbleUpon
Reddit
Facebook








single HD to raid?
On July 13th, 2009 Dan Fekete (not verified) says:
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
On July 13th, 2009 Anonymous (not verified) says:
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......
On July 11th, 2009 Anonymous (not verified) says:
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?
On July 11th, 2009 Anonymous (not verified) says:
Why not just use dd to copy and then resize the partition using gparted? Works on windows partitions too.
defragmentation
On July 11th, 2009 Lars H (not verified) says:
Hi, Think this implies getting a very good defrag of partition at the same time. Right??
you are assuming
On July 10th, 2009 Anonymous (not verified) says:
All the files are in / directory. You need to ask also if say /home or /usr or /var is mounted separately
Parted Magic
On July 10th, 2009 John and Dagny Galt (not verified) says:
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:
On July 10th, 2009 Ridgeland (not verified) says:
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?
On July 9th, 2009 zcat (not verified) says:
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...
On July 9th, 2009 Anonymous (not verified) says:
It's so easy. Yet it's probably not even possible to move a Windows installation to a new drive.
Post new comment