Computer literacy, assistance and repair

Proper disk partitioning for Linux. How to connect a new disk in Linux, partition and format partitions How to create a partition for Linux

Today we will learn on our own create disk partitions in Linux Ubuntu during the installation of the distribution on the computer, in other words, manually create partitions hard drive in Ubuntu, which are necessary for the normal functioning of the operating system on a home computer, and we’ll also talk a little about which partition is needed for what.

So, as an example, we will use the current one on this moment version of Linux Ubuntu 17.04, i.e. let's imagine that you are installing Ubuntu 17.04 ( or any other derivative distribution, such as Kubuntu, Lubuntu, Ubuntu MATE and others) and reached the step where you need to select “ type of instalation", i.e. disk partition type, for example: automatic - this is the first item " Erase disk and install Ubuntu", or " Another variant" - this is exactly the type in which we can mark up the disk ourselves; we will now consider it.

Note! In this article we will look at the initial layout of a blank disk, i.e. on which there are no partitions and data yet.

Creating disk partitions during Linux Ubuntu installation

To independently create disk partitions during Linux installations Ubuntu needs to select the installation type " Another variant" and press " Continue».

If you have several physical disks ( I have one) select the one you need and press “ New partition table».


Then we are warned that a new one will be created empty table sections, we click " Continue", i.e. confirm your action.


The first thing we must create is the root partition, i.e. basic ( systemic) section for installing the system. For this we indicate:

  • Size– for the root partition, a minimum of 15 gigabytes; in the future, of course, it is better to specify more, for example, 50 gigabytes. I have a small disk for testing, so I specify 15 gigabytes;
  • New partition type– indicate “ Primary", because this type must be on disk;
  • – indicate “ The beginning of this space»;
  • Use as– here we need to select the type of file system, leaving the default file system Ext4 - this is a journaled file system for Linux operating systems, which is currently most suitable for the file system of the root partition ( yes and for user data);
  • Mount point– we indicate “/”, since this is our root partition.

Click " OK».


Then, in almost the same way, we create a section for user data, i.e. home section. Creating such a section will allow you to store your data ( documents, music, photos and so on) in a separate place that will not need to be copied in any special way ( reserve) during reinstallation ( updates) distribution or even its change.

IN in this case we need to specify:

  • Size– maximum possible, i.e. all the remaining space, as I already said, my test disk is small, so there is little space left;
  • New partition type– indicate “ Logical»;
  • Location of the new section– indicate “ The beginning of this space»;
  • Use as– also select the Ext4 file system;
  • Mount point– specify “/home”.

Click " OK».


Swap partition (swap) we will not create, since in Ubuntu, starting from version 17.04, a swap file is used instead of a swap partition ( like in Windows). By default, its size is 5% of the free disk space, but not more than 2 gigabytes. After installation, the paging file size can be changed at any time.

This is the disk layout that is suitable for home computer, finished, press " Install now».


Confirm making changes to the disk, click “ Continue" and continue installing the distribution.


That's all for me, I hope the material was useful to you, bye!

The first part of the fourth part of the series of tutorials for beginners talks about block devices, partitions and file systems. You will learn to mark HDD using the fdisk utility, create file systems and mount them. Get to know the syntax configuration file fstab.

Thanks to andrewww for translating this part. To be continued...

About the authors

Daniel Robbins

Daniel Robbins is the founder of the Gentoo community and creator of the Gentoo Linux operating system. Daniel resides in New Mexico with his wife Mary and two energetic daughters. He is also the founder and CEO of Funtoo, and has written numerous technical articles for IBM developerWorks, Intel Developer Services, and C/C++ Users Journal.

Chris Houser

Chris Houser has been a UNIX advocate since 1994, when he joined the administrative team at Taylor University (Indiana, USA), where he received a bachelor's degree in computer science and mathematics. He has since worked in a variety of areas, including web applications, video editing, UNIX drivers, and cryptographic security. Currently working at Sentry Data Systems. Chris has also contributed to many free projects, such as Gentoo Linux and Clojure, and co-authored the book The Joy of Clojure.

Aron Griffis

Aaron Griffis lives in the Boston area, where he has spent the last decade working at Hewlett-Packard on projects such as UNIX network drivers for Tru64, Linux security certification, Xen and KVM virtualization, and most recently the HP ePrint platform. When he's not programming, Aaron likes to think about programming problems while riding his bike, juggling bats, or cheering on the Boston Red Sox professional baseball team.

Posted by Hrafn

From time to time people come to me with a search engine with queries about fdisk. Somewhere in the articles this team is mentioned. But I really didn’t have anything about her. And I remember how I struggled with it at one time. Since I am very often too lazy to write my own something, I slowly translate articles that are interesting and useful to me. And now I did the same. Maybe it will be useful to someone. Although, as always, the article does not fully cover the topic, and therefore no one can cancel reading mana

