Home Wiki > SDB:Partitioning
Sign up | Login

SDB:Partitioning

tagline: From openSUSE

Tested on openSUSE Recommended articles Related articles
Icon-checked.png

Icon-manual.png Icon-help.png

Partitioning Schemes

Linux requires at least one partition, assigned to /, which is the root filesystem. But it is common to have separate partitions for particular directories, like /home. The different partitions you create, are connected to the / partition, by mounting them to mountpoints that you choose. The partitioning scheme in Linux depends on the purpose of your installation. For a standard, general use installation, you can have a /, a /boot, a /home and a swap partition. However, there are many different setups for different purposes, so here is a list of some directories that can be a separate partition:

/boot - The partition mounted on /boot contains the operating system kernel, along with files used during the bootstrap process. For most users, a 500MB boot partition is sufficient.

swap - Should normally be twice the size of the RAM installed on your system (see section no.3).

/home - It’s useful to put users’ home directories on a separate partition. Even if the root partition is corrupted or destroyed, user data has a good chance of remaining intact. Conversely, the system can continue to operate even after a user’s /home directory fills up.

/usr - Holds the bulk of the system’s libraries and data. Creating /usr as a separate partition makes updating the system easier if you need to reinstall the system from the beginning.

/opt - If you install third-party products, you may want to have /opt in a separate partition. This will keep the third-party apps during system upgrades.

/srv - If you use your system as a server, create a /srv partition, since it contains the web and the FTP servers.

/tmp - Putting /tmp on a separate filesystem limits temporary files to a finite size and saves you from having to back them up.

/var - Since log files are kept in /var, it’s a good idea for it to be a separate disk partition. Leaving /var as part of a small root partition, makes it easy to fill the root and brings the machine to a halt.

Note: The filesystem structure may differ in different distributions, so you may need to consult the web site of the distribution of your choice, for details on usage of a specific directory. Here, the /srv directory is used in the openSUSE distribution, to serve web content. Other distributions, like Debian, Ubuntu, RHEL, use the /var/www directory for serving web content.


Partition Tables

MBR

MBR (Master Boot Record) lives in the first sector (512 bytes) of the Hard Drive. It consists of 3 parts:
1 - The bootloader, which resides in the first 446 bytes.
2 - The partition table resides in the next 64 bytes (4 entries of 16 bytes each, one entry for every primary partition).
3 - The last 2 bytes contain the boot record signature (55 AA).

MBR is limited to 2TB Drive size. You can have only 4 primary partitions, or 3 primary partitions and an extended partition, which you can partition further on, to your needs (up to 24 logical partitions). It is used in computers that have BIOS (Basic Input/Output System).

Partitioning under MBR

The partitioning tool used under MBR, is called fdisk.

Note: In order to perform this procedure, you need to have administrative permissions.

First, if you don’t know your drive name, you can list all drives in the system like this:

# fdisk -l

If you know your drive name, select it with fdisk , to edit its partitions:

# fdisk /dev/sdX
Welcome to fdisk (util-linux 2.28).                                   
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):

Note: There is no (sdX) device, but is used here conventionally. SATA drives are listed as /dev/sdX, for example /dev/sda, /dev/sdb, etc., and the partitions inside a drive, are listed as sda1, sda2, or sdb1, sdb2, and so on. IDE drives are listed as /dev/hdX, for example /dev/hda, /dev/hdb, etc., and the partitions inside an IDE drive, are listed as hda1, hda2, or hdb1, hdb2, and so on.

Once you are in the fdisk prompt, it is recommended that you first see the current partitions. In the example below is shown a 15 GiB USB, which contains two partitions, /dev/sdb1 and /dev/sdb2:

Command (m for help): p
Disk /dev/sdb: 15 GiB, 16131293184 bytes, 31506432 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
Disklabel type: dos
Disk identifier: 0xc8e0e6ee

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1  *        0 1638399 1638400  800M  0 Empty
/dev/sdb2       13200   37775   24576   12M ef EFI (FAT-12/16/32)

If you want to delete a partition, type d. If you want to add a new partition, without touching the existing ones, type n. After pressing n, you can choose what partition type you want, primary (p) or extended (e). Than you enter the partition number (press Enter to accept the default). Next you can select the first sector of the partition (it is safe to accept the default, because fdisk selects the next sector that comes after the last partition in the disk). Than, for the last sector, accept the default if you want the partition to take all the remaining space on the disk, or specify the amount of space in sectors or KiB, MiB, GiB, etc. In the example shown, a primary partition is created and is assigned 5GiB of space.

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p

