Wiki‎ > ‎

Linux OS Disk Clone and ext3 resize, fdisk change partition type

posted Feb 2, 2016, 1:46 PM by Dong Xu   [ updated Mar 30, 2016, 9:37 AM ]
Essentially, to resize (shrink or enlarge) ext3 partition, we have to use resize2fs. Other methods are unsafe

Aomei and ddrescue are preferred methods for cloning Linux disks, albeit slower. Aomei mislable swap to FAT12, need to change it back to Linux swap type 82.

All these methods only apply to non-GPT disk and < 2TB. Generally, there is no need to use a > 2TB disk for Linux OS partition.

To shrink, run resize2fs first to move the data, then fdisk to shrink the partition table
To enlarge, run fdisk to enlarge the partition table, then resize2fs to move the data

When fdisk, the start/end cylinders and the +size K need to be precise!


To Shrink an ext3 partition on a larger SUSE drive, then clone it to a small SUSE drive


Seagate 1.5TB  to WD 80GB, OS partition filesize ~10GB

Clone software can't allow If the Linux partition to go from large to small. So I had to resize it from 1.5TB to ~50GB because the actual space used is only 10GB. To do this, move the Seagate 1.5TB to another different SUSE computer and connect it using a USB adapter (you can also do this using SUSE boot DVD and enter RESCUE MODE)

lsscsi (to find out it's /dev/sdf)

fdisk -l /dev/sdf   (to check on the current partition info)

Disk /dev/sdf: 1500.3 GB, 1500301910016 bytes
255 heads, 63 sectors/track, 182401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0001f6ef

   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1               1        1044     8385898+  82  Linux swap / Solaris
/dev/sdf2   *        1045      182401  1456750102+  83  Linux


/dev/sdf2 is the Linux partition to shrink

e2fsck -f /dev/sdf2  (have to do this before shrinking)

resize2fs /dev/sdf2 50G (note that the size should be smallest to accommodate the actual space used, in this case, 10GB)

Resizing the filesystem on /dev/sde2 to 13107200 (4k) blocks.
The filesystem on /dev/sde2 is now 13107200 blocks long.

Write down 13107200 (4k) block

Now fdisk /dev/sdf2
delete old partition
create a new primary partition 2
start cylinder 1045, then add  13107200 X 4 X 1.03 = 54001664K  (1.03 is some extra space used as wiggle room)
lable the new partition active
write partition table

e2fsck -n /dev/sdf2 (to check if everything is clean)

Now, the new partition should ~50GB, small enough to be cloned using (Aomei, HDClone, ddrescue, etc)
During cloning, choose to extend to extra space all the way to the end of WD 80GB drive. If can't do that, see the enlarging ext3 steps using resize2fs below.

Now the OS is cloned, connect the WD 80GB to a SUSE PC (or use RESCUE DVD)

mount  /dev/sd?  /mnt
ls -l /dev/disk/by-id > /mnt/disk-by-id

This contains the ATA-DISK-ID of the new WD drive. ATA-DISK-ID will be inserted into
/mnt/etc/fstab 
/mnt/boot/grub/menu.lst


Now, connect the WD 80GB drive and boot, but likely you will run into GRUB Error (such as GEOM Error)

Boot into RESCUE DVD

#grub
grub> find /boot/grub/stage1   (or /grub/stage1)
(hd6,1)

grub> root (hd6,1)

grub> setup (hd6)

...... succeeded

grub> quit

reboot

That should boot the new drive!!!

==================================================
I used Aomei Backupper (free windows program) to clone a 160GB Maxtor to a Seagate 250GB.
Cloning was good, but boot from Seagate failed. The issue is that SUSE uses Hard Drive brand name+SN to label partitions in /etc/fstab and /boot/grub/menu.lst files, so if the two drives are not identical, the labels need to be changed in the files.

A work around is to replace the labels with /dev/sd? (df -h plus lsscsi will show) in these files before taking out the Maxtor for cloning. After cloning and booting from the Seagate, switch the labels back by get the new drive label info from

/dev/disk/by-id/....

If you don't do the above, you will have to connect the new drive to another linux system, mount it and make the changes.

/etc/fstab
/boot/grub/menu.lst


===========================================================
To enlarge  (new drive is larger than the old drive)

2nd issue is that the Seagate is larger than the Maxtor by 85GB, we need to resize the OS partition without destroying OS data. Windows-based partition programs cannot resize ext3, so we have to use Linux commands:

https://www.novell.com/support/kb/doc.php?id=7006860

/sbin/fdisk /dev/sdd  (see the detailed steps below)
/usr/sbin/partprobe
/sbin/fdisk -l /dev/sdd
/sbin/fsck.ext3 /dev/sdd2
/sbin/e2fsck -f /dev/sdd2
/sbin/resize2fs /dev/sdd2
/sbin/fdisk -l /dev/sdd

Steps

The file-system on a server has consumed all free space. Space has been added to the disk and now the partition and file-system needs to be expanded.
In the following situation we have a partition of roughly 1GB. We need to use an additional 1GB that is free for the file-system. This situation also assumes that the partition is not mounted. See the additional information section for the commands to run a re-size while the file-system is mounted.

Please note: that you will want to make a backup of your data. Even though this process can be run without problem you will still want to plan as if you are going to loose the data in this process.
This assumes the partition is not mounted

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         131     1052226   83  Linux

Note: The start and end sector. When we delete the partition we will use the same start but the end sector will change to consume the extra space.Your data should not be affected.

linux-tleg:~ # fdisk /dev/sdb

Command (m for help): d
Selected partition 1

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

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

linux-tleg:~ # partprobe     *This command will rescan the partitions and attempt to update the kernel*

*NOTE* Generally at this point you need to reboot before you can re-size the file-system!

linux-tleg:~ # fdisk -l /dev/sdb      *This shows that the new end sector is 261*


Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         261     2096451   83  Linux




linux-tleg:/ # resize_reiserfs /dev/sdb1
resize_reiserfs 3.6.19 (2003 www.namesys.com)

ReiserFS report:
blocksize             4096
block count           524112 (263056)
free blocks           515885 (254836)
bitmap block count    16 (9)

Syncing..done


resize_reiserfs: Resizing finished successfully.


If you are using ext3 you will need to first fsck the file-system and then re-size:

linux-tleg:/ #  fsck.ext3 /dev/sdb1

Then re-size

linux-tleg:/ #  resize2fs /dev/sdb1
=========================================================

3nd issue is to change swap partition type from FAT12 (incorrectly labeled) to 86

http://computernetworkingnotes.com/file-system-administration/how-to-create-swap-partition.html

/sbin/fdisk /dev/sde
/sbin/mkswap /dev/sdd1
/sbin/swapon /dev/sdd1
cat /proc/swaps

Remember to label the partition bootable in fdisk!
Comments