Gentoo Logo

1.  Introduction to Block Devices

Block Devices

We'll take a good look at disk-oriented aspects of Gentoo Linux and Linux in general, including Linux filesystems, partitions and block devices. Then, once you're familiar with the ins and outs of disks and filesystems, you'll be guided through the process of setting up partitions and filesystems for your Gentoo Linux installation.

To begin, we'll introduce block devices. The most famous block device is probably the one that represents the first drive in a Linux system, namely /dev/sda. SCSI and Serial ATA drives are both labeled /dev/sd*; even IDE drives are labeled /dev/sd* with the new libata framework in the kernel. If you're using the old device framework, then your first IDE drive is /dev/hda.

The block devices above represent an abstract interface to the disk. User programs can use these block devices to interact with your disk without worrying about whether your drives are IDE, SCSI or something else. The program can simply address the storage on the disk as a bunch of contiguous, randomly-accessible 512-byte blocks.

Partitions

Although it is theoretically possible to use the entire 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. These are known as partitions or slices.

The first partition on the first SCSI disk is /dev/sda1, the second /dev/sda2 and so on.

The third partition on Sun systems is set aside as a special "whole disk" slice. This partition must not contain a file system.

Users who are used to the DOS partitioning scheme should note that Sun disklabels do not have "primary" and "extended" partitions. Instead, up to eight partitions are available per drive, with the third of these being reserved.

1.  Designing a Partitioning Scheme

Default Partitioning Scheme

If you are not interested in drawing up a partitioning scheme, the table below suggests a suitable starting point for most systems. Note that this is only an example, so feel free to use different partitioning schemes yourself.

Note that a separate /boot partition is generally not recommended on SPARC, as it complicates the bootloader configuration.

Partition Filesystem Size Mount Point Description
/dev/sda1 ext4 <2 GB / Root partition. For SPARC64 systems with OBP versions 3 or less, this must be less than 2 GB in size, and the first partition on the disk. More recent OBP versions can deal with larger root partitions and, as such, can support having /usr, /var and other locations on the same partition.
/dev/sda2 swap 512 MB none Swap partition. For bootstrap and certain larger compiles, at least 512 MB of RAM (including swap) is required.
/dev/sda3 none Whole disk none Whole disk partition. This is required on SPARC systems.
/dev/sda4 ext4 at least 2 GB /usr /usr partition. Applications are installed here. By default this partition is also used for Portage data (which takes around 500 MB excluding source code).
/dev/sda5 ext4 at least 1 GB /var /var partition. Used for program-generated data. By default Portage uses this partition for temporary space whilst compiling. Certain larger applications such as Mozilla and LibreOffice.org can require over 1 GB of temporary space here when building.
/dev/sda6 ext4 remaining space /home /home partition. Used for users' home directories.

1.  Using fdisk to Partition your Disk

The following parts explain how to create the example partition layout described previously, namely:

Partition Description
/dev/sda1 /
/dev/sda2 swap
/dev/sda3 whole disk slice
/dev/sda4 /usr
/dev/sda5 /var
/dev/sda6 /home

Change the partition layout as required. Remember to keep the root partition entirely within the first 2 GB of the disk for older systems. There is also a 15-partition limit for SCSI and SATA.

Firing up fdisk

Start fdisk with your disk as argument:

Code Listing 1.1: Starting fdisk

# fdisk /dev/sda

You should be greeted with the fdisk prompt:

Code Listing 1.1: The fdisk prompt

Command (m for help):

To view the available partitions, type in p:

Code Listing 1.1: Listing available partitions

Command (m for help): p

Disk /dev/sda (Sun disk label): 64 heads, 32 sectors, 8635 cylinders
Units = cylinders of 2048 * 512 bytes

   Device Flag    Start       End    Blocks   Id  System
/dev/sda1             0       488    499712   83  Linux native
/dev/sda2           488       976    499712   82  Linux swap
/dev/sda3             0      8635   8842240    5  Whole disk
/dev/sda4           976      1953   1000448   83  Linux native
/dev/sda5          1953      2144    195584   83  Linux native
/dev/sda6          2144      8635   6646784   83  Linux native

Note the Sun disk label in the output. If this is missing, the disk is using the DOS-partitioning, not the Sun partitioning. In this case, use s to ensure that the disk has a Sun partition table:

Code Listing 1.1: Creating a Sun Disklabel