So, another translation. The original, as always, is on Novell Cool Solutions. Read.

Creating partitions via the command line interface is simple and fast, also taking into account the kernel's knowledge of the modified partition table and the presence of a partition scheme, which allows you to get the desired effect without the need to reboot the workstation/server and without the need to use partitioning utilities.

The first step is to see what hard drives are available and which hard drive needs to be partitioned. If your machine has multiple hard drives, fdisk will tell you about this. The two hard drives are shown in the following listing as sda and sdb.

The fdisk command with the -l operator (fdisk -l ) will show you the current partition tables along with the hard drives connected to your workstation or server:

linux-1reo:~ # fdisk -l








Disk /dev/sdb: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 9729 78148161 8e Linux LVM

Disk /dev/sdc: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Once you have determined which hard drive you want to partition, you can run the fdisk command, ending with the hard drive you want (fdisk /dev/sda). In this article, we will use the first hard drive − sda.

linux-1reo:~ # fdisk /dev/sda
The number of cylinders for this disk is set to 9729.
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)Command (m for help):

Once fdisk is running, your command prompt will change to "Command (m for help): " and you'll be ready to check and partition your hard drive. The commands that fdisk supports can be viewed by pressing the m key and then Enter. The commands we will use are:

  • p - to print the partition table
  • n - to create a new partition
  • w - to save changes and exit

Displaying the current partition table

Before we start dividing the hard drive, we need to find out how much disk space is available to us and what the current partition table generally looks like. "p", the command we will use will display the current partition table:

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1402 11261533+ 7 HPFS/NTFS
/dev/sda2 1403 1415 104422+ 83 Linux
/dev/sda3 1416 1546 1052257+ 82 Linux swap / Solaris
/dev/sda4 1547 9729 65729947+ 5 Extended
/dev/sda5 1547 7920 51199123+ 8e Linux LVM

The output above shows that we have 1809 cylinders available (9729 - 7920 = 1809) and we can do the calculation (1809 * 16065 * 512) = 14879531520bytes, which is approximately 14 gigabytes.

Creating a new partition

The next step is to create our new section. For example, we will create a partition with the type “Linux” and the size of 1 gigabyte. We use the "n" command to create a new partition. We will be asked which cylinder the partition will start from (I recommend leaving it as default) and the required partition size:

Command (m for help): n
First cylinder (7921—9729, default 7921):
Using default value 7921
Last cylinder or +size or +sizeM or +sizeK (7921—9729, default 9729): +1024M

Once the partition is created, we can use the “p” command to display our new partition table on the screen:

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1402 11261533+ 7 HPFS/NTFS
/dev/sda2 1403 1415 104422+ 83 Linux
/dev/sda3 1416 1546 1052257+ 82 Linux swap / Solaris
/dev/sda4 1547 9729 65729947+ 5 Extended
/dev/sda5 1547 7920 51199123+ 8e Linux LVM
/dev/sda6 7921 8045 1004031 83 Linux

Writing a new partition to disk

Now that the partition has been successfully created, you can write the changes to disk by pressing the "w" key and then Enter:

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.

Activating the newly created partition

Once the new partition table is written to the hard disk, the kernel is able to read the new partition table without the need for a reboot. The first step is to create a mount point for the new partition, in our example we will use /media/newpart, and also use the "partprobe" command so that the kernel can re-read the partition table:

Linux-1reo:~ # mkdir /media/newpart
linux-1reo:~ # partprobe

Now that the new mount point has been created and the kernel has reread the partition table, you can format the partition and place the file system of your choice (ext2, ext3, reiserfs, etc.) on it. In our example, we chose reiserfs:

linux-1reo:~ # mkfs.reiserfs /dev/sda6
mkfs.reiserfs 3.6.19 (2003 www.namesys.com)

A pair of credits:
The Defense Advanced Research Projects Agency (DARPA, www.darpa.mil) is the
primary sponsor of Reiser4. DARPA does not endorse this project; it's just
sponsors it.

Alexander Lyamin keeps our hardware running, and was very generous to our
project in many little ways.

Guessing about desired format... Kernel 2.6.16.21−0.8-default is running.
Format 3.6 with standard journal
Count of blocks on the device: 126496
Number of blocks consumed by mkreiserfs formatting process: 8215
Blocksize: 4096
Hash function used to sort names: "r5″
Journal Size 8193 blocks (first block 18)
Journal Max transaction length 1024
inode generation number: 0
UUID: e20e2dc1−7277−4ab1−930c-038e54548540
ATTENTION: YOU SHOULD REBOOT AFTER FDISK!
ALL DATA WILL BE LOST ON ‘/dev/sda3′!
Continue (y/n):y
Initializing journal — 0%….20%….40%….60%….80%….100%
Syncing...ok
ReiserFS is successfully created on /dev/sda3.
linux-1reo:~ #

