要在Linux中使用(Linux)USB驱动器和硬盘驱动器等存储设备,您还需要了解在使用Linux操作系统时如何构造它们。存储设备通常被分割成单独的部分,称为分区。这使您能够通过将硬盘驱动器拆分为多个虚拟部分来创建文件系统。
Linux磁盘分区就像一个边界设备,它告诉每个文件系统它可以使用多少空间。它在创建共享驱动器(creating shared drives)时非常方便,使您能够更有效地分配和编辑驱动器空间。(edit drive space)
例如,如果您有一个 2GB 的USB驱动器,您可以创建一个占用整个驱动器的分区、两个每个 1GB 的分区,或者大小不同的分区。每个Linux磁盘分区都充当自己的硬盘驱动器。如果您在同一台计算机上使用多个操作系统,这将特别有用。
使用 Parted 命令(Use The Parted Command)
Ubuntu 预装了parted。如果您使用的是不同的发行版,请使用以下命令安装它:
apt-get-install 分开(apt-get-install parted)
要查看系统上的硬盘驱动器,请键入:sudo parted -l。请参阅下面屏幕截图中的设备列表:
您可以在上面看到Disk /dev/sdaUbuntu分区磁盘。让我们使用名为/dev/sda5的分区来创建一个新分区。
下一步是启动parted。但请确保您使用的是 root 权限。选择要分区的驱动器。我们将使用/dev/vdc。
键入以下命令:
(parted) select /dev/vdc
要查看Linux磁盘分区中的内容,请键入:print。您将看到硬盘驱动器、大小和分区表的摘要。
在下面的例子中,硬盘是Model: Virtio Block Device,大小是1396MB,分区表是gpt。
要配置Ubuntu分区磁盘,您必须先键入quit 退出(quit)。下一步是使用parted 打开选定的存储设备。(parted.)在本文中,我们将使用/dev/vdc设备。
如果您不指定要使用的特定设备,您的系统将随机选择一个设备。使用以下包含设备名称 (vdc) 的命令:
sudo parted /dev/vdc
要设置分区表,请输入GPT,然后输入Yes接受它。您应该只在不包含您想要保留的数据的分区上执行此操作。
使用以下命令查看分区表以显示有关存储设备的信息:
(分开)打印((parted) print)
要查看有关如何创建新分区的说明,请键入(parted) help mkpart。
对于本文,我们将使用以下命令创建一个新的Linux磁盘分区:(Linux)
(parted) mkpart 主 0 1396MB((parted) mkpart primary 0 1396MB)
0表示您要在驱动器的开头启动分区。我们从上面的屏幕截图中知道该驱动器有1396MB。上面的命令告诉您的系统从0(0)开始分区并在1396MB结束。
为了能够使用该分区,必须对其进行格式化。首先(First),您需要通过输入quit退出(quit)parted。然后,使用ext4文件系统,键入以下命令来格式化磁盘:
mkfs.ext4 /dev/vdc
通过键入sudo parted /dev/vdc进行验证。要退出parted,请键入quit。当您退出parted时,更改会自动保存。
在命令模式下,使用单个字母命令向您显示可以执行的操作列表。键入m并按Enter。
使用 cfdisk 创建磁盘分区(Create Disk Partitions Using cfdisk)
Cfdisk是一个 Linux 实用程序,用于在磁盘设备上创建、删除和修改分区。要使用它来创建分区,请输入以下命令:
# cfdisk /dev/sda
此示例的驱动器名称为sda。
在上面的屏幕截图中,您可以看到磁盘设备的摘要信息。窗口中间显示分区表。底部的括号显示可选择的命令。
要从列表中选择一个分区,请使用向上和向下箭头键。使用左右箭头选择命令。
上面的示例显示了三个主分区(1,2 和 3)。注意(Notice)可用空间(free space)分区类型。
通过从底部窗口中选择新建(New)来创建一个新分区。我们将此分区称为/dev/sdb。键入命令# cfdisk /dev/sdb。接下来从下一个屏幕中选择主要(primary )作为分区类型。
在下一个屏幕上,您将指定分区的大小。我们将创建一个 800 KB 的分区。现在将要求您确定从哪里开始分区。选择自由空间的开始(beginning of free space)。
在下一个屏幕中,选择写入(Write)以保存更改并将分区数据写入磁盘。通过使用以下命令打印新分区来验证它:
fdisk -l /dev/sdb
创建 Linux 磁盘分区的总结性技巧(Concluding Tips for Creating Linux Disk Partitions)
您应该始终备份您的数据。即使是最小的错误也可能破坏关键驱动器的分区。
此外,请务必在创建分区时验证并重新验证您使用的是正确的驱动器。否则,您可能会丢失数据。
在下面的评论中让我们知道您的问题。
How to Create a Linux Disk Partition
To use storagе devices ѕuch as USB driveѕ and hard driνes in Linux, you need to also understand how to structure them when using Linux operating system. Storаge devicеs are often split into separate portions called partіtіons. This enables you to create a file system by splitting your hard drive into multiple virtual parts.
A Linux disk partition is like a boundary device that tells each file system how much space it can use. It’s handy when creating shared drives and enables you to allocate and edit drive space more effectively.
For example, if you have a 2GB USB drive, you can create a partition that takes up the entire drive, two partitions of 1GB each, or variations of sizes. Each Linux disk partition acts as its own hard drive. It is especially useful if you are using more than one operating system on the same computer.
Use The Parted Command
Ubuntu comes preinstalled with parted. If you are using a different distribution, install it using the following command:
apt-get-install parted
To see the hard drives on your system, type: sudo parted -l. See the list of devices in the screenshot below:
You can see above that there are three Ubuntu partition disks on Disk /dev/sda. Let’s use the partition called /dev/sda5 to create a new partition.
The next step is to launch parted. But be sure you are using root privileges. Choose the drive you want to partition. We will be using /dev/vdc.
Type the following command:
(parted) select /dev/vdc
To see what is in the Linux disk partition, type: print. You will see a summary of your hard drive, the size, and the partition table.
In the example below, the hard drive is Model: Virtio Block Device, the size is 1396MB, and the partition table is gpt.
To configure the Ubuntu partition disk, you must exit first by typing quit. The next step is to open the selected storage device using parted. In this article, we will use the /dev/vdc device.
If you don’t specify the specific device you want to use, your system will randomly select a device. Use the command below that includes the device name (vdc):
sudo parted /dev/vdc
To set the partition table, type GPT, then Yes to accept it. You should only do this on partitions that contain no data you want to keep.
Review your partition table to show information about the storage device with the following command:
(parted) print
To see instructions on how to make a new partition, type (parted) help mkpart.
For this article, we will create a new Linux disk partition using the command below:
(parted) mkpart primary 0 1396MB
The 0 means you want to start the partition at the beginning of the drive. We know from the screenshot above that the drive has 1396MB. The command above tells your system to start the partition at 0 and end it at 1396MB.
To be able to use the partition, it must be formatted. First, you need to exit parted by typing quit. Then, using the ext4 file system, type the command below to format the disk:
mkfs.ext4 /dev/vdc
Verify by typing sudo parted /dev/vdc. To exit parted, type quit. When you exit parted, the changes save automatically.
In command mode, use a single letter command to show you a list of the actions you can take. Type m and press Enter.
Create Disk Partitions Using cfdisk
Cfdisk is a Linux utility program used to create, delete, and modify partitions on a disk device. To use it to create a partition, enter the following command:
# cfdisk /dev/sda
The name of the drive for this example is sda.
In the screenshot above, you can see summary information for the disk device. The middle of the window shows the partition table. The brackets on the bottom show selectable commands.
To select a partition from the list, use the up and down arrow keys. Select a command by using the right and left arrows.
The example above shows three primary partitions (1,2 & 3). Notice the free space partition type.
Create a new partition by selecting New from the bottom window. We will call this partition /dev/sdb. Type the command # cfdisk /dev/sdb. Next select primary as the partition type from the next screen.
On the next screen you will specify the size of the partition. We will create a partition that is 800 KB. Now you will be asked to determine where to start the partition. Choose the beginning of free space.
From the next screen, select Write to save your changes and write the partition data to disk. Verify the new partition by printing it using the following command:
fdisk -l /dev/sdb
Concluding Tips for Creating Linux Disk Partitions
You should always back up your data. Even the smallest mistake can destroy the partition of a critical drive.
Also, be sure to verify and re-verify that you are using the correct drive when creating your partition. Otherwise, you could lose data.
Let us know your questions in the comments below.