Debian – How to Move Root (/) to a New Partition

debianpartition

I have Windows 7 and Debian dual-booted on my laptop. I'm getting cramped for space on the Debian side, so I want to remove the Windows partition and use the whole computer for Debian. I'm currently using a single partition for Debian (plus a swap partition), but would like to split /home into its own partition in the new arrangement.

Since the Windows partition is larger than all the Debian partitions, there's space enough for the whole linux installation in the current windows partition. So I'm thinking what I'd do is:

  1. reformat the 60GB windows 7 partition /sda2 to an extended partition with two two ext4 partitions, one 15 GB for /, the other 45 GB for /home
  2. move/clone my current / and /home directories to the new partitions
  3. reformat the partitions where the debian install was, freeing up 50GB of space
  4. resizing the new extended partition, and the /home partition in it, to incorporate the 50GB freed in step 3
  5. leave a 2 GB at the end for swap

I've posted my fdisk -l below.

My questions:

  1. Is this a sensible approach?
  2. How do I do step 2? Can I move the / directory without having to reinstall it?
  3. I'm assuming that 4 will be straightforward, since I'll be resizing by moving the end of the partition, not the beginning – is that correct?
 
fdisk -l

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x45689f01

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200    7  HPFS/NTFS/exFAT
/dev/sda2          616448   127768575    63576064    7  HPFS/NTFS/exFAT
/dev/sda3       127770622   234440703    53335041    5  Extended
/dev/sda5       127770624   230021119    51125248   83  Linux
/dev/sda6       230023168   234440703     2208768   82  Linux swap / Solaris

Best Answer

I'd suggest using LVM. To switch over, something like this:

  1. Create a physical volume (pvcreate) on your existing /dev/sda2 (Windows partition)
  2. Create a volume group consisting of just /dev/sda2 (vgcreate)
  3. Create a logical volume to hold your / and /home (any reason you're splitting them? You don't need to.) (lvcreate)
  4. mkfs the two new logical volumes (device: /dev/mapper/vgname-lvname)
  5. If you're not already booted from a rescue disc, boot from one (you could use the Debian install disc, just switch to a terminal at the appropriate point, or alternatively a Ubuntu disc)
  6. mount the new logical volumes (or just one, if you decide not to split). If you mount both, make sure to mount home inside the new root (e.g., as /new-root/ and /new-root/home) Mount your old rootfs somewhere, too.
  7. use tar to copy the files over. Something like ( cd /old-root && tar --one-file-system -c . ) | ( cd /new-root && tar vx )
  8. chroot into your new root filesystem, and:
    1. change /etc/fstab to give the proper root filesystem
    2. mount /proc and /sys and /boot (inside the chroot)
    3. update-initramfs -u
    4. update-grub
    5. if you're paranoid, look at the generated /boot/grub/grub.cfg to check that it picked up the new rootfs.
    6. exit the chroot
  9. Finally, unmount the filesystems you mounted (starting with /new-root/{proc,sys,boot}), and reboot.
  10. Confirm you've booted to the new root filesystem. E.g., check /proc/mounts. Confirm your data is present. (You do have backups too, right?) VERY IMPORTANT.
  11. This is the point of no return. The next steps destroy your old partitions.
  12. You can now pvcreate your old partitions, then add them to your volume group (vgextend)
  13. You can now resize your / and /home logical volumes using lvextend followed by resize2fs (or whichever tool for your filesystem). This can be done online, with your system running.

Once you're on LVM, any future disk changes are much easier. You can use the LVM commands to do pretty much any disk change, with the system running even.

(Note: I haven't done this for a bit, I may have missed a step or two, but I'm pretty sure I haven't missed anything catastrophic. And of course, your original install & data is still around until after you've confirmed you're booted onto the new rootfs)

Related Question