Repartition Linux Disk and Move /root Around

This is a Linux server that does not have a floppy disk. It has ext2, ext3, and reiserfx on it. I want to preserve the conntent, while repartitioning the disks.

Move data in non-root partition

If the partitions you want to move or change are not root or boot, then it is easy. Just install "parted" and follow the instruction. Or do the same as in the next section "Move data in root partition" - no need to use dd. Just use tar to archive and unarchive. Use dd when you have to deal with low level stuff, such as data in /root, /dev, etc.

Move data in root partition

This is what I did today. It was quite involving. So I write it down for later reference.

I think "parted" can do this job more gracefully and safely. Maybe I will try it next time I had to move things around.

I had / in /dev/sdb3. Now I wanted to move / to /dev/sda3, and move the stuff in /dev/sda3 into /dev/sdb3. This also will impact the /dev/sda4. I will use /dev/sdb4 to park my data.

I thought tar had the option but I wasn't able to locate tar's option to preserve device node information. Thus I used dd. When we use dd to move data from one partition to another, the destination partition must be at least as large as the input partition. In other words, in this example, the size of /dev/sda3 must be greater than or equal to the size of /dev/sdb4. Also, they better have the same filesystem type.

  1. Boot from a LiveCD, and eventually you will get a root prompt.
  2. See the list of disks:
    # fdisk -l
  3. Mount the 4 partitions mentioned above:
    # mkdir /mnt/sda3
    # mount /dev/sda3 /mnt/sda3
    # mkdir /mnt/sda4
    # mount /dev/sda4 /mnt/sda4
    # mkdir /mnt/sdb3
    # mount /dev/sdb3 /mnt/sdb3
    # mkdir /mnt/sdb4
    # mount /dev/sdb4 /mnt/sdb4
  4. Archive the content in /dev/sda3 and /dev/sda4 and temporarily store the archive in /dev/sdb4:
    # cd /mnt
    # tar cpf sdb4/sda3-content.tar sda3
    # tar cpf sdb4/sda4-content.tar sda4
  5. Recreate /dev/sda3 and /dev/sda4 using
    # cfdisk /dev/sda
  6. Format the new patition:
    To create ext3 filesystem:
    # mkfs.ext3 /dev/sda3
    To create reiserfs filesystem:
    # mkfs.reserfs /dev/sda3
  7. Copy the data to the new partition:
    # dd if /dev/sdb4 of=/dev/sda3 bs=1k
  8. Move the tar files to the appropriate places, and untar using "tar xpf ...".
  9. Now we need to fix /etc/fstab.
    # mount /dev/sda3 sda3
    # cd sda3/etc
    # vi fstab
    And then change the mount points for the affected partitions and directories.
  10. Next we must fix the boot loader to have it recognize the new root location. Say, the /boot is in /dev/sda1:
    # mkdir /mnt/sda1
    # mount /dev/sda1 /mnt/sda1
    # cd /mnt/sda1/boot
    # vi grub.conf
    And change the location for the /root.
  11. Now we can reboot from hard disk and see.
  12. Check /var/log and see if there is anything went wrong.