Partitioning Drives in Linux

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Recommended Partitions

  1. /boot needs to be on its own partition, preferably /dev/sda1. You should make this plenty bigger than you need. If you go with 1GB, that should be much more than enough. 10GB would be overkill. I used to run systems with as small as 200MB /boot partition, but Fedora's preupgrade needs to stick stuff there so the bigger the better.
  2. You should have plenty of swap space, preferably on all your drives. The swap space should roughly equal the amount of memory you have. In general, your system should never swap, but if it needs to, you'll need enough swap space that the system will run until you can fix it.
  3. For the rest of the space, I prefer using LVM to manage it all. LVM allows you a lot of flexibility.
    1. Allocate only enough space as you need it. 20G should be enough for a good root partition---just the static stuff. Mount the directories in /var where a lot of data is used in its own partitions. For instance, I stick /var/lib/pgsql, the PostgreSQL data directory, in its own partition.
    2. /home lives in its own partition. 20GB is more than enough. If I use a lot of data, then I may extend it, or I may make a partition for the data.
    3. For backups, I sometimes have a prep area in a partition I mount at /backup. However, I don't trust this for long-term backup. For that, you need an external drive or an external server to store your data.

On a modern system, you only really need about 50GB of space for all your computing needs. Of course, you can get much larger drives for less than $50, and the smaller drives are hardly worth it. But eventually, we'll see Solid-State Drives get into the affordable range at 50GB, so you can think about mounting your root and home directories on an SSD and have your long-term, slow storage on a connected hard drive.

fdisk

fdisk -l will give you a list of your partitions on a drive.

# fdisk -l /dev/sda

Disk /dev/sda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x41ab2316 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26         156     1052257+  82  Linux swap / Solaris
/dev/sda3             157        9726    76871025   8e  Linux LVM
# fdisk -l /dev/sdb 

Disk /dev/sdb: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000081

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1        2550    20482843+  83  Linux
/dev/sdb2            2551        2681     1052257+  82  Linux swap / Solaris
/dev/sdb3            2682        9726    56588962+  8e  Linux LVM

Swap

Use swapon to list your swap partitions that are currently engaged.

swapon -s shows you what swap partitions you have activated.

# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdb2                               partition       1052248 7120    -1
/dev/sda2                               partition       1052248 0       -2

swapoff will remove a partition from swap.

# swapoff /dev/sda2
# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdb2                               partition       1052248 7120    -1

swapon will put a partition in swap.

# swapon /dev/sda2
# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sdb2                               partition       1052248 7120    -1
/dev/sda2                               partition       1052248 0       -2

Note that the swap partition has to be prepared as swap. You need to have it marked as swap according to fdisk ("Linux swap / Solaris") and then run mkswap on the partition before adding it to swap.

Removing a partition

To remove a partition, use fdisk.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9726.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

We want to see the partition table before we manipulate it. Use p to show the table.

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x41ab2316

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26         156     1052257+  82  Linux swap / Solaris
/dev/sda3             157        9726    76871025   8e  Linux LVM

To delete a partition, use d.

Command (m for help): d
Partition number (1-4): 2

View the partition table to ensure you did it right with p.

Command (m for help): p 

Disk /dev/sda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x41ab2316

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda3             157        9726    76871025   8e  Linux LVM

Finally, write the changes to the table with w. Note that in this case, the partition table isn't written yet because the kernel is still using the drive.

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.

Extend a Partition

Let's say you want to extend a partition. You'll need space after the partition on your drive. This can take some shuffling.

First, backup the data in the partition if you are worried at all of losing it. Here, I'm going to extend the /boot partition.

# cd /backup
# tar zcf boot.tar.gz /boot
tar: Removing leading `/' from member names

Now, make sure you are extending the right partition.

# df -h /boot
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             190M  156M   25M  87% /boot

Next, you need to remove that mount point.

# cd /
# umount /boot

Now, you should delete the partition and remake a new one of the bigger size. Note that there must be room free after the partition.

Use fdisk /dev/sda. Use p to see the partitions. Use d to delete partition 1. Then use n to create a new partition starting where it used to start and ending where you'd like it to end. Then finally w to write the changes to disk.

If you're using any partitions on the disk, then you'll need to reboot. After the reboot, umount the partition and then run resize2fs on the partition. It may complain about having to run e2fsck first. After that, mount the partition again and you should be good to go. Bold text