Once the size is formatted, you can mount and use your newly created partition:

linux-1reo:~ # mount /dev/sda6 /media/partnew

Conclusion

After mounting the created and formatted partition, you can store your files on this partition without any problems, you can also add your new partition to the /etc/fstab file so that your partition is mounted after every reboot.

Tested on:

  • SUSE Linux Enterprise Desktop 10
  • SUSE Linux Enterprise Server 10

Relevant for all versions of Ubuntu.
The file system structure in Linux is different from Windows.

In Windows the drives are c: d:, in Linux it is just a folder.
Everything, including devices, has files.

The root of the file system is designated / , which contains many folders that only the administrator (root) has access to.
The only folder accessible to a simple user is the folder /home/user, which contains all the user's files and folders, including user configuration files.
To share system files and custom ones, usually /home placed in a separate section. It turns out that it’s analogous windows disk d:.
When reinstalling the system, including updating to new version, you can safely format the system partition and leave the user one untouched.

Hard drives in Ubuntu are called /dev/sda, /dev/sdb etc.

Partitions on hard drives /dev/sda1, /dev/sda2 etc.

For Ubuntu I create three partitions:

/dev/sda1 - / ~15GB root, system partition;
/dev/sda2 - swap~4GB, in size random access memory, swap partition;
/dev/sda3 - /home all remaining space, user section.

All manipulations with the disk can also be conveniently carried out in the GParted program, available on the Ubuntu live disk.
Just press the WIN key and enter gparted in the search. And during installation, all that remains is to select mount points and file systems.

Click "New partition table", then the plus sign creates a partition,

We indicate the size as agreed: 15 GB. Use as Ext4 Journaled File System. Mount point / slash.
This is the root partition.

Select the swap partition.

And a section under home. Where the user's files will be stored.

If you install Ubuntu next to Windows, then most likely the first partitions will be used under Windows, then our partitions will be called /dev/sda3 and so on.

We will use a journaled file system ext4.
You can read in more detail.

It is worth noting that Ubuntu works great with ntfs out of the box, though slower.
Therefore, if you install Ubuntu next to Windows and plan to use it often, then you can leave the d: drive large and store all the information on it.

When installing, you need to select a disk with ntfs

and select the mount point, the folder in which the disk will be available, select /home/username/foldername, for example /home/goodigy/disk_d

After installing the OS, Windows disks will be automatically mounted in the specified folder.
All this can be done after installing Ubuntu.

Let me remind you that the MBR partition table can only contain 4 primary partitions; if you need more, you need to create an extended one, and there are as many logical ones in it as you like.
If installed next to Windows, this will happen. .

Ubuntu can be installed on logical partitions and anywhere on the disk.

In this short article I will show you with a practical example how to work with disks in Linux.

Disk activation in Linux

Suppose you connected a new disk that does not yet have a file system. The system does not see it and we need to run commands to connect it.

Let me say right away that all the actions described here can be performed in commands with a graphical interface. I will mark and format the disks in command line- this is a more universal method, as it allows you to configure disks and partitions even in operating systems, which do not have a graphical environment.

When connecting a new drive we need:

  • divide it into sections
  • format each partition to create a file system
  • mount the disk to be able to write and read data
  • configure automatic mounting when you turn on the computer

How to find out disk names, how to view all disks in the system

We need to start by finding out the name of the disk that we will change. In Linux it is completely different from Windows system disk designations. Instead of the usual drives C, D, E and so on, in Linux disks these are the devices in the folder /dev/. In dev, in general, all possible devices, even very exotic ones that do not exist in Windows.

To view available drives, run the command:

Sudo fdisk -l

As can be seen from the screenshot, there is a disk /dev/nvme0n1. Then two devices are listed /dev/nvme0n1p1 And /dev/nvme0n1p2. By matching part of the name nvme0n1 you can understand that these are the partitions into which the disk is divided /dev/nvme0n1.

There is also a disk /dev/sda, which is not divided into any sections - this is what I will connect.

Disk partitioning (partitioning) in Linux

In the console, the disk can be partitioned with the command cfdisk. After it, specify the name of the disk with which you want to perform actions:

Sudo cfdisk /dev/sda

The disk may be GPT or MBR(shown as dos). You can easily find a lot of information about these partition tables on the Internet. I’ll just note that GPT is more modern and has more features. Therefore, if you don’t need to work with old hardware that only understands MBR, then choose GPT.

To create a disc, select New:

Enter its size.

If necessary, change the partition type:

Then move down to the free space and repeat these steps to create partitions. When you're done, select Record.

