Search site

Add to Google

Featured Articles
Workshop 1: clone NTFS and Linux partitions into a smaller disk back
Previous article: Workshops for NTFS and Linux disk cloning and partition resizing view
Next article: Workshop 1: Post installation problems for cloned Ubuntu drives view

Winubu-01

You start off with having a 60GB hard drive with Windows XP on it. Let's make this disk your first drive i.e. /dev/sda. The target disk will then be your second disk, namely /dev/sdb. The steps below starts from scratch, but feel free to skip to the relevant bit if your starting point is different.

It is much more challenging to squeeze the partitions into a smaller disk, so I will be covering that here. If you are copying the partitions to a larger disk without performing OS level resizing, the worst will be that you are wasting the extra space as the OS does not make use of it, but at least it will boot. It will not be true if the partition is smaller, as your OS will be truncated, and that is never a good thing, be it Windows or Linux.

OK, to start with, we have a 60GB disk, as shown in the fdisk output below

$ sudo fdisk -ul /dev/sda

Disk /dev/sda: 60.0 GB, 60011642880 bytes
64 heads, 32 sectors/track, 57231 cylinders, total 117210240 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x000ca9dd

Device Boot Start End Blocks Id System
/dev/sda1 * 63 117210239 58605087 7 HPFS/NTFS
$

Note that it is very important that you use the -u switch to fdisk, as this will display the beginning and end blocks in sectors rather than cyclinders. Why is this important ? Well, it is crucial that when you allocate the partitions in the target disk later, you have to make sure the Start offset of the first partition (i.e. /dev/sdb1) is the same on the target disk as in the source disk. Getting the Start value incorrect will of course render the NTFS partition completely useless, as this value is stored in the BIOS Parameter Block, or BPB of the NTFS partition. Of course, you can manually poke this value into the correct offset of the BPB, as described here, but this bit of bypass surgery is too hair raising for me, I prefer not having to tackle it. As you have already noticed, the Start offset of the first partition does not actually start at 1. I notice that, if you let fdisk decide, this value seems to depend on the size of the disk (for 40GB it is 63, but for a 30GB disk it is 32).

Stick a Ubuntu (or a Unix distro of your choice) into the CD drive and complete the installation, with Ubuntu roughly occupying half the space behind Windows. Hopefully, once everything is done, you will end up with the partition table looking something like

$ sudo fdisk -ul /dev/sda

Disk /dev/sda: 60.0 GB, 60011642880 bytes
64 heads, 32 sectors/track, 57231 cylinders, total 117210240 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0004fea4

Device Boot Start End Blocks Id System
/dev/sda1 * 63 60546907 30273422+ 7 HPFS/NTFS
/dev/sda2 60546908 115234408 27343750+ 83 Linux
/dev/sda3 115234409 117210239 987915+ 82 Linux swap / Solaris
$

If you do not, and believe me, sometimes you do not (I discovered this after installing Ubuntu 8.10 Intrepid Ibex), then that is covered in another workshop

Once you have installed Ubuntu and updated both Windows and Ubuntu, then the disk is ready for cloning. You use this disk as the basis to clone to another larger or smaller disk (you can skip certain steps below if you are simply cloning to a disk of the same size). I find that it is still more worthwhile doing the below compared to simply using dd to copy the contents of the first disk to the second, specially for larger size disks. The procedure described here simply copies the contents and not the empty spaces, so should speed up the process significantly

Create the partitions on the target disk

Let's make this challenging, let's say the target disk is only 40GB, and I want to fit NTFS into the first 20GB, with Ubuntu occupying the remaining 20GB

First we create the NTFS partition. The responses are hightlighted in bold below

$ sudo fdisk -u /dev/sdb
The number of cylinders for this disk is set to 4864.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p
Disk /dev/sdb: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders, total 78140160 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0004fea4

Device Boot Start End Blocks Id System

Now we create the 20GB NTFS partition

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-78140159, default 63): 63
Last sector or +size or +sizeM or +sizeK (63-78140159, default 78140159): +20000M

Note that the value in red above must be the same as the Start offset of /dev/sda1. Now let's make /dev/sdb1 an NTFS partition by toggling the partition type to 7 (by default, all partitions are created as Linux, code 83)

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 7
Changed system type of partition 1 to 7 (HPFS/NTFS)

Now create the Linux partition for /dev/sdb2, which should be the rest of the space, but minus around 500MB for the Linux swap space

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First sector (39062564-78140159, default 39062564):
Using default value 39062564
Last sector or +size or +sizeM or +sizeK (39062564-78140159, default 78140159): +19500M

Now create the Linux swap partition, which should be simply the remaining space. fdisk would have helpfully calculated this for you, so just choose the default values

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First sector (77148503-78140159, default 77148503):
Using default value 77148503
Last sector or +size or +sizeM or +sizeK (77148503-78140159, default 78140159):
Using default value 78140159

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 82
Changed system type of partition 3 to 82 (Linux swap / Solaris)