Command (m for help): s
Building a new sun disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Drive type
   ?   auto configure
   0   custom (with hardware detected defaults)
   a   Quantum ProDrive 80S
   b   Quantum ProDrive 105S
   c   CDC Wren IV 94171-344
   d   IBM DPES-31080
   e   IBM DORS-32160
   f   IBM DNES-318350
   g   SEAGATE ST34371
   h   SUN0104
   i   SUN0207
   j   SUN0327
   k   SUN0340
   l   SUN0424
   m   SUN0535
   n   SUN0669
   o   SUN1.0G
   p   SUN1.05
   q   SUN1.3G
   r   SUN2.1G
   s   IOMEGA Jaz
Select type (? for auto, 0 for custom): 0
Heads (1-1024, default 64): 
Using default value 64
Sectors/track (1-1024, default 32): 
Using default value 32
Cylinders (1-65535, default 8635): 
Using default value 8635
Alternate cylinders (0-65535, default 2): 
Using default value 2
Physical cylinders (0-65535, default 8637): 
Using default value 8637
Rotation speed (rpm) (1-100000, default 5400): 10000
Interleave factor (1-32, default 1): 
Using default value 1
Extra sectors per cylinder (0-32, default 0): 
Using default value 0

You can find the correct values in your disk's documentation. The 'auto configure' option does not usually work.

Deleting Existing Partitions

It's time to delete any existing partitions. To do this, type d and hit Enter. You will then be prompted for the partition number you would like to delete. To delete a pre-existing /dev/sda1, you would type:

Code Listing 1.1: Deleting a partition

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

You should not delete partition 3 (whole disk). This is required. If this partition does not exist, follow the "Creating a Sun Disklabel" instructions above.

After deleting all partitions except the Whole disk slice, you should have a partition layout similar to the following:

Code Listing 1.1: View an empty partition scheme

Command (m for help): p

Disk /dev/sda (Sun disk label): 64 heads, 32 sectors, 8635 cylinders
Units = cylinders of 2048 * 512 bytes

   Device Flag    Start       End    Blocks   Id  System
/dev/sda3             0      8635   8842240    5  Whole disk

Creating the Root Partition

We're ready to create the root partition. To do this, type n to create a new partition, then type 1 to create the partition. When prompted for the first cylinder, hit enter. When prompted for the last cylinder, type +512M to create a partition 512 MB in size. Make sure that the entire root partition fits within the first 2 GB of the disk. You can see output from these steps below:

Code Listing 1.1: Creating a root partition

Command (m for help): n
Partition number (1-8): 1
First cylinder (0-8635): (press Enter)
Last cylinder or +size or +sizeM or +sizeK (0-8635, default 8635): +512M

Now, when you type p, you should see the following partition printout:

Code Listing 1.1: Listing the partition layout

Command (m for help): p

Disk /dev/sda (Sun disk label): 64 heads, 32 sectors, 8635 cylinders
Units = cylinders of 2048 * 512 bytes

   Device Flag    Start       End    Blocks   Id  System
/dev/sda1             0       488    499712   83  Linux native
/dev/sda3             0      8635   8842240    5  Whole disk

Creating a swap partition

Next, let's create the swap partition. To do this, type n to create a new partition, then 2 to create the second 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 512 MB in size. After you've done this, type t to set the partition type, 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:

Code Listing 1.1: Listing of available partitions

Command (m for help): p

Disk /dev/sda (Sun disk label): 64 heads, 32 sectors, 8635 cylinders
Units = cylinders of 2048 * 512 bytes

   Device Flag    Start       End    Blocks   Id  System
/dev/sda1             0       488    499712   83  Linux native
/dev/sda2           488       976    499712   82  Linux swap
/dev/sda3             0      8635   8842240    5  Whole disk

Creating the /usr, /var and /home partitions

Finally, let's create the /usr, /var and /home partitions. As before, type n to create a new partition, then type 4 to create the third partition, /dev/sda4 in our case. When prompted for the first cylinder, hit enter. When prompted for the last cylinder, enter +2048M to create a partition 2 GB in size. Repeat this process for sda5 and sda6, using the desired sizes. Once you're done, you should see something like this:

Code Listing 1.1: Listing complete partition table

Command (m for help): p

Disk /dev/sda (Sun disk label): 64 heads, 32 sectors, 8635 cylinders
Units = cylinders of 2048 * 512 bytes

   Device Flag    Start       End    Blocks   Id  System
/dev/sda1             0       488    499712   83  Linux native
/dev/sda2           488       976    499712   82  Linux swap
/dev/sda3             0      8635   8842240    5  Whole disk
/dev/sda4           976      1953   1000448   83  Linux native
/dev/sda5          1953      2144    195584   83  Linux native
/dev/sda6          2144      8635   6646784   83  Linux native

Save and Exit

To save your partition layout and exit fdisk, type w:

