- Partitioning, Formatting, and Mounting a Hard Drive in Linux Ubuntu 18.04 (2020)
- A. Partitioning (for >2GB Harddisk) (New) (Updated on 18 Jan 2020)
- 10 fdisk Commands to Manage Linux Disk Partitions
- 1. View all Disk Partitions in Linux
- 2. View Specific Disk Partition in Linux
- 3. Check all Available fdisk Commands
- 4. Print all Partition Table in Linux
- 5. How to Delete a Partition in Linux
- 6. How to Create a New Partition in Linux
- 7. How to Format a Partition in Linux
- 8. How to Check Size of a Partition in Linux
- 9. How to Fix Partition Table Order
- 10. How to Disable Boot Flag (*) of a Partition
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How To Partition and Format Storage Devices in Linux
- Introduction
- Install the Tools
- Identify the New Disk on the System
- Partition the New Drive
- Choose a Partitioning Standard
- Create the New Partition
- Create a Filesystem on the New Partition
- Mount the New Filesystem
- Mounting the Filesystem Temporarily
- Mounting the Filesystem Automatically at Boot
- Testing the Mount
- Conclusion
Partitioning, Formatting, and Mounting a Hard Drive in Linux Ubuntu 18.04 (2020)
Jul 4, 2018 · 4 min read
I have a brand new 4-TB harddisk to add to my computer recently. So I need to mount it in Linux. (Sik-Ho Tsang @ Medium)
To do this, I need to do the following:
- A. Partitioning Using Parted (for > 2GB Harddisk) (New) (Updated on 18 Jan 2020)
- B. Formatting
- C. Mounting (including auto mount after reboot)
- D. Checking whether the hard drive is mounted
- E. Unmounting (Updated on 05 May 2020)
- F. Partitioning Using fdisk (for Harddisk larger than 2GB, GPT is needed, A > B > C > D.
To partition a Harddisk smaller than 2GB, fdisk already can help, E > B > C > D.
A. Partitioning (for >2GB Harddisk) (New) (Updated on 18 Jan 2020)
- First, after connecting the harddisk to the computer by SATA and power cables, we can check the new 4-TB harddisk by:
Note: You may have a different name other than sdb. The name sdb needs to be changed in the following steps.
2. But we cannot mount it right now, if we mount it now, errors will come out. We need to partition it first, we use parted to partition:
3. Within the parted, type the following to have gpt partition, gpt can allow partition larger than 2GB:
4. Set the size for partition, I here partition from 0GB to 4GB:
Источник
10 fdisk Commands to Manage Linux Disk Partitions
fdisk stands (for “fixed disk or format disk“) is an most commonly used command-line based disk manipulation utility for a Linux/Unix systems. With the help of fdisk command you can view, create, resize, delete, change, copy and move partitions on a hard drive using its own user friendly text based menu driven interface.
This tool is very useful in terms of creating space for new partitions, organising space for new drives, re-organising an old drives and copying or moving data to new disks. It allows you to create a maximum of four new primary partition and number of logical (extended) partitions, based on size of the hard disk you have in your system.
fdisk command to manage disk partition
This article explains 10 basic fdisk commands to manage a partition table in Linux based systems. You must be root user to run fdisk command, otherwise you will get a “command not found” error.
1. View all Disk Partitions in Linux
The following basic command list all existing disk partition on your system. The ‘-l‘ argument stand for (listing all partitions) is used with fdisk command to view all available partitions on Linux. The partitions are displayed by their device’s names. For example: /dev/sda, /dev/sdb or /dev/sdc.
2. View Specific Disk Partition in Linux
To view all partitions of specific hard disk use the option ‘-l‘ with device name. For example, the following command will display all disk partitions of device /dev/sda. If you’ve different device names, simple write device name as /dev/sdb or /dev/sdc.
3. Check all Available fdisk Commands
If you would like to view all commands which are available for fdisk. Simply use the following command by mentioning the hard disk name such as /dev/sda as shown below. The following command will give you output similar to below.
Type ‘m‘ to see the list of all available commands of fdisk which can be operated on /dev/sda hard disk. After, I enter ‘m‘ on the screen, you will see the all available options for fdisk that you can be used on the /dev/sda device.
4. Print all Partition Table in Linux
To print all partition table of hard disk, you must be on command mode of specific hard disk say /dev/sda.
From the command mode, enter ‘p‘ instead of ‘m‘ as we did earlier. As I enter ‘p‘, it will print the specific /dev/sda partition table.
5. How to Delete a Partition in Linux
If you would like to delete a specific partition (i.e /dev/sda9) from the specific hard disk such as /dev/sda. You must be in fdisk command mode to do this.
Next, enter ‘d‘ to delete any given partition name from the system. As I enter ‘d‘, it will prompt me to enter partition number that I want to delete from /dev/sda hard disk. Suppose I enter number ‘4‘ here, then it will delete partition number ‘4‘ (i.e. /dev/sda4) disk and shows free space in partition table. Enter ‘w‘ to write table to disk and exit after making new alterations to partition table. The new changes would only take place after next reboot of system. This can be easily understood from the below output.
Warning : Be careful, while performing this step, because using option ‘d‘ will completely delete partition from system and may lost all data in partition.
6. How to Create a New Partition in Linux
If you’ve free space left on one of your device say /dev/sda and would like to create a new partition under it. Then you must be in fdisk command mode of /dev/sda. Type the following command to enter into command mode of specific hard disk.
After entering in command mode, now press “n” command to create a new partition under /dev/sda with specific size. This can be demonstrated with the help of following given output.
While creating a new partition, it will ask you two options ‘extended‘ or ‘primary‘ partition creation. Press ‘e‘ for extended partition and ‘p‘ for primary partition. Then it will ask you to enter following two inputs.
- First cylinder number of the partition to be create.
- Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).
You can enter the size of cylinder by adding “+5000M” in last cylinder. Here, ‘+‘ means addition and 5000M means size of new partition (i.e 5000MB). Please keep in mind that after creating a new partition, you should run ‘w‘ command to alter and save new changes to partition table and finally reboot your system to verify newly created partition.
7. How to Format a Partition in Linux
After the new partition is created, don’t skip to format the newly created partition using ‘mkfs‘ command. Type the following command in the terminal to format a partition. Here /dev/sda4 is my newly created partition.
8. How to Check Size of a Partition in Linux
After formatting new partition, check the size of that partition using flag ‘s‘ (displays size in blocks) with fdisk command. This way you can check size of any specific device.
9. How to Fix Partition Table Order
If you’ve deleted a logical partition and again recreated it, you might notice ‘partition out of order‘ problem or error message like ‘Partition table entries are not in disk order‘.
For example, when three logical partitions such as (sda4, sda5 and sda6) are deleted, and new partition created, you might expect the new partition name would be sda4. But, the system would create it as sda5. This happens because of, after the partition are deleted, sda7 partition had been moved as sda4 and free space shift to the end.
To fix such partition order problems, and assign sda4 to the newly created partition, issue the ‘x‘ to enter an extra functionality section and then enter ‘f‘ expert command to fix the order of partition table as shown below.
After, running ‘f‘ command, don’t forget to run ‘w‘ command to save and exit from fdisk command mode. Once it fixed partition table order, you will no longer get error messages.
10. How to Disable Boot Flag (*) of a Partition
By default, fdisk command shows the boot flag (i.e. ‘*‘) symbol on each partition. If you want to enable or disable boot flag on a specific partition, do the following steps.
Press ‘p‘ command to view the current partition table, you see there is a boot flag (asterisk (*) symbol in orange color) on /dev/sda1 disk as shown below.
Next enter command ‘a‘ to disable boot flag, then enter partition number ‘1‘ as (i.e. /dev/sda1) in my case. This will disable boot flag on the partition /dev/sda1. This will remove the asterisk (*) flag.
I’ve tried my best to include almost all basic commands of fdisk commands, but still fdisk contains a variety of other expert commands you can use them by entering ‘x‘. For more detailed information, check out ‘man fdisk‘ command from the terminal. If I’ve missed any important command, please do share with me via comment section.
Read Also :
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How To Partition and Format Storage Devices in Linux
Published on July 13, 2016
Introduction
Preparing a new disk for use on a Linux system can be quick and easy. There are many tools, filesystem formats, and partitioning schemes that may complicate the process if you have specialized needs, but if you want to get up and running quickly, it’s fairly straightforward.
This guide will cover the following process:
- Identifying the new disk on the system.
- Creating a single partition that spans the entire drive (most operating systems expect a partition layout, even if only one filesystem is present)
- Formatting the partition with the Ext4 filesystem (the default in most modern Linux distributions)
- Mounting and setting up Auto-mounting of the filesystem at boot
Install the Tools
To partition the drive, we’ll use the parted utility. In most cases, this will already be installed on the server.
If you are on an Ubuntu or Debian server and do not have parted yet, you can install it by typing:
If you are on a CentOS or Fedora server, you can install it by typing:
Identify the New Disk on the System
Before we set up the drive, we need to be able to properly identify it on the server.
If this is a completely new drive, the easiest way to find it on your server may be to look for the absence of a partitioning scheme. If we ask parted to list the partition layout of our disks, it will give us an error for any disks that don’t have a valid partition scheme. This can be used to help us identify the new disk:
You should see an unrecognized disk label error for the new, unpartitioned disk:
You can also use the lsblk command and look for a disk of the correct size that has no associated partitions:
Remember to check lsblk in every session before making changes. The /dev/sd* and /dev/hd* disk identifiers will not necessarily be consistent between boots, which means there is some danger of partitioning or formatting the wrong disk if you do not verify the disk identifier correctly.
Consider using more persistent disk identifiers like /dev/disk/by-uuid , /dev/disk/by-label , or /dev/disk/by-id . See our introduction to storage concepts and terminology in Linux article for more information.
When you know the name the kernel has assigned your disk, you can partition your drive.
Partition the New Drive
As mentioned in the introduction, we’ll create a single partition spanning the entire disk in this guide.
Choose a Partitioning Standard
To do this, we first need to specify the partitioning standard we wish to use. GPT is the more modern partitioning standard, while the MBR standard offers wider support among operating systems. If you do not have any special requirements, it is probably better to use GPT at this point.
To choose the GPT standard, pass in the disk you identified like this:
If you wish to use the MBR format, type this instead:
Create the New Partition
Once the format is selected, you can create a partition spanning the entire drive by typing:
If we check lsblk , we should see the new partition available:
Create a Filesystem on the New Partition
Now that we have a partition available, we can format it as an Ext4 filesystem. To do this, pass the partition to the mkfs.ext4 utility.
We can add a partition label by passing the -L flag. Select a name that will help you identify this particular drive:
If you want to change the partition label at a later date, you can use the e2label command:
You can see all of the different ways to identify your partition with lsblk . We want to find the name, label, and UUID of the partition.
Some versions of lsblk will print all of this information if we type:
If your version does not show all of the appropriate fields, you can request them manually:
You should see something like this. The highlighted output indicate different methods you can use to refer to the new filesystem:
Mount the New Filesystem
Now, we can mount the filesystem for use.
The Filesystem Hierarchy Standard recommends using /mnt or a subdirectory under it for temporarily mounted filesystems. It makes no recommendations on where to mount more permanent storage, so you can choose whichever scheme you’d like. For this tutorial, we’ll mount the drive under /mnt/data .
Create the directory by typing:
Mounting the Filesystem Temporarily
You can mount the filesystem temporarily by typing:
Mounting the Filesystem Automatically at Boot
If you wish to mount the filesystem automatically each time the server boots, adjust the /etc/fstab file:
Earlier, we issued a sudo lsblk —fs command to display three filesystem identifiers for our filesystem. We can use any of these in this file. We’ve used the partition label below, but you can see what the lines would look like using the other two identifiers in the commented out lines:
You can learn about the various fields in the /etc/fstab file by typing man fstab . For information about the mount options available for a specific filesystem type, check man [filesystem] (like man ext4 ). For now, the mount lines above should get you started.
For SSDs, the discard option is sometimes appended to enable continuous TRIM. There is debate over the performance and integrity impacts of performing continuous TRIM in this manner, and most distributions include method of performing periodic TRIM as an alternative.
Save and close the file when you are finished.
If you did not mount the filesystem previously, you can now mount it by typing:
Testing the Mount
After we’ve mounted the volume, we should check to make sure that the filesystem is accessible.
We can check if the the disk is available in the output from the df command:
You should also be able to see a lost+found directory within the /mnt/data directory, which typically indicates the root of an Ext* filesystem:
We can also check that the file mounted with read and write capabilities by writing to a test file:
Read the file back just to make sure the write executed correctly:
You can remove the file after you have verified that the new filesystem is functioning correctly:
Conclusion
Your new drive should now be partitioned, formatted, mounted, and ready for use. This is the general process you can use turn a raw disk into a filesystem that Linux can use for storage. There are more complex methods of partitioning, formatting, and mounting which may be more appropriate in some cases, but the above is a good starting point for general use.
Источник