Now toggle the active flag on /dev/sdb1 (the first partition which houses our NTFS file system) to make it bootable

Command (m for help): a
Partition number (1-4): 1

Let's check to see if our partition table looks correct

Command (m for help): p

Disk /dev/sdb: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders, total 78140160 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x0004fea4

Device Boot Start End Blocks Id System
/dev/sdb1 * 63 39062563 19531250+ 7 HPFS/NTFS
/dev/sdb2 39062564 77148502 19042969+ 83 Linux
/dev/sdb3 77148503 78140159 495828+ 82 Linux swap/ Solaris

Finally, write out the new partition table. This is the point of no return for /dev/sdb

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
$

At this point, the partitions are created on /dev/sdb and their types marked accordingly. However, the partitions are not yet formatted and contain no data either. If you try to mount them at this point as File Systems, the mounter will complain about missing NTFS signature for NTFS and missing superblock for Linux. What we want here is to perform OS aware copying of data from the respective partitions in /dev/sda into /dev/sdb (i.e. /dev/sda1 into /dev/sdb1 and /dev/sda2 into /dev/sdb2). You can not use dd because the source and target partitions have different sizes. Here is where we take two different approaches between NTFS and Linux

NTFS partition cloning

For NTFS, we use ntfsclone, but first we have to inform the partition that it now has less space to play with (because ntfsclone refuses to clone a partition into smaller space). You do this with ntfsresize. Unfortunately, this has to be done on the source partition. As resizing is pretty quick, you can simply resize it back once you are done with the cloning. The commmand is:

$ sudo ntfsresize --size 20G /dev/sda1
ntfsresize v2.0.0 (libntfs 10:0:0)
Device name : /dev/sda1
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 29999993344 bytes (30000 MB)
Current device size: 30999984640 bytes (31000 MB)
New volume size : 19999994368 bytes (20000 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use : 7453 MB (24.8%)
Collecting resizing constraints ...
Needed relocations : 131789 (540 MB)
WARNING: Every sanity check passed and only the dangerous operations left.
Make sure that important data has been backed up! Power outage or computer
crash may result major data loss!
Are you sure you want to proceed (y/[n])? y
Schedule chkdsk for NTFS consistency check at Windows boot time ...
Resetting $LogFile ... (this might take a while)
Relocating needed data ...
100.00 percent completed Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
Syncing device ...
Successfully resized NTFS on device '/dev/sda1'.
You can go on to shrink the device for example with Linux fdisk.
IMPORTANT: When recreating the partition, make sure that you
1) create it at the same disk sector (use sector as the unit!)
2) create it with the same partition type (usually 7, HPFS/NTFS)
3) do not make it smaller than the new NTFS filesystem size
4) set the bootable flag for the partition if it existed before
Otherwise you won't be able to access NTFS or can't boot from the disk!
If you make a mistake and don't have a partition table backup then you
can recover the partition table by TestDisk or Parted's rescue mode.
$

You can add the -n option to perform a test resize if you do not want to jump in head first

Now we are ready to clone the partition. The command is

$ sudo ntfsclone --overwrite /dev/sdb1 /dev/sda1
ntfsclone v2.0.0 (libntfs 10:0:0)
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 19999993856 bytes (20000 MB)
Current device size: 30999984640 bytes (31000 MB)
Scanning volume ...
100.00 percent completed
Accounting clusters ...
Space in use : 7453 MB (37.3%)
Cloning NTFS ...
$

NOTE that for this command, the target comes before the source, so be very very careful. If you get the arguments the wrong way round, you kiss good bye to all the hard work you have just put into so far, not to mention all that lovely data in /dev/sda1. As you can see, only 7.5GB of data needed to be copied, which is much quicker than using dd for the whole partition.

Now you can resize the source partition back to its original size

$ sudo ntfsresize --size 30G /dev/sda1
$

As I mentioned before, NTFS is not very relocatable, so it is better to go with the flow and simply create the partition /dev/sdb1 at the same offset from the beginning of the disk as in /dev/sda1. If you do not do this, your Windows partition will not boot, unless you manually poke the new offset into the BPB sector as mentioned at the beginning of this article

Linux (ext3) partition cloning

Linux is far more relocatable than Windows (at least for me anyway), so we can simply format the Linux partition and sync up the contents. After that, we touch up the boot loader and we are done

First, we format the partition

$ sudo mke2fs -j /dev/sdb2
$

The -j parameter tells the formatter to create a journal for the ext2 file system. This is good for recovery purposes. Next, we need to prepare /dev/sdb3 to be the swap partition, or it will not be given a UUID which we will need later

$ sudo mkswap /dev/sdb3
Setting up swapspace version 1, size = 507723 kB
no label, UUID=f1550ad4-37e8-4ee4-8d6a-2956f4b787fd
$

