Linux command line partitions

How to partition a drive on Linux

Every hard disk, in order to be accessible under Linux, must have at least one partition on it. A partition is a way to logically separate different sections of a disk. For example, a 4 TB hard drive could have four different 1 TB partitions, and all would appear as separate storage systems under the operating system. Alternatively, a hard disk could simply contain a single partition that spans the entire volume. The configuration is entirely up to the user.

Whatever configuration you decide for your hard disk, setting up partitions is one of the most essential and dangerous task to perform when working with operating systems. It is possible to create new partitions, delete partitions, and to shrink or expand existing partitions. In the sections below, we will assume that you need to add a new partition to a hard disk that is either currently unpartitioned or already contains some partitions.

In this tutorial, we will cover the step by step instructions to partition a hard drive or solid state drive on an existing Linux system. We will show the necessary steps for both a brand new disk and one that already has one or more partitions on the disk. You will see the steps for both command line and GUI methods, so you can follow along with set of instructions you are most comfortable with. Let’s get started.

In this tutorial you will learn:

  • How to add partition to new, blank disk
  • How to add to, delete, and resize an existing partition table
  • How to partition a drive via command line
  • How to partition a drive via GUI

How to partition a drive on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software parted, gparted
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user
Читайте также:  Загрузить на флешку на линуксе

How to partition a drive on Linux via command line

Your steps for partitioning a hard drive on Linux will vary depending on whether your disk already contains existing partitions or not. For example, if your hard disk already contains a partition that spans the entire volume, then you will first need to shrink the existing partition in order to be able to add a new one to the disk.

In the step by step instructions below, we will initialize a disk with a partition table, create a new partition, check the partition alignment, resize a partition, and remove an existing partition. This should cover a variety of scenarios depending on whether you are trying to partition a new disk or one with existing partitions.

Initializing a device with a partition table

The device we will be working with in these steps is /dev/sdX . To run in interactive mode we must launch parted with root permissions, passing as an argument to the command, the path of device we want to operate on, in this case:

GNU Parted 3.4 Using /dev/sdX Welcome to GNU Parted! Type 'help' to view a list of commands. (parted)
(parted) print Error: /dev/sdX: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sdX: 42.9GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags:
(parted) mklabel New disk label type? msdos

Creating a partition

Next, we will create our first partition on the device. We will need to provide the partition type, choosing between primary or extended, the filesystem type (optional), the partition starting point and the partition ending point. Again if not provided directly, those values will be requested interactively. The command to create a partition is mkpart :

(parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? ext4 Start? 0% End? 100%
(parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdX: 42.9GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 42.9GB 42.9GB primary ext4 lba

Checking the partition alignment

The alignment of a partition is a very important factor to optimize performance. With parted , we can check two type of alignments, minimal and optimal . In minimal mode, the program checks that the partition respects the minimum alignment value to physical blocks, while in optimal mode, it checks if the partition is aligned to a multiple of the physical block size, to provide optimal performances. The command to use to perform those checks is align-check :

(parted) align-check alignment type(min/opt) [optimal]/minimal? minimal Partition number? 1 1 aligned

Resizing a partition

Resizing a partition is also a very dangerous operation, especially if the partition already contains a filesystem. Be aware that when changing the size of a partition, parted will never adapt the filesystem to it, therefore, especially when shrinking, you must use the dedicated tools to resize the filesystem in use first. The command used to perform a partition resize is resizepart . Our partition size currently consumes 100% of the disk; if for example, we would like to extend it to cover only 50% of the device instead, we would type:

(parted) resizepart Partition number? 1 End? [42.9GB]? 50% Warning: Shrinking a partition can cause data loss, are you sure you want to continue? Yes/No? yes

Removing a partition

Removing a partition is just as easy. Obviously we should perform such an operation with the greatest amount of caution. The command to use in this case is rm :

(parted) rm Partition number? 1

WARNING
There is no prompt for confirmation, so think twice before running this command to destroy an existing partition.

Читайте также:  Удаление символической ссылки linux

How to partition a drive on Linux via GUI

There are many GUI programs which can also be used to create or edit partitions for a hard disk on Linux. In this tutorial, we will focus on using gparted , which, as the name implies, is the GUI counterpart of the command line parted tool that we used above.

You can use the appropriate command below to install gparted with your system’s package manager.

To install gparted on Ubuntu, Debian, and Linux Mint:

To install gparted on Arch Linux and Manjaro:

After it is installed, follow the steps below to use gparted to create, resize, and delete partitions:

    Get started by searching for the gparted application in your desktop envrionment’s app launcher. You will be prompted for the root password upon opening the program.

Search for and open the gparted appYou must supply your root password in order to use gparted

  • The first thing we need to do is select the correct device that we will be working with from the upper right corner. On our test system, this would be /dev/sdb as indicated in the screenshot below. First select the correct hard disk to work with in gparted
  • Create new partition

    As you can see, your disk currently has no partitions. Let’s add one by going to Device > Create Partition Table. Select to create a new partition table from the Device menu

  • Next, select the type of partition you would like to create. We will by sticking with msdos in our tutorial but feel free to pick something else. GPT is recommended for hard disks greater than 2 TB in size. Select partition type and confirm that these changes will erase current disk data
  • Now that the partition table has been created, we can move forward with adding a new partition to the hard disk. This option can be found by heading to Partition > New.

    Читайте также:  Arch linux wifi broadcom

    Select to create a new partition
    On this menu, we get to select the size of our new partition. Rather than working with exact values, feel free to use the sliders with your mouse in order to configure the size that you want. For our example, we will simply make one partition that spans the entire size of the hard disk. Click ‘Add’ when ready to proceed. Select size of partition and optionally a name

    NOTE
    ext4 is the recommended file system to use on Linux systems, unless you have a special reason to format the disk with some other kind.

    Applying the changes to disk

  • The changes are not yet written to the disk, but we can see in the preview pane the configurations that we are about to apply. Once you are ready, click on Edit > Apply All Operations.
  • Resize a partition

    Click on Partition > Resize in order to resize the currently selected partition. Select the Resize partition option from the menus

  • Enter the new start and end points for your partition. Alternatively, simply use the slider to adjust the size of the partition. In this case, we will shrink our partition to about half the size. Enter values and then click Resize when done
  • You can see the new changes that we have made. To finish writing them, click on Edit > Apply All Operations. Applying the changes to disk and shrinking the partition
  • Delete a partition

    How to delete a partition in gparted

    To delete the partition, just select it in gparted and navigate to Partition > Delete. When done, click Edit > Apply All Operations to confirm the changes.

    Closing Thoughts

    In this tutorial, we saw how to partition a hard drive from command line and GUI on a Linux system. Managing partitions is a dangerous task that should be performed with the utmost caution. The command line and GUI both prove as viable methods for managing partitions, especially in the case of parted and gparted , which are closely related tools.

    Comments and Discussions

    Источник

  • Оцените статью
    Adblock
    detector