Here is everything you ever wanted to know about setting up storage but were afraid to ask.

On most computer systems, Linux or otherwise, when you plug in a USB device, you’ll notice an alert indicating the presence of the drive. If the drive is already partitioned and formatted the way you want, you just need your computer to list the drive in a file manager or somewhere on your desktop. This is a simple requirement, and usually a computer can fulfill it.

Sometimes, however, the drive is not formatted the way you want it to be. For these, you must know how to find storage devices ready to connect to your computer.

What is a block device?

Hard drives are often referred to as “block devices” because hard drives read and write in fixed-size blocks. This differentiates hard drives from other devices that might plug into your computer, such as printers, gamepads, microphones, or cameras. An easy way to list the block devices connected to your Linux system is to use LSBLK (list block Devices) :

NAME MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB MB └ ─ luks - e2bb... E9f8 253:237.5 G 0 0 0 crypt ├ ─ fedora - root 253:50 G 1 0 0 LVM / ├ ─ fedora - swap 253:5.8 G 2 0 0 LVM/swap └ ─ fedora - home 08.02.7g 0 LVM /home SDB 08.02.7g 0 disk ├ ─sdb1 08.02.7g 0 part 08.02.7g 0 LVM /home SDB 08.02.7g 0 disk ├ ─sdb1 08.02.7g 0 partCopy the code

The leftmost column is the device identifier, each beginning with SD and ending with a letter, starting with a. Each partition on a block device is assigned a number, starting with 1. For example, the second partition on the first device is represented by SDA2. If you’re not sure which partition it is, don’t worry, just read on.

The LSBLK command is non-destructive and is only used for detection, so you can use it without worrying about corrupting the data on your drive.

Use dMESG for testing

If in doubt, you can look at the drive’s volume label in the last few lines of the dmesg command, which shows the operating system’s recent logs (such as inserting or removing a drive). In short, if you want to make sure that the device you are plugging into is /dev/sdc, then plug the device into your computer and run this dmesg command:

$ sudo dmesg | tailCopy the code

The latest drive listed in the display is the one you just plugged in. If you unplug it and run the command again, you can see that the device has been removed. If you plug it in again and run the command, the device will be there again. In other words, you can monitor the kernel’s recognition of the drive.

Understanding the file system

If all you need is the device label, your job is done. But if your goal is to create a usable drive, you must also make a filesystem for that drive.

If you don’t already know what a file system is, it may be easier to understand the concept by looking at what happens when you don’t have one. If you have a spare device drive and don’t have any important data on it, you can follow this experiment. Otherwise, please don’t try it, because it will definitely delete your data by design.

This is also possible when a drive does not have a file system. Once you are sure that a drive has been correctly identified and that nothing important is on it, then you can plug it into your computer — but do not mount it, and if it is mounted automatically, uninstall it manually.

$ su -
# umount /dev/sdx{,1}Copy the code

To prevent catastrophic copy-paste errors, the following example uses the unlikely SDX as the volume label for the drive.

Now that the drive has been unmounted, try using the following command:

# echo 'hello world' > /dev/sdxCopy the code

You can already write data to block devices without having to mount it to your operating system, and you don’t need a file system.

Pull out the data you just wrote, and you can see the raw data on the drive:

# head -n 1 /dev/sdx
hello worldCopy the code

This seems to work fine, but imagine if the phrase “hello world” was a file, and if you wanted to write a new file this way, you would have to:

  1. We know that line 1 already has a file
  2. It takes only 1 line to know that the file already exists
  3. Create a new method to add data later, or override line 1 when you write line 2

Such as:

# echo 'hello world
> this is a second file' >> /dev/sdxCopy the code

Get the first file, unchanged.

# head -n 1 /dev/sdx
hello worldCopy the code

However, getting the second file becomes a bit more complicated.

# head -n 2 /dev/sdx | tail -n 1
this is a second fileCopy the code

Obviously, reading and writing data this way wasn’t practical, so the developers created a system to track the composition of a file, identify the beginning and end of a file, and so on.

Most file systems require a partition.

Create partitions

A partition is a kind of boundary on a hard drive that tells the file system what space it can occupy. For example, if you have a 4GB USB drive, you can have just one partition for one drive (4GB), or two partitions, 2GB each (or one 1GB and one 3GB if you want), or three different sizes, etc. The combination would be endless.

Assuming your drive is 4GB, you can use the GNU Parted command to create a large partition.

# parted /dev/sdx --align opt mklabel msdos 0 4GCopy the code

As required by the Parted command, the path to the drive is first specified.

— The align option causes the Parted command to automatically select an optimal start and end point.

The mklabel command creates a partition table (called disk volume label) on the drive. This example uses the MSDOS disk volume label because it is a very compatible and popular volume label, although GPT is becoming more and more common.

