The purpose of this how-to is to describe how to move your Ubuntu Linux partition to a different partition either in same hard-disk or different hard-disk.

Many other article of same subject in Internet seems to be outdated on following parts:

  1. Grub2 (Ubuntu 9.10 and later)
  2. UUID for partition identification (Ubuntu 6.10 and later)

Hence, the main purpose of this article to fill in the gap as well as one stop reference for the entire steps.

Following are outlines of steps involved:

  • Create new target partition
  • Clone current Linux partition to target partition
  • Generate and update UUID for target partition
  • Update grub and fstab
  • Update MBR to point the new grub.

Few assumptions I make are:

  1. Your Ubuntu is 9.10 or later
  2. Your Ubuntu is on ext4 partition (gparted should be to copy/paste any other file-system as well, however these steps were tested on ext4)
  3. Your Ubuntu configuration is of standard (not too customized)
  4. Grub2 is your boot loader
  5. SystemRescueCd - I prefer this rescue CD as it comes with gparted utility

  6. You are not changing swap partition

If any of above assumptions do not apply to your system, then you will need to execute following steps with cautions.

Step 1: Booting through Rescue CD

  • A copy of SystemRescueCd can be obtain from here.

  • Burn this to a CD
  • And reboot the machine to boot from the rescue CD
    • You may need to change configuration in your bios to enable boot up from CD
  • Choose default booting option and type "wizard" once it goes to prompt
  • Now you should have X desktop loaded.

Step 2: Create new partition

  • Launch gparted from start menu
  • Using gparted create a new partition on your target hard-disk.
    • Make sure the size new partition is same or bigger than your current Ubuntu partition used space.

    • You may also mount current Ubuntu partition to clean up unnecessary files in the partition to reduce the size.

shell> mkdir ~/ubuntu
shell> mount /dev/sdZY ~/ubuntu
or
shell> mount /dev/hdZY ~/ubuntu
# where ZY is your ubuntu partition's block device name (you can find this from gparted window)
shell> cd ~/ubuntu
  • And once you are done, make sure to unmount

shell> umount ~/ubuntu

Step 3: Clone Ubuntu partition to new location

  • On gparted window, right-click on Ubuntu partition and select "Copy"
  • Then right-click on the new partition you have just created and select "Paste"
    • gparted will start step-by-step copy process. This may take a while.
    • You may also opt to use dd for the same purpose, but I prefer gparted because
      • It does calculate optimal way to copy over the data (block size)
      • It able to verbose current progress status

Step 4: Generate and update UUID

  • Since step 3 copy and paste produce the new partition with exact same UUID, this will conflict and grub not able to tell the partitions apart. Hence we need to update UUID information on new partition.
  • Following is to check current UUID (You should notice duplicate UUID)

shell> blkid
  • And following is to generate and update UUID.

shell> tune2fs -U random /dev/sdZY
or
shell> tune2fs -U random /dev/hdZY
# where ZY is your new partition's block device name (you can obtain this from gparted window)
  • Verify UUID (All UUIDs should be unique now)

shell> blkid

Step 5: Update grub and fstab

  • Reboot your system from Rescue system back to normal Ubuntu
  • Once you have booted to your original Ubuntu, mount the partition via gnome -> Places option

  • Edit grub.cfg file from new partition.

shell> gksu gedit /media/<new partition uuid>/boot/grub/grub.cfg &
  • Note that you should edit grub.cfg from new partition and not from "/boot/grub/grub.cfg"

  • Using gedit, find and replace every "(hdX,Y)" appropriately
    • X and Y should be replaced based on your new partition's /dev/sdZY or /dev/hdZY, if Z is 'a' then X should be '0', if Z is 'b' then X should be '1' and so on.
  • Using gedit, find and replace every "--fs-uuid --set <UUID>" based on new generated UUID.

  • Also still within the same file, find and replace every 'menuentry 'Ubuntu..." to "menuentry 'UBUNTU...". This is just to verify which grub is used for main boot menu. This will be overwritten by grub-update later.
  • Edit fstab file from new partition

shell> gksu gedit /media/<new partition uuid>/etc/fstab &
  • Note that you should edit grub.cfg from new partition and not from "/etc/fstab"

  • Using gedit, find and replace root mount point's (line with "/") <UUID> based on new generated UUID.

Step 6: Update MBR to point the new grub

  • By now, you have 2 proper bootable Ubuntu in your system, and the idea now is to make the new Ubuntu partition self bootable without any dependencies to old Ubuntu partition. And to get ride the old Ubuntu partition eventually.
  • Before updating MBR, backup current MBR data

shell> sudo dd if=/dev/sdZ of=~/sdZ.img bs=1 count=512
or
shell> sudo dd if=/dev/hdZ of=~/sdZ.img bs=1 count=512
# where Z is your old Ubuntu partition's block device name
# note that device name is without 'Y'; without partition id.
  • Mount the partition via gnome -> Places option (if is not mounted)

  • Run grub setup for new partition

shell> sudo grub-install -d /media/<new partition uuid>/usr/lib/grub/[instance] /dev/sda

Where [instance] is the version you want to make bootable

  • Update new ubuntu partition as bootable

gnome> Run the "Disks" application
  • Find and select your old Ubuntu partition to highlight the partition
  • Click the "Settings" gear button and select "Edit Mount Options"
  • Uncheck "Mount at Startup" and click "OK"
  • Find and select your new Ubuntu partition to highlight the partition
  • Click the "Settings" gear button and select "Edit Mount Options"
  • Set "Automatic Mount Options" to OFF
  • Check "Mount at startup"
  • "Show in user interface" and "Require additional authorization to mount" should NOT be checked
  • Mount options should be "errors=remount-ro"
  • Mount point should be "/"
  • Click "OK"
  • Reboot you machine again
  • On booting, make sure new partition's grub menu is displayed as.

1) UBUNTU...

  • Finally, once rebooted successfully on new Ubuntu partition.
  • Refresh the GRUB 2 menu

sudo update-grub
  • Make sure Ubuntu from new partition is perfectly fine before deleting the old Ubuntu partition.
  • In case to revert back to old Ubuntu partition, all you need to do is restore the backed-up MBR image.

shell> sudo dd if=~/sdZ.img of=/dev/sdZ bs=1 count=512
or
shell> sudo dd if=~/sdZ.img of=/dev/hdZ bs=1 count=512
# where Z is your old Ubuntu partition's block device name
# note that device name is without 'Y'; without partition id.

See Also

External Links


CategorySystem

MovingLinuxPartition (last edited 2020-03-23 10:04:38 by rs2009)