Formatting Partitions

To format in ext4:

Sudo mkfs.ext4 /dev/partitionname

To format in ext3:

Sudo mkfs.ext3 /dev/partition_name

To format in ext2:

Sudo mkfs.ext2 /dev/partitionname

To format in FAT32:

Sudo mkfs.fat -F32 /dev/partitionname

My example (I did not partition the disk):

Sudo mkfs.ext4 /dev/sda

By the way, if you want to format a partition that you are already using, you must first unmount it (more on this later).

Mounting and unmounting disks

With disks in /dev/ You cannot work directly, that is, you cannot write files to them or copy files from them. To work with them they need to be mounted. This is done by the team mount.

The disk is connected to the mount point and in this folder you can work with the contents of the disk.

This concept may seem foreign to Windows users, but it's very convenient! The disk can be mounted in any folder. For example, the home folder can be on another drive - convenient when reinstalling the system - the data does not need to be copied to a safe place, since it is already on another drive.

To mount, use the following command:

Sudo mount /dev/partition_name /mount/mount point/

Let's say I want a new disk to be mounted (mounted) to a folder /mnt/disk_d. You can choose any name and the mount point does not have to be in the directory /mnt/– can be done in your home folder or in any other folder.

We start by creating the directory in which the disk will be mounted:

Sudo mkdir /mnt/disk_d

We mount:

Sudo mount /dev/sda /mnt/disk_d

We allow everyone access to this disk so that regular user I could also watch and write files there:

Sudo chmod 0777 /mnt/disk_d

In fact, access rights can be configured more finely - without giving full permission to everyone.

To unmount a disk, use any of two commands:

Sudo umount /dev/partitionname

Sudo umount /mountpoint/

Automatically mount disk when Linux boots

Disks for mounting at starting Linux are registered in the file /etc/fstab.

Let's say I want to mount the disk at boot /dev/sda to the directory /mnt/disk_d/. At the same time, I want the disk to be readable and writable by the user. Then I create a directory - a mount point:

Sudo mkdir /mnt/disk_d

I open the file /etc/fstab:

Sudo gedit /etc/fstab

and add the following line there:

/dev/sda /mnt/disk_d ext4 rw,relatime 0 0

In this line you need to replace

  • /dev/sda- to the disk you want to mount
  • mial- in your username
  • /mnt/disk_d- to your mount point that you chose for your disk.
  • ext4- to the file system of your disk

If you are not sure about the file system or its designation, then run the command:

Lsblk -f

As a result, the disk will be automatically mounted every time the system starts.

To allow your user to access this folder without having to escalate their privileges, run the command:

Sudo chown -R $USER /mnt/disk_d

You can fine-tune access rights - for example, by creating a group in which only members will have access to the disk.

Please note that if you did something wrong in the file /etc/fsta, then the system will not be able to boot into GUI and you will have to fix everything from the console. Link to additional instructions at the end of this article.

Connecting removable media (flash drive, external drive) in Linux

By the way, many Linux distributions automatically mount them when you connect a USB flash drive or external drive. Automounting is usually a feature of the desktop environment. That is, on the same Linux distribution, but with different desktops (Linux Mint versions as an example), automounting may or may not be present.

If this does not happen, then you need to mount it manually. This process is no different from normal disk mounting: also create a mount point and use the command mount.

Removable media can be mounted via /etc/fstab, there is even a special option for this nofail— ignore errors if the disk is missing.

How to view all drives and mount points

To do this, use the command we are already familiar with:

How to delete disk partitions

If you want to delete the entire contents of a disk, including its partitioning, then this is done like this:

Open the drive in gdisk:

Gdisk /dev/disk

To switch to expert mode, enter

Then to remove GPT enter

Agree twice to completely clear the drive.

Remounting a drive with write permissions

Sometimes disks are mounted with read-only permissions. In this case, you can copy files from the disk, but you cannot write or delete anything to it. You can remount the disk for writing with one command:

Sudo mount -rw -o remount /dev/sdb1

In it /dev/sdb1 replace with the name of your section.

If you encounter an error:

Mount: /run/media/mial/New volume: /dev/sdb1 is write-protected but explicit read-write mode requested.

Then you need to run the command (replace /dev/sdb1 to your section):

Sudo hdparm -r0 /dev/sdb1

Example output:

/dev/sdb1: setting readonly to 0 (off) readonly = 0 (off)

After that, remount the disk.

note what if you mount the disk with file system NTFS, then you must have the package installed ntfs-3g, otherwise, no matter what you do, the disk will be read-only.

Conclusion

If you still have questions, ask them in the comments.

Some more tips and problem cases are discussed in the article “Working with disks in BlackArch (mounting, solving problems)” - this article is suitable for all distributions, not just BlackArch.

Related publications