How to create partition and file system in Linux

Time:2020-10-24

Creating partitions or new file systems in Linux usually means one thing: install the Gnome parted partition editor (GParted). For most Linux users, this is the only way. However, have you considered creating these partitions and file systems on the terminal? Certainly. Here’s how!

Creating a basic Linux partition using cfdisk
Here’s the right way to create a basic Linux partition on the command line. The first thing to do is turn on your terminal first. If you have it open, you need to find the disk you want to create the partition on. This can be found with a simple command.

Copy code

The code is as follows:

lsblk

2015124114813499.png (692×125)

When you run lsblk, you should see a detailed list of each disk on the current system. Take a look at the list and find out which disk you want to use. In this article, I’ll use SDB for demonstration.

Enter the command at the terminal. It will display a powerful partition based program.

Copy code

The code is as follows:

sudo cfdisk /dev/sdb

2015124114835594.png (700×480)

Note: replace the SDB with the disk you want to use from the lsblk command output.

When you enter this command, you will enter the partition editor and access the disk you want to change.

Due to the different disk partitions, which depends on the user’s needs, this part of the guide will be on how to establish a separate Linux home / root partition layout.

First, you need to create the root partition. This needs to be split according to the number of bytes on the disk. The disk I tested was 32 GB.

In cfdisk, use the arrow keys on the keyboard to select the space to be allocated. When you find it, use the arrow keys to select [new], and then press enter.
2015124114853600.png (700×413)

The program will ask you to enter the partition size. Once you have specified the size, press enter. This will be called the root partition (or / dev / sdb1).

Next, it’s time to create the home partition (/ dev / sdb2). You need to select some more free partitions in cfdisk. Use the arrow to select the [new] option and press enter. Enter the size of your home partition and press enter to create it.
2015124114910595.png (700×406)

Finally, you need to create the swap partition. As in the previous two times, first find some free partitions and use the arrow to select the [new] option. After that, you can use Linux partition.

Note: swap partitions are usually about the same size as your computer’s memory.
2015124114926374.png (485×690)

Now that you have created the swap partition, you should specify its type. Use the up and down arrows to select it. After that, use the left and right arrows to select [type]. Find the Linux swap option and press enter.
2015124114947098.jpg (700×476)

After all partitions are created. Then you write it to disk. Use the right arrow key, select the [write] option, and then press enter. This will write the newly created distribution directly to disk.

Creating a file system using mkfs
Sometimes, you don’t need an entire repartition; you just want to create a file system. You can use the mkfs command directly on the terminal.
2015124115005057.png (1024×768)

First, find the disk you want to use. Input lsblk at the terminal to find out. It will print out a list, and then just find the partition or drive letter where you want to create the file system.

In this example, I’ll use / dev / sdb1 from the second hard disk as the first partition. You can use mkfs with / dev / SDB (which will use the entire partition).
2015124115026660.png (1024×768)

To create a new file system on a specific partition, just type

Copy code

The code is as follows:

sudo mkfs.ext4 /dev/sdb1

At the terminal. It should be noted that, mkfs.ext4 You can switch to any file system you want to use.

PS: use GParted to adjust partitions
The simplest way to adjust partitions in Linux is to use the GUI GParted program. Although you can run GParted from your regular Linux Installation, it refuses to operate on any currently mounted partition. As a result, you cannot adjust the root (/) or other partitions that are critical to the functionality of the operating system. Here I’ll show you how to use a Linux emergency boot disk to make your changes. After the disk is booted, you can run GParted, just as you would from a regular system, using point and click to tell the software what you want to do. Install the software first. Type GParted directly in the terminal, get the prompt, input sudo apt get install GParted, draw the gourd according to the same, and download from the network.
1. Run an emergency disk
There are many linux emergency disks, such as partedmagic and systemrescue CD (see resources). You can also use your Linux installation media, and many versions include a rescue mode that supports access to GParted or similar utilities. To illustrate, I’ll cover the use of partedmagic 4.11; however, in general, other tools are similar. However, GParted needs to be started in a different way.
Most emergency disks come in the form of X86 (32-bit), but these disks work well in x86-64 (64 bit) computers, even if the system is running a 64 bit version of Linux. The file system data structure is not affected by the CPU architecture. If you want to run on other machines, in addition to a standard personal computer (PC), such as the Macintosh, you need to check that your emergency disk works with your hardware.
In general, you must boot an emergency disk just as you would a Linux installation disk. On some systems, insert the disk and then reboot the computer to perform the task. On some systems, you must press a function key to select the boot device, or adjust a basic input / output system (BIOS) option to boot from the optical drive rather than from the hard disk. The details of how to do this depend on the system and you need to consult the man page for details.
When you boot partedmagic, a boot menu appears. Select default settings (NS from RAM) to boot to the standard system. When the system boots, you can see a desktop with icons for common tools, including a partition editor that starts GParted. Double click the icon to start resizing your partition.
2. Tell GParted about your changes
To adjust a partition, right-click on it and select Restore / move from the drop-down menu. The result is a dialog box as shown. You can use the graphic slider or text input fields to adjust the size and location of the partition.
2015124115056156.jpg (556×297)

Move its start point to the right to make room for the extension of / dev / sdb5.
You also want to move the swap partition (/ dev / sdb6) to the right before resizing / dev / sdb5. Alternatively, you can delete and recreate the swap partition; however, this may require modifying the UUID reference to swap space in / etc / fstab, and when the swap space is moved, you can extend / dev / sdb5.
If you want to make space from the logical partition to the primary partition, you must explicitly adjust the extended partition around the logical partition, and vice versa. You can do this as you would any other partition; however, you will find it easier to click on its list entry than on the chart entry in the partition list above.
GParted does not allow you to adjust the partition you are using. For example, there is a lock icon next to an entry in a partition. You can right-click the partition and select unmount to dismount it. Note that if any of the partitions contained in the extended partition (including swap space) is in use, it is locked in this way.
3. Implement your changes in GParted
After you notify GParted of your changes, you must do so by selecting the Edit > apply all operations menu item or by clicking the apply icon. The result is a progress dialog that outlines what the program is doing. If you decide to undo the operation, you can use GParted’s Undo function or exit directly from the program without applying your changes, then your disk will remain unchanged.
The partitioning operation can take some time to complete, ranging from a second to several hours, depending on the nature of the change and the amount of data moved. Under no circumstances can you interrupt a running operation! If this is done, the file system will be destroyed and the data cannot be recovered.
 

Recommended Today

Mongodb splitchunk caused routing table refresh, resulting in slow response

Mongodb splitchunk caused routing table refresh, resulting in slow response Mongodb sharding instance fromVersion 3.4Upgrade toVersion 4.0In the future, the performance of the insertion was significantly reduced, and a large number ofinsertRequest slow log: 2020-08-19T16:40:46.563+0800 I COMMAND [conn1528] command sdb.sColl command: insert { insert: “sColl”, xxx} … locks: {Global: { acquireCount: { r: 6, w: […]