Code Listing 1.1: Save and exit fdisk

Command (m for help): w

Now that your partitions are created, you can continue with Creating Filesystems.

1.  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 is used as default in this handbook, continue with Applying a Filesystem to a Partition. Otherwise, read on to learn about the available filesystems...

Filesystems

The Linux kernel supports various filesystems. We'll explain ext2, ext3, ext4, ReiserFS, XFS and JFS as these are the most commonly used filesystems on Linux systems.

ext2 is the tried and true Linux filesystem but doesn't have metadata journaling, which means that routine ext2 filesystem checks at startup time can be quite time-consuming. There is now quite a selection of newer-generation journaled filesystems that can be checked for consistency very quickly and are thus generally preferred over their non-journaled counterparts. Journaled filesystems prevent long delays when you boot your system and your filesystem happens to be in an inconsistent state.

ext3 is the journaled version of the ext2 filesystem, providing metadata journaling for fast recovery in addition to other enhanced journaling modes like full data and ordered data journaling. It uses an HTree index that enables high performance in almost all situations. In short, ext3 is a very good and reliable filesystem.

ext4 is a filesystem created as a fork of ext3 bringing new features, performance improvements and removal of size limits with moderate changes to the on-disk format. It can span volumes up to 1 EB and with maximum file size of 16 TB. Instead of the classic ext2/3 bitmap block allocation ext4 uses extents, which improve large file performance and reduce fragmentation. Ext4 also provides more sophisticated block allocation algorithms (delayed allocation and multiblock allocation) giving the filesystem driver more ways to optimise the layout of data on the disk. The ext4 filesystem is a compromise between production-grade code stability and the desire to introduce extensions to an almost decade old filesystem. Ext4 is the recommended all-purpose all-platform filesystem.

If you intend to install Gentoo on a small partition (less than 8GB), then you'll need to tell ext2, ext3 or ext4 (if available) to reserve enough inodes when you create the filesystem. The mke2fs application uses the "bytes-per-inode" setting to calculate how many inodes a file system should have. By running mke2fs -T small /dev/<device> (ext2) or mke2fs -j -T small /dev/<device> (ext3/ext4) the number of inodes will generally quadruple for a given file system as its "bytes-per-inode" reduces from one every 16kB to one every 4kB. You can tune this even further by using mke2fs -i <ratio> /dev/<device> (ext2) or mke2fs -j -i <ratio> /dev/<device> (ext3/ext4).

JFS is IBM's high-performance journaling filesystem. JFS is a light, fast and reliable B+tree-based filesystem with good performance in various conditions.

ReiserFS is a B+tree-based journaled filesystem that has good overall performance, especially when dealing with many tiny files at the cost of more CPU cycles. ReiserFS appears to be less maintained than other filesystems.

XFS is a filesystem with metadata journaling which comes with a robust feature-set and is optimized for scalability. XFS seems to be less forgiving to various hardware problems.

Applying a Filesystem to a Partition

To create a filesystem on a partition or volume, tools specific to the chosen filesystem are available:

Filesystem Creation Command
ext2 mkfs.ext2
ext3 mkfs.ext3
ext4 mkfs.ext4

For instance, to create the root (/dev/sda1 in our example), /usr, /var, and /home partitions (/dev/sda4, 5 and 6 in our example, respectively) as ext4, you would use:

Code Listing 1.1: Applying a filesystem on a partition

# mkfs.ext4 /dev/sda1
# mkfs.ext4 /dev/sda4
# mkfs.ext4 /dev/sda5
# mkfs.ext4 /dev/sda6

Activating the Swap Partition

mkswap is the command used to initialize swap partitions:

Code Listing 1.1: Creating a Swap signature

# mkswap /dev/sda2

To activate the swap partition, use swapon:

Code Listing 1.1: Activating the swap partition

# swapon /dev/sda2

Create and activate the swap with the commands mentioned above.

1.  Mounting

Now that your partitions are initialized and are housing a filesystem, it is time to mount them using the mount command. Don't forget to first create the necessary mount directories for every partition you created. For example:

Code Listing 1.1: Mounting partitions

# mount /dev/sda1 /mnt/gentoo
# mkdir /mnt/gentoo/usr
# mount /dev/sda4 /mnt/gentoo/usr
# mkdir /mnt/gentoo/var
# mount /dev/sda5 /mnt/gentoo/var
# mkdir /mnt/gentoo/home
# mount /dev/sda6 /mnt/gentoo/home

Note: 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).

Page updated December 17, 2013

Donate to support our development efforts.

Copyright 2001-2014 Gentoo Foundation, Inc. Questions, Comments? Contact us.