Using default response p.
Partition number (3,4, default 3): 
First sector (1638400-31506431, default 1638400): 
Last sector, +sectors or +size{K,M,G,T,P} (1638400-31506431, default 31506431): +5G

Created a new partition 3 of type 'Linux' and of size 5 GiB.

Now, if you want to change the partition type, you can do so by typing t. Than specify the number of the partition you want to change. Afterwards, specify the partition type. In the example, the partition no.3 is changed to Windows FAT32.

Command (m for help): t
Partition number (1-3, default 3): 
Partition type (type L to list all types): L

Partition type (type L to list all types): b

Changed type of partition 'Linux' to 'W95 FAT32'.

In this example, the remaining space is assigned to an extended partition. The procedure is similar with that of a primary partition. After creation, you can continue to add more logical partitions.

Command (m for help): n
Partition type
   p   primary (3 primary, 0 extended, 1 free)
   e   extended (container for logical partitions)
Select (default e): 

Using default response e.
Selected partition 4
First sector (12124160-31506431, default 12124160): 
Last sector, +sectors or +size{K,M,G,T,P} (12124160-31506431, default 31506431): 

Created a new partition 4 of type 'Extended' and of size 9.2 GiB.

Command (m for help): n
All primary partitions are in use.
Adding logical partition 5
First sector (12126208-31506431, default 12126208): 
Last sector, +sectors or +size{K,M,G,T,P} (12126208-31506431, default 31506431): +3.2G

Created a new partition 5 of type 'Linux' and of size 3.2 GiB.

Now, the partition table for the USB, looks like this:

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1  *           0  1638399  1638400  800M  0 Empty
/dev/sdb2          13200    37775    24576   12M ef EFI (FAT-12/16/32)
/dev/sdb3        1638400 12124159 10485760    5G  b W95 FAT32
/dev/sdb4       12124160 31506431 19382272  9.2G  5 Extended
/dev/sdb5       12126208 18827263  6701056  3.2G 83 Linux

After finished creating partitions, you have to write the partition table to the disk, by typing w.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

If you see a message like the following: WARNING: Re-reading the partition table failed with error 16: Device or resource busy, the most likely reason is that a partition from the disk is still mounted. Unmount the partition (in this case /dev/sdb), and try running the following command to sync the disk partition table with the kernel:

# umount /dev/sdb
# partprobe /dev/sdb
Warning If you make a mistake partitioning your disk with fdisk, just type (q) to exit without saving your changes. If you are stuck in the middle of an operation, such as adding a partition, just complete that operation, and then type (q) after you see the command prompt (Command (m for help): q).

Now that the partitions are created and the partition table written to disk, the last step is to write a filesystem on those partitions. For this task, the mkfs command is used, together with the name of the filesystem (if you don't specify a filesystem, mkfs creates an ext2 filesystem by default). In this example, the two partitions (in this case /dev/sdb3 and /dev/sdb5) will be formatted, one as a FAT32 partition and the other as an ext4 partition.

# mkfs -t vfat /dev/sdb3
# mkfs -t ext4 /dev/sdb5

Note: A primary partition, is a partition used to boot an operating system. An extended partition, is a partition which holds logical partitions. A logical partition, is any partition beyond the 4(MBR)/128(GPT) primary partitions, which use space from an extended partition.


GPT (GUID)

The GPT (GUID) partition table is stored in multiple locations on the disk. It’s called GUID Partition Table because every partition on your drive has a globally unique identifier, or GUID. GPT-based drives can be much larger, with size limits dependent on the operating system and its file systems. You can have up to 128 primary partitions and than, extended partitions. In the GPT partitioning, the backup partition table is stored at the end of the disk. GPT is used on computers that have UEFI (Unified Extensible Frimware Interface). On computers that have BIOS, GPT acts like MBR (BIOS sees GPT as a drive with a single MBR partition).

Partitioning under GPT

The partitioning tool used under MBR, is called gdisk.

Note: In order to perform this procedure, you need to have administrative permissions.

In this example, the 15GiB USB is used again. Currently, the USB contains an MBR partition table and has a single partition. The procedure of partitioning, is similar with that of the fdisk. First, it is recommended that you start by listing the drives, by typing gdisk -l, to check your drive name.

Note: If you try to list the drives on a system that has an MBR table, you may encounter an error.

GPT fdisk (gdisk) version 0.8.8

Problem opening -l for reading! Error is 2.
The specified file does not exist!

To get around this, just use fdisk to list your drives.

Than, select the drive with gdisk.

Note: In this case, the USB has an MBR partition table, so gdisk throws a warning! First, gdisk scans for partition tables. Below, the scan show that there is an MBR table.

Warning If you type w (write) in the command prompt, it will just convert the MBR table to GPT. This action is destructive and causes data loss, so first, you may want to backup any important data.
# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.8

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************


Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

Now, lets select the USB again, to create some partitions! In the example, the USB will have a single partition. To see a list of options, type ? and to create a new partition, type n. It is safe to accept the default first sector. For the last sector, you can specify the amount of space in KiB, MiB, GiB, (for example +2G, for 2GiB) etc. After that, type L, to list the filesystem types and their codes. In this case, the selected code is 0700, which is an exFAT filesystem. Afterward type w to write the partition table (if you want to check the partition table first, type p).

# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.8

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-31506398, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-31506398, default = 31506398) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 0700                                                                                                                         
Changed type of partition to 'Microsoft basic data'                                                                                                                            