Once this is done, you can already mount the partition, although it will have no file at all. We need to sync up the contents of /dev/sda2 into /dev/sdb2, but we will let the file system decide on where to put the files. To do the sync, we must first mount the source and target partitions. After the sync, we can unmount the source, but not the target, as we still need to prepare the boot loader

$ sudo mkdir /mnt/source /mnt/target
$ sudo mount /dev/sda2 /mnt/source
$ sudo mount /dev/sdb2 /mnt/target
$ sudo rsync -az /mnt/source/ /mnt/target/
$ sudo umount /mnt/source
$

Note the trailing / characters hightlighted in red in the rsync command. You need to have them or rsync will place the wrong contents into the target file system

Now to touch up the target partition to give it an identity. You need to give the machine where you deploy the target disk a new name. Let's call it spock. If you do not do this, the machine will boot up with the same machine name as the source machine, which is not a good thing on the same network

$ sudo echo 'spock' > /mnt/target/etc/hostname
$

One final thing is to change the UUID of the new partition in the Grub boot menu file, which is /mnt/target/boot/grub/menu.lst, and also the fstab file at /mnt/target/etc/fstab and the file /mnt/target/etc/initramfs-tools/conf.d/resume which affects the splash screen upon bootup. You need to load it into textedit and perform a global search and replace of all the UUIDs of /dev/sda2 with that of /dev/sdb2, and of /dev/sda3 with that of /dev/sdb3, or you can use sed as shown below. How do you find out these UUID's ? The command is blkid

$ sudo su
# blkid /dev/sda2
/dev/sda2: UUID="17a03d74-2579-49bb-890f-c038f8868037" TYPE="ext3"
# blkid /dev/sdb2
/dev/sdb2: UUID="a7a93d64-1770-48b1-99af-30acf8e6903f" TYPE="ext3"
# blkid /dev/sda3
/dev/sda3: TYPE="swap" LABEL="Swap" UUID="71460ad4-3d32-4ab1-835a-19c6f0a797cc"
# blkid /dev/sdb3
/dev/sdb3: TYPE="swap" LABEL="Swap" UUID="f1550ad4-37e8-4ee4-8d6a-2956f4b787fd"
# cd /mnt/target/boot/grub
# cp menu.lst menu.lst.orig
# cat menu.lst.orig | sed -e 's/17a03d74-2579-49bb-890f-c038f8868037/a7a93d64-1770-48b1-99af-30acf8e6903f/g' > menu.lst
# cd /mnt/target/etc
# cp fstab fstab.orig
# cat fstab.orig | sed -e 's/17a03d74-2579-49bb-890f-c038f8868037/a7a93d64-1770-48b1-99af-30acf8e6903f/g' -e 's/71460ad4-3d32-4ab1-835a-19c6f0a797cc/f1550ad4-37e8-4ee4-8d6a-2956f4b787fd/g' > fstab
# cp initramfs-tools/conf.d/resume initramfs-tools/conf.d/resume.orig
# cat initramfs-tools/conf.d/resume.orig | sed -e 's/71460ad4-3d32-4ab1-835a-19c6f0a797cc/f1550ad4-37e8-4ee4-8d6a-2956f4b787fd/g' > initramfs-tools/conf.d/resume
# exit
$

The original menu.lst is copied into menu.lst.orig, fstab into fstab.orig, and resume into resume.orig, in case you need to switch back for whatever reason.

Prepare the boot loader

If you are using Windows as the boot loader, the command is

$ sudo dd if=/dev/sda of=/dev/sdb bs=446 count=1
$

This is equivalent to the fdisk /mbr command under Windows, which essentially copies the Master Boot Record from the source disk into the target disk. Refer to the figure at the end of this article to find out where it is on a disk. If you are using the Grub boot loader from the Ubuntu installation then the command is (don't worry too much about the scary output)

$ sudo grub-install --root-directory=/mnt/target /dev/sdb
Due to a bug in xfs_freeze, the following command might produce a segmentation
fault when /mnt/target/boot/grub is not in an XFS filesystem. This error is harmless and
can be ignored.
xfs_freeze: specified file ["/mnt/target/boot/grub"] is not on an XFS filesystem
Installation finished. No error reported.
This is the contents of the device map /mnt/target/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(hd0)   /dev/sda

$

Don't forget to unmount the target before rebooting, as a matter of tidiness.

$ sudo umount /mnt/target
$

That should be all. You should now be able to deploy the target disk into a different machine and boot up into either Ubuntu or Windows

Footnote: If you do not see the Ubuntu splash screen when you boot up with the cloned disk, it is because the UUID of the swap space has not been updated into the boot image. If this happens, you can simply run the command below to bring back the splash screen.

$ sudo dpkg-reconfigure usplash
$

back
Previous article: Workshops for NTFS and Linux disk cloning and partition resizing view
Next article: Workshop 1: Post installation problems for cloned Ubuntu drives view

discuss
 by by David at 14 Feb 2009 23:58:00
Copyrights © Transcraft Trading Limited 2006.All rights reserved. Bots Rss-rss