Finally, the starting and ending points required for partitioning are defined. Since the –align opt flag is used, Parted adjusts the size as needed to optimize the performance of the drive, but these numbers are still useful as a guide.

Next, create the actual partition. If your choice of starting and ending points isn’t optimal, Parted warns you and lets you make adjustments.

# parted /dev/sdx -a opt mkpart primary 0 4G Warning: The resulting partition is not properly aligned for best performance: 1s % 2048s ! = 0s Ignore/Cancel? C # parted /dev/sdx -a opt mkpart primary 2048s 4GCopy the code

If you run LSBLK again (you may have to unplug the drive and plug it back in), you will see that you now have a partition on your drive.

Manually create a file system

We have many file systems at our disposal. Some are open source and free, others are not. Some companies refuse to support open source file systems, so their users can’t read from open source file systems, and open source users can’t read from closed file systems without reverse-engineering them.

Despite this particular case, there are still many file systems available, depending on the purpose of the drive. If you want your drive to be compatible with multiple systems, then your only option is the exFAT file system. However, Microsoft has not yet committed exFAT code to any open source kernel, so you may have to install exFAT support in package manager, but Windows and MacOS both support exFAT file systems.

Once you have installed exFAT support, you can create an exFAT filesystem in the partition you created on your drive.

# mkfs.exfat -n myExFatDrive /dev/sdx1Copy the code

Your drive can now be read and written by kernel modules from closed systems and other open source systems (not yet approved by Microsoft).

A common file system in Linux is ext4. But for portable devices, this can be a cumbersome file system because it retains the user’s permissions, which often vary from computer to computer, but it is generally a reliable and flexible file system. As long as you’re familiar with administrative permissions, ext4 is a great file system for portable devices.

# mkfs.ext4 -L myExt4Drive /dev/sdx1Copy the code

Unplug your drive and plug it back in. For portable devices with ext4 file systems, use sudo to create a directory and grant permissions to the user and common groups on the system. If you are not sure which user and group to use, you can also use sudo or root to change the read and write permissions on the offending device.

Using desktop Tools

It’s nice to know how to manipulate and handle your block device when you only have a Linux shell, but sometimes you just want to make one drive usable without doing that much checking. GNOME’s KDE developers have provided some excellent tools to make this process easier.

GNOME Disk and KDE Partition Manager is a graphical tool that provides an all-in-one solution for everything mentioned in this article so far. Start any of them to view all connected devices (in the list on the left), create and resize partitions, and create file systems.




KDE partition manager

Predictably, the GNOME version is simpler than the KDE version, so I’ll demonstrate with the complex version — if you’re willing to get your hands on it, it’s easy to figure out how to use GNOME disk tools.

Start the KDE partition manager tool and enter your root password.

In the leftmost column, select the drive you want to format. If your drive is not listed, make sure it is plugged in and choose Tools > Refresh Devices (or use the F5 key on your keyboard).

Do not continue unless you want to destroy an existing partition table on the drive. After selecting a drive, click New Partition Table on the top toolbar. You will be prompted to choose a volume label for the partition: GPT or MSDOS. The former is more flexible and can handle larger drives, while the latter, like many Microsoft technologies, is the de facto standard with a large market share.

Now that you have a New partition table, right-click your device in the panel on the right and select “New” to create a New partition, setting the type and size of the partition as prompted. This includes partitioning steps and creating a file system.




Create a new partition

To Apply the changes to your drive, click the Apply button in the upper left corner of the window.

Hard drive, easy drive

Hard drives are easy to work with on Linux, even easier if you understand the hard drive language. Since switching to Linux, I’ve been able to handle my hard drives any way I want. Recovering data is also easier as Linux handles the transparency provided by storage.

If you want to experiment and learn more about hard drives, please refer to the following tips:

  1. Back up your data, not just your experimental drive. It only takes a small misoperation to destroy a partition on a critical drive. This is a good way to learn how to rebuild lost partitions, but it’s not very interesting.
  2. Double check that the drive you are locating is the correct one. I often uselsblkTo make sure I didn’t move the drive. (Because it’s easy to remove two drives from two separate USB ports and then reconnect them in a different order, it’s easy to cause them to get new drive labels.)
  3. Take the time to “destroy” the drive you are testing and see if you can restore the data. Recreating partitioned tables or trying to recover data after deleting a file system can be a great learning experience.

For something even more fun, if you have a closed operating system, try using an open source file system on top of it. There are projects that address this compatibility, and trying to get them to work in a reliable and stable way is a good side project.


via: https://opensource.com/article/18/11/partition-format-drive-linux

By Seth Kenlon (lujun9972

This article is originally compiled by LCTT and released in Linux China