Command (? for help): p
Disk /dev/sdb: 31506432 sectors, 15.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 1D24896C-7AA3-4713-80E0-7DAEA507DD0F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 31506398
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        31506398   15.0 GiB    0700  Microsoft basic data

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

Now, that the partitioning is done and the partition table written to disk, the last step is to write a filesystem. For this task, the mkfs command is used, together with the type of the filesystem and the partition name (in this case /dev/sdb1).

# mkfs.exfat /dev/sdb1
mkexfatfs 1.2.4
Creating... done.
Flushing... done.
File system created successfully.

So far, the MBR table on the USB was converted to GPT. After that, the USB was partitioned with a single partition and an exFAT filesystem on it. Now, lets see how to convert back from GPT to MBR, using gdisk. To do this, first you select the drive with gdisk. Than type x, for expert functionality and than z, to destroy GPT data structures. When prompted to blank out MBR, type Y to wipe it and start new, or N to keep it. In the later case, the drive will end up with a single EFI partition. Afterward, you can launch fdisk and change the type of the partition to what you like. In this example, only the GPT is destroyed.

# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.8

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): x

Expert command (? for help): z
About to wipe out GPT on /dev/sdb. Proceed? (Y/N): Y
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
Blank out MBR? (Y/N): N
MBR is unchanged. You may need to delete an EFI GPT (0xEE) partition
with fdisk or another tool.

Now you can change the partitions in the drive, with fdisk.

# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.28).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 15 GiB, 16131293184 bytes, 31506432 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
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1           1 31506431 31506431  15G ee GPT

Command (m for help):

The swap partition type

A swap space is a hard disk swap partition or a swap file, where your computer can “swap out” (the system requires more memory than is physically available) data from RAM that isn’t being used at the moment and then “swap in” (a program requires its files that are in the swap area, to be loaded into RAM) the data back to RAM when it is again needed. Usually, the amount of swap is twice the amount of your physical memory (RAM). Sometimes excessive swapping or thrashing occurs where data is swapped out and then very soon swapped in and then swapped out again and so on. In such situations the system is struggling to find free memory and keep applications running at the same time. In this, case only adding more RAM will help. Although it is better to never exceed your RAM (performance takes a hit when you swap), swapping out is better than having processes just fail.

Creating a swap file

To create a swap area from a file, first you need to create an empty file, with the size of how much swap you want to have. In this example, a 1GB file called swapfile, will be created in the root filesystem, with the dd command. First, you specify the input file if=/dev/zero (/dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it. It is used to create files or memory pages filled with only zeroes.), than the output file, of=/path/to/where_you_want/to_store_swap. The bs=1M means that you will read and write up to 1M (Mega) bytes at a time, and the count=1024 will repeat the writing 1024 times.

# dd if=/dev/zero of=/swapfile bs=1M count=1024

After creation, you need to convert the file into a swap file, with the mkswap command:

# mkswap /swapfile

Than, to make the swap available to the computer, use the swapon command:

# swapon /swapfile

If you want to disable swap, you can do so with the swapoff command:

# swapoff /swapfile

Creating a swap partition

The swap partition, is usually created during the system installation. However, you can create it afterward, if it happens that you didn't create a separate partition for swap, during installation. Follow the procedure for creating partitions (point 2.1.1 and 2.2.1) depending on your system, and don't forget to make it a Linux Swap partition type. After creation, you can format it with the mkswap command, and than enable or disable it with the swapon and swapoff commands respectively.

# mkswap /dev/sdX
# swapon /dev/sdX
# swapoff /dev/sdX

See also