Most users that have multiple operating systems eventually choose to delete, resize, or re-arrange the partitions containing the operating systems. This can become an anxiety-producing task especially when it comes to ensuring subsequent bootup capabilities.
Use the (K)Ubuntu Desktop LiveCD
There are several tools that are required to accomplish partition-manipulation tasks, including GParted, KDE Partition Manager from newer versions of the Kubuntu LiveCD, and several Linux commands accomplished from within a Linux command-line terminal. The SystemRescueCD (which has been a preferred tool for many years) has all the required tools (and more), but uses as its operating system Gentoo Linux instead of Ubuntu Linux (so it may be less familiar to many (K)Ubuntu users). The Ubuntu Desktop LiveCD (32-bit regular version, Lucid 10.04LTS or later) can be used instead of SystemRescueCD for most hard disk manipulation tasks, and already has GParted included on it. (Kubuntu LiveCDs, Natty 11.04 or later, have KDE Partition Manager, which works almost identically to GParted.)
- Download and burn onto CD/DVD a copy of the Ubuntu Desktop LiveCD (32-bit regular version, Lucid 10.04LTS or later) or Kubuntu Desktop LiveCD (Natty 11.04 or later).
- Boot into the (K)Ubuntu Desktop LiveCD and start it with the "Try (K)Ubuntu" option (not the "Install" option).
Use GParted to manage partitions
(Note: These instructions can be accomplished in a similar fashion using the KDE Partition Manager from newer versions of the Kubuntu LiveCD as well.)
- Start GParted from the Ubuntu Desktop LiveCD:
- Menu -> System -> Administration -> GParted
- A graphical display of all the partitions on your hard disk will be shown. If you have two hard disks on your system, they generally are referenced as /dev/sda and /dev/sdb (or sometimes /dev/hda and /dev/hdb). GParted works with only one hard disk at a time. To select which hard disk to work with, choose:
- GParted menu -> Devices
- Working with GParted is relatively intuitive. However, it is very easy to irreparably damage your system by undertaking changes without a thorough knowledge of partitions. It is highly recommended to read this article about multiple operating systems for an overview. Specifically heed the warnings about using GParted to change any NTFS partition on which a Windows OS systems resides. Windows has quirks and peculiarities about its OS partition that is better managed with Windows-specific tools. (NTFS partitions that do not have a Windows OS on them, however, can be managed with GParted.)
- It is especially important to recognize that deleting or adding a partition will change the partition numbering scheme (and other partition characteristics) on the hard drive. This is the major consideration in reorganizing your hard drive. Bootloaders find and load operating systems based on their partition location, specified by the partition number on the disk or by a UUID associated with the partition, both of which can change when creating, changing, or deleting partitions.
- GParted only allows changes to partitions that are subsequent to any locked partitions on a hard drive (locked partitions are designated in GParted with a key icon.) For this reason, it is best to have any partitions that are rarely likely to change and/or likely to be locked (e.g. boot partitions, the linux-swap partition, and Windows partitions that won't be manipulated) closer to the beginning of the hard drive and to locate Linux partitions that will be manipulated the most towards the end of the hard drive.
- In general, the least problems occur when a test or temporary partition is the last one on the hard drive. Adding, deleting, or changing the last partition on a hard disk does not affect any of the preceding partitions, so it is the least troublesome. Whenever possible, relegate a temporary or test partition (and its operating system) to be the last partition of the hard drive.
- Write down the details of all the partitions displayed in GParted on some scratch paper, and note any changes that are made by GParted as they are made. In specific, when GParted changes the designation of a partition (from /dev/sda8 to /dev/sda7, for example), note this carefully, as this information becomes critical later in changing bootloader settings.
One linux-swap partition per computer
- Only one linux-swap partition is required for computers that will run only one operating system at a time. (This does not apply to the special case of virtual machines, but virtual machines do not use conventional partitions anyway. Virtual machines are not viewed by a computer as independent operating systems but instead are viewed as applications running within the primary operating system). If already present on a hard drive, the linux-swap partition is used by the Ubuntu LiveCD and therefore locked in GParted. If changes need to be made to the linux-swap partition itself, therefore, use the GParted LiveCD or SystemRescueCD instead of the Ubuntu LiveCD (and run GParted from one of them).
- When installing, updating, re-arranging, or otherwise any Linux operating system, it is not necessary to alter the linux-swap partition in any way. The linux-swap partition is used by all Linux operating systems and is not peculiar to any Linux distribution. There is no need to recreate it, reformat it, nor change it in any way (except perhaps its size, which is ideally 2 Gb).
Creating and "moving" free space
There are two places free space ("unallocated space") can exist: within the extended partition (assuming that one exists) and on the hard disk outside the extended partition. Moving the free space so that it is inside or outside the extended partition is a skill that must be mastered in order to successfully manage partitions.
- When free space exists outside the extended partition, it can be used to create or increase the size of any primary partition or the extended partition itself.
- When free space exists within the extended partition, it can be used to create or increase the size of any logical partition within the extended partition.
In addition, the position of free space determines how it can be used. Free space can only be added to an existing partition if it is next to ("touching") that partition.
Free space can not itself be moved, however. Only partitions can be moved. "Moving free space" really means that partitions themselves must be moved in such a way that the free space "ends up" being in the desired location.
- Free space can only be created by deleting or shrinking an existing partition. This is the critical decision in manipulating partitions. Which partition can be shrunk or deleted safely? (Again, be very careful not to shrink any partition with a Windows OS on it using Gparted.)
Creating or resizing a partition
GParted can create many types of partitions, including ext4, ext3, NTFS, and FAT32, which is the majority of partition types that most users will create. It can resize any of these types of partitions as well. However, resizing an NTFS partition that contains a Windows OS within it may cause problems with the Windows OS itself. (All NTFS and FAT32 partitions should also be defragmented before resizing.)
- The most important decision will be whether a new partition will be a primary partition (to be used for a Windows operating system, for a Windows boot partition, or for a Grub Legacy boot partition), an extended partition (of which there can only be one per hard drive), or a logical partition that resides within a pre-existing extended partition. All Linux partitions can be in logical partitions.
- To create or increase the size of a logical partition within the extended partition, the extended partition itself must already be big enough to accommodate the new logical partition or its new size. To increase the size of the extended partition itself, there must be free space available outside the extended partition and contiguous to it ("touching" it). This requires manipulating the exsting partitions (by moving them and/or shrinking them) until the free space is in the necessary position. Once the free space is contiguous with (and outside) the extended partition, the size of the extended partition can be increased. This will have the effect of moving the free space into the extended partition.
- Once the free space is within the extended partition, it can be used to create or increase the size of a logical partition. To increase the size of a logical partition, the free space must be contiguous to (i.e. "touching") that logical partition by rearranging the positions of the existing logical partitions within the extended partition.
Changing Grub Legacy in a boot partition
When partitions have been moved, added, or deleted, the position and designation of all partitions on a hard drive may change. For example, if the /dev/sda7 partition is deleted, a partition that previously was designated as /dev/sda8 will now become /dev/sda7.
Grub Legacy (sometimes used in a freestanding boot partition) often boots operating systems by referring to the partition (in which the OS is located) by its position on the hard drive. In Grub Legacy, the position /dev/sda7 is referred to as (hd0,6), for example, and /dev/sdb2 is referred to as (hd1,1).
After manipulating partitions on a hard disk, therefore, the main Grub Legacy menu.lst (that resides on the boot partition) needs to be edited. This can be done by starting a command-line terminal from the Ubuntu LiveCD:
- Menu -> Applications -> Accessories -> Terminal
Then follow the instructions here.
Changing Grub2 in a changed partition
Note: This section is being edited.
The hardest thing to do is to change Grub or Grub Legacy that exists within a partition that has been changed or moved. If that partition uses Grub2, then the Grub2 bootloader within that partition can be reconstructed using the Ubuntu LiveCD and then stored within that partition once again.
For example, if a (K)Ubuntu operating system (Karmic 9.10 or later) has been moved from /dev/sda8 and now resides at /dev/sda7, Grub2 can be reinstalled on that partition for the (K)Ubuntu operating system there using the Ubuntu LiveCD. Open the command-line terminal from the Ubuntu LiveCD:
- Menu -> Applications -> Accessories -> Terminal
and use the command:
sudo grub-install /dev/sda7
Booting (K)Ubuntu manually from Grub Legacy
When a partition has been changed whose operating system contains a Grub2 bootloader, the Grub2 bootloader might no longer function. If, however, a Grub Legacy bootloader has been previously installed in its own boot partition on the system (as is recommended here), the Grub Legacy bootloader can be used to manually boot the operating system. (Once the operating system has been manually booted, Grub2 can then be reconstructed from within the running OS.)
- Reboot the computer without using the Ubuntu LiveCD. When the Grub Legacy menu appears, enter the Grub Legacy command line (using the command c ):
In newer versions of (K)Ubuntu there are symbolic links to the current kernel files, so the following commands can be entered at the grub prompt (the example assumes the OS is in the partition at /dev/sda7):
grub> root (hd0,6) grub> kernel /vmlinuz root=/dev/sda7 ro grub> initrd /initrd.img grub> boot
In newer versions of (K)Ubuntu, the following commands can also be used (if the core.img has not been changed during updates):
grub> root (hd0,6) grub> kernel /boot/grub/core.img grub> boot
- Once the OS has successfully booted, the Grub2 bootloader within it can be reconstructed using the instructions here.
Discovering the current kernel files manually
In older versions of (K)Ubuntu, symbolic links were not included to the current kernel files. For those versions, the kernel files must be discovered and then entered into the Grub Legacy command line manually.
- Discover the current kernel used by the OS. Using the Ubuntu LiveCD, open a command-line Terminal:
- Menu -> Applications -> Accessories -> Terminal
If the designation of the partition is currently /dev/sda7, create a mount point for the partition. Use ext4 if the partition uses an ext4 filesystem or ext3 if it uses an ext3 filesystem. If you are unsure about the partition's filesystem type or designation, use GParted from the Ubuntu LiveCD to find out.
sudo mkdir /media/sda7 sudo mount -t ext4 /dev/sda7 /media/sda7 cd /media/sda7/boot ls
Write down the most recent vmlinuz and initrd files listed there. As an example, the latest files may be vmlinuz-2.6.32-21-generic and initrd.img-2.6.32-21-generic
- Reboot the system and at the Grub Legacy menu, enter the Grub Legacy command line (using the command c ). then enter the commands at the grub prompt:
grub> root (hd0,6) grub> kernel /boot/vmlinuz-2.6.32-21-generic root=/dev/sda7 ro grub> initrd /boot/initrd.img-2.6.32-21-generic grub> boot
- Once the operating system has successfully booted, Grub2 can be reconfigured using the instructions here.
Changing Grub Legacy in a changed partition
Generally, only versions of (K)Ubuntu prior to Karmic use Grub Legacy by default. (Only Hardy and Dapper are still supported.) The local Grub Legacy menu.lst of one of these versions must be edited manually, using the instructions here.