Introduction to Block Devices
Partitions
Although it is theoretically possible to use a full disk to house your Linux
system, this is almost never done in practice. Instead, full disk block devices
are split up in smaller, more manageable block devices. On
systems, these are called partitions.
Partitions are divided in three types:
primary, extended and logical.
A primary partition is a partition which has its information stored in
the MBR (master boot record). As an MBR is very small (512 bytes) only four
primary partitions can be defined (for instance, /dev/sda1 to
/dev/sda4).
An extended partition is a special primary partition (meaning the
extended partition must be one of the four possible primary partitions) which
contains more partitions. Such a partition didn't exist originally, but as
four partitions were too few, it was brought to life to extend the formatting
scheme without losing backward compatibility.
A logical partition is a partition inside the extended partition. Their
definitions aren't placed inside the MBR, but are declared inside the extended
partition.
Advanced Storage
The Installation CDs provide support for LVM2.
LVM2 increases the flexibility offered by your partitioning setup.
During the installation instructions, we will focus on "regular" partitions,
but it is still good to know LVM2 is supported as well.
Designing a Partitioning Scheme
Default Partitioning Scheme
If you are not interested in drawing up a partitioning scheme for your system,
you can use the partitioning scheme we use throughout this book:
| Partition |
Filesystem |
Size |
Description |
/dev/sda1
ext2
32M
Boot partition
/dev/sda2
(swap)
512M
Swap partition
/dev/sda3
ext3
Rest of the disk
Root partition
If you are interested in knowing how big a partition should be, or even how
many partitions you need, read on. Otherwise continue now with partitioning
your disk by reading Using fdisk to Partition your
Disk or Using parted to Partition your Disk
(both are partitioning tools, fdisk is well known and stable,
parted is a bit more recent but supports partitions larger than
2TB).
How Many and How Big?
The number of partitions is highly dependent on your environment. For instance,
if you have lots of users, you will most likely want to have your
/home separate as it increases security and makes backups easier.
If you are installing Gentoo to perform as a mailserver, your
/var should be separate as all mails are stored inside
/var. A good choice of filesystem will then maximise your
performance. Gameservers will have a separate /opt as most gaming
servers are installed there. The reason is similar for /home:
security and backups. You will definitely want to keep /usr big:
not only will it contain the majority of applications, the Portage tree alone
takes around 500 Mbyte excluding the various sources that are stored in it.
As you can see, it very much depends on what you want to achieve. Separate
partitions or volumes have the following advantages:
-
You can choose the best performing filesystem for each partition or volume
-
Your entire system cannot run out of free space if one defunct tool is
continuously writing files to a partition or volume
-
If necessary, file system checks are reduced in time, as multiple checks can
be done in parallel (although this advantage is more with multiple disks than
it is with multiple partitions)
-
Security can be enhanced by mounting some partitions or volumes read-only,
nosuid (setuid bits are ignored), noexec (executable bits are ignored) etc.
However, multiple partitions have one big disadvantage: if not configured
properly, you might result in having a system with lots of free space on one
partition and none on another. There is also a 15-partition limit for SCSI and
SATA.
As an example partitioning, we show you one for a 20GB disk, used as a
demonstration laptop (containing webserver, mailserver, gnome, ...):
$ df -h
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda5 ext3 509M 132M 351M 28% /
/dev/sda2 ext3 5.0G 3.0G 1.8G 63% /home
/dev/sda7 ext3 7.9G 6.2G 1.3G 83% /usr
/dev/sda8 ext3 1011M 483M 477M 51% /opt
/dev/sda9 ext3 2.0G 607M 1.3G 32% /var
/dev/sda1 ext2 51M 17M 31M 36% /boot
/dev/sda6 swap 516M 12M 504M 2% <not mounted>
(Unpartitioned space for future usage: 2 GB)
/usr is rather full (83% used) here, but once
all software is installed, /usr doesn't tend to grow that much.
Although allocating a few gigabytes of disk space for /var may
seem excessive, remember that Portage uses this partition by default for
compiling packages. If you want to keep /var at a more reasonable
size, such as 1GB, you will need to alter your PORTAGE_TMPDIR variable
in /etc/make.conf to point to the partition with enough free space
for compiling extremely large packages such as OpenOffice.
Using fdisk to Partition your Disk
If your environment will deal with partitions larger than 2 TB, please
use the Using parted to Partition your Disk
instructions instead. fdisk is not able to deal with larger
partitions.
The following parts explain how to create the example partition layout
using fdisk. The example partition layout was mentioned earlier:
| Partition |
Description |
/dev/sda1
Boot partition
/dev/sda2
Swap partition
/dev/sda3
Root partition
Change your partition layout according to your own preference.
Viewing the Current Partition Layout
fdisk is a popular and powerful tool to split your disk into partitions.
Fire up fdisk on your disk (in our example, we use
/dev/sda):
# fdisk /dev/sda
Once in fdisk, you'll be greeted with a prompt that looks like this:
Command (m for help):
Type p to display your disk's current partition configuration:
Command (m for help): p
Disk /dev/sda: 240 heads, 63 sectors, 2184 cylinders
Units = cylinders of 15120 * 512 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 105808+ 83 Linux
/dev/sda2 15 49 264600 82 Linux swap
/dev/sda3 50 70 158760 83 Linux
/dev/sda4 71 2184 15981840 5 Extended
/dev/sda5 71 209 1050808+ 83 Linux
/dev/sda6 210 348 1050808+ 83 Linux
/dev/sda7 349 626 2101648+ 83 Linux
/dev/sda8 627 904 2101648+ 83 Linux
/dev/sda9 905 2184 9676768+ 83 Linux
Command (m for help):
This particular disk is configured to house seven Linux filesystems (each with
a corresponding partition listed as "Linux") as well as a swap partition
(listed as "Linux swap").
Removing all Partitions
We will first remove all existing partitions from the disk. Type d to
delete a partition. For instance, to delete an existing /dev/sda1:
Command (m for help): d
Partition number (1-4): 1
The partition has been scheduled for deletion. It will no longer show up if you
type p, but it will not be erased until your changes have been saved. If
you made a mistake and want to abort without saving your changes, type q
immediately and hit enter and your partition will not be deleted.
Now, assuming that you do indeed want to wipe out all the partitions on your
system, repeatedly type p to print out a partition listing and then type
d and the number of the partition to delete it. Eventually, you'll end
up with a partition table with nothing in it:
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
Device Boot Start End Blocks Id System
Command (m for help):
Now that the in-memory partition table is empty, we're ready to create the
partitions. We will use a default partitioning scheme as discussed previously.
Of course, don't follow these instructions to the letter if you don't want the
same partitioning scheme!
Creating the Boot Partition
We first create a small boot partition. Type n to create a new partition,
then p to select a primary partition, followed by 1 to select the
first primary partition. When prompted for the first cylinder, hit enter. When
prompted for the last cylinder, type +32M to create a partition 32 Mbyte
in size and set its bootable flag:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-3876, default 1): (Hit Enter)
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-3876, default 3876): +32M
Now, when you type p, you should see the following partition printout:
Command (m for help): p
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
Device Boot Start End Blocks Id System
/dev/sda1 1 14 105808+ 83 Linux
We need to make this partition bootable. Type a to toggle the bootable
flag on a partition and select 1. If you press p again, you will
notice that an * is placed in the "Boot" column.
Creating the Swap Partition
Let's now create the swap partition. To do this, type n to create a new
partition, then p to tell fdisk that you want a primary partition. Then
type 2 to create the second primary partition, /dev/sda2 in
our case. When prompted for the first cylinder, hit enter. When prompted for
the last cylinder, type +512M to create a partition 512MB in size. After
you've done this, type t to set the partition type, 2 to select
the partition you just created and then type in 82 to set the partition
type to "Linux Swap". After completing these steps, typing p should
display a partition table that looks similar to this:
Command (m for help): p
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 105808+ 83 Linux
/dev/sda2 15 81 506520 82 Linux swap
Creating the Root Partition
Finally, let's create the root partition. To do this, type n to create a
new partition, then p to tell fdisk that you want a primary partition.
Then type 3 to create the third primary partition, /dev/sda3
in our case. When prompted for the first cylinder, hit enter. When prompted for
the last cylinder, hit enter to create a partition that takes up the rest of the
remaining space on your disk. After completing these steps, typing p
should display a partition table that looks similar to this:
Command (m for help): p
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 14 105808+ 83 Linux
/dev/sda2 15 81 506520 82 Linux swap
/dev/sda3 82 3876 28690200 83 Linux
Saving the Partition Layout
To save the partition layout and exit fdisk, type w.
Command (m for help): w
Now that your partitions are created, you can continue with Creating Filesystems.
Using parted to Partition your Disk
In this chapter, we guide you through the creation of the example partition
layout mentioned earlier in the instructions. Unlike the previous chapter, we
describe the method using the parted application instead. Both
parted and fdisk offer the same functions, so if you partitioned
your system using fdisk already, you can skip this section and continue
with Creating Filesystems.
The example partition layout we use is shown in the next table:
| Partition |
Description |
/dev/sda1
Boot partition
/dev/sda2
Swap partition
/dev/sda3
Root partition
Change your partition layout according to your own preference.
Viewing the Current Partition Layout
The parted application is a somewhat more modern variant of
fdisk. It offers a simpler interface for partitioning your disks and
supports very large partitions (more than 2 TB). Fire up parted on your
disk (in our example, we use /dev/sda):
# parted /dev/sda
GNU Parted 2.3
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
To find out about all options supported by parted, type help and
press return. For now, we just continue by asking parted to show the
partitions currently in use on the selected disk. The print command can
be used for that.
(parted) print
Model: SCSI Block Device
Disk /dev/sda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 2148MB 2148MB primary ext4
2 2148MB 3222MB 1074MB primary linux-swap(v1)
3 3222MB 21.5GB 18.3GB primary lvm
Optional: Setting the GPT Label
Most disks on x86/amd64 are prepared using an msdos label. However, if
you plan on creating huge partitions (2 TB and more), you must use a gpt
label (the GUID Partition Type) for your disk. Using parted, this
can be accomplished with mklabel gpt:
Changing the partition type will remove all partitions from your disk. All data
on the disk will be lost.
(parted) mklabel gpt
Removing all Partitions
If this isn't done yet (for instance through the mklabel operation
earlier, or because the disk is a freshly formatted one), we will first
remove all existing partitions from the disk. Type rm <number>
where <number> is the partition you want to remove.
(parted) rm 2
Do the same for all other partitions that you don't need. However, make sure you
do not make any mistakes here - parted executes the changes immediate
(unlike fdisk which stages them, allowing a user to "undo" his changes
before saving or exiting fdisk).
Creating the Partitions
Now let's create the partitions we mentioned earlier. Creating partitions with
parted isn't very difficult - all we need to do is inform parted
about the following settings:
-
The partition type to use. This usually is primary in case you
are not going to have more than 4 partitions (with the msdos
partition label). Otherwise, you will need to make your fourth partition an
extended one which hosts the rest of the disk, and create
logical partitions inside it. If you use a gpt-labeled
partition, then there is no limit on the number of primary partitions.
-
The file system type to use. The parted application supports
most common file systems and knows which kind of partition ID it needs to
use for these partitions. This does not mean that parted will
create a file system on the partition (you can with the mkpartfs
command, but we'll use the regular mkfs.* commands later for this
purpose). The partition ID is often used by auto-detection tools to know
what to do with a particular partition.
-
The start location of a partition (which can be expressed in MB or GB)
-
The end location of the partition (which can be expressed in MB or GB)
One advantage of parted is that you can easily just use the partition
sizes to automatically find the correct start and end location as you will see
in the next example.
# Create a 32 mbyte /boot partition
(parted) mkpart primary ext2 0 32mb
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i
# Create a 512 mbyte swap partition
(parted) mkpart primary linux-swap 32mb 542mb
# Create a partition that spans the remaining disk.
# -1s (minus one s) means the end of the disk
(parted) mkpart primary ext4 542mb -1s
Warning: You requested a partition from 542MB to 21.5GB.
The closest location we can manage is 542MB to 21.5GB.
Is this still acceptable to you?
Yes/No? y
You can now print the partition layout again to validate if everything is
as expected. When you are satisfied, use the quit command to exit
parted.
Creating Filesystems
Introduction
Now that your partitions are created, it is time to place a filesystem on them.
If you don't care about what filesystem to choose and are happy with what we use
as default in this handbook, continue with Applying a Filesystem to a Partition.
Otherwise read on to learn about the available filesystems...
Applying a Filesystem to a Partition
To create a filesystem on a partition or volume, there are tools available for
each possible filesystem:
| Filesystem |
Creation Command |
ext2
mkfs.ext2
ext3
mkfs.ext3
ext4
mkfs.ext4
reiserfs
mkreiserfs
xfs
mkfs.xfs
jfs
mkfs.jfs
For instance, to have the boot partition (/dev/sda1 in our
example) in ext2 and the root partition (/dev/sda3 in our example)
in ext3 (as in our example), you would use:
# mkfs.ext2 /dev/sda1
# mkfs.ext3 /dev/sda3
Now create the filesystems on your newly created partitions (or logical
volumes).
Activating the Swap Partition
mkswap is the command that is used to initialize swap partitions:
# mkswap /dev/sda2
To activate the swap partition, use swapon:
# swapon /dev/sda2
Create and activate the swap with the commands mentioned above.
Mounting
Now that your partitions are initialized and are housing a filesystem, it is
time to mount those partitions. Use the mount command. Don't forget to
create the necessary mount directories for every partition you created. As an
example we mount the root and boot partition:
# mount /dev/sda3 /mnt/gentoo
# mkdir /mnt/gentoo/boot
# mount /dev/sda1 /mnt/gentoo/boot
If you want your /tmp to reside on a separate partition, be sure to
change its permissions after mounting: chmod 1777 /mnt/gentoo/tmp. This
also holds for /var/tmp.
We will also have to mount the proc filesystem (a virtual interface with the
kernel) on /proc. But first we will need to place our files on the partitions.
Continue with Installing the Gentoo
Installation Files.