深入浅出话Linux分区管理

译文
系统 Linux
创建Linux分区是新Linux用户最棘手的任务之一。我在本文中将探讨有关Linux分区的各个主题,将使用不同的Linux分区管理器来列出、创建、调整、扩展和删除Linux分区。

[[262425]]

【51CTO.com快译】创建Linux分区是新Linux用户最棘手的任务之一。我在本文中将探讨有关Linux分区的各个主题,将使用不同的Linux分区管理器来列出、创建、调整、扩展和删除Linux分区。

Linux分区管理器

我们在本文中将使用各种Linux分区管理器对系统存储驱动器执行CRUD(创建或克隆、读取、删除和更新)等操作。如果你是新的Linux用户,可能想尝试一种易于使用的基于GUI的工具。如果你喜欢使用命令行(CLI),我们还会介绍4款基于CLI的分区管理器。

1. 基于GUI的工具

(1)Gparted

1)列出分区

2) 创建分区

3) 调整分区大小

4) 删除分区

2.基于CLI的工具

(1)cfdisk

1)列出分区

2)创建分区

3)调整分区大小

4)删除分区

(2)fdisk

1)列出分区

2)创建分区

3) 删除分区

列出Linux中的驱动器和分区

1. Gparted列出分区

Gparted是最流行最强大的Linux分区管理器。你可以对存储驱动器轻松执行CRUD。

在Gparted中列出分区只需打开应用程序本身。加载所有系统驱动器和分区需要几秒钟。

gparted list linux partitions

图1

2. cfdisk列出分区

cfdisk是一款极易使用的分区管理工具。它基于cli,但提供了易于使用的选项,可以对系统驱动器执行操作。

  1. sudo cfdisk 

只要打开该应用程序,它会列出当前存储驱动器的所有分区。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. >> /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2   19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. /dev/sda3 50782208 63211519 12429312 5.9G 83 Linux 
  10.  
  11. ┌───────────────────────────────────────────────────────────┐ 
  12.  
  13. │Partition type: Linux (83) 
  14.  
  15. │Attributes: 80 
  16.  
  17. │Filesystem UUID: 77518fa2-4624-430d-8965-abf7b328165d 
  18.  
  19. │Filesystem: ext4 
  20.  
  21. │Mountpoint: / (mounted) 
  22.  
  23. └───────────────────────────────────────────────────────────┘ 
  24.  
  25. [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]  
  26. Quit program without writing changes 

3. fdisk列出分区

想用fdisk列出分区,使用fdisk时使用‘l’参数。

  1. sudo fdisk -l  
  2. sandy@LinuxAndPeppermint ~ $ sudo fdisk -l  
  3. Disk /dev/sda: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  4. Units: sectors of 1 * 512 = 512 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes 
  7.  
  8. Disklabel type: dos  
  9. Disk identifier: 0xa51a596a  
  10. Device Boot Start End Sectors Size Id Type  
  11. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  12. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  13. /dev/sda3 50782208 63213343 12431136 5.9G 83 Linux  
  14. /dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  15. /dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  16. Partition table entries are not in disk order

创建Linux分区

1. Gparted创建新分区

在Linux中用Gparted创建分区极容易极快捷。只要选择想要创建分区的驱动器,鼠标右击,点击“新建”。确保未挂载驱动器,否则“新建”选项呈灰色禁用。

​ 输入新分区大小,点击“添加”按钮。

create linux partition gparted

图2

点击“应用”,开始创建分区。

2. cfdisk创建分区

想使用cfdisk创建新分区,以sudo权限运行cfdisk。

  1. sudo cfdisk  
  2. Disk: /dev/sda  
  3. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  4. Label: dos, identifier: 0xa51a596a  
  5. Device Boot Start End Sectors Size Id Type  
  6. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  7. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended 
  8.  
  9. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris 
  10.  
  11. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux 
  12.  
  13. >> Free space 50782208 63213343 12431136 5.9G 
  14.  
  15. [ New ] [ Quit ] [ Help ] [ Write ] [ Dump ]  
  16. Create new partition from free space 

选择你想创建分区的闲置空间。选择“新建”,按回车键。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. >> Free space 50782208 63213343 12431136 5.9G 
  10.  
  11. Partition size: 5.9G  
  12. May be followed by M for MiB, G for GiB, T for TiB, or S for sectors. 

输入分区大小,按回车键。默认情况下,它会创建全部闲置空间大小的分区。在按回车键之前暂停,看看你是否真想创建全部闲置空间大小的分区。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. >> /dev/sda3 50782208 63213343 12431136 5.9G 83 Linux 
  10.  
  11. ┌──────────────────────────────────────────────────────────────────┐ 
  12.  
  13. │Partition type: Linux (83) 
  14.  
  15. └──────────────────────────────────────────────────────────────────┘ 
  16.  
  17. [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ] 

一切就绪。现在选择“写入”,按回车键。它会让你输入“Yes”,按回车键。这是最后一次确认;出现该提示之后,cfdisk会创建分区。

  1. ┌────────────────────────────────────────────────────┐ 
  2.  
  3. │Partition type: Linux (83) 
  4.  
  5. └────────────────────────────────────────────────────┘ 
  6.  
  7. Are you sure you want to write the partition table to disk? yes_  
  8. Type "yes" or "no"or press ESC to leave this dialog. 

就这样。分区已创建完毕!

  1. ┌─────────────────────────────────────────────────────┐ 
  2.  
  3. │Partition type: Linux (83) 
  4.  
  5. └─────────────────────────────────────────────────────┘ 
  6.  
  7. [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]  
  8. The partition table has been altered. 

3. fdisk创建分区

想使用fdisk创建Linux分区,先列出分区,记下你想创建分区的设备路径。

  1. sudo fdisk -l  
  2. sandy@LinuxAndPeppermint ~ $ sudo fdisk -l  
  3. Disk /dev/sda: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  4. Units: sectors of 1 * 512 = 512 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes 
  7.  
  8. Disklabel type: dos  
  9. Disk identifier: 0xa51a596a  
  10. Device Boot Start End Sectors Size Id Type  
  11. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  12. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  13. /dev/sda3 50782208 63213343 12431136 5.9G 83 Linux  
  14. /dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  15. /dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  16. Partition table entries are not in disk order

我想从/dev/sda3创建一个新分区。然后使用fdisk命令选择分区。

  1. sandy@LinuxAndPeppermint ~ $ sudo fdisk /dev/sda3  
  2. Welcome to fdisk (util-linux 2.31.1).  
  3. Changes will remain in memory only, until you decide to write them.  
  4. Be careful before using the write command.  
  5. Device does not contain a recognized partition table 
  6. Created a new DOS disklabel with disk identifier 0x64d7e7ab.  
  7. Command (m for help): 

想创建新分区,使用‘n’命令。

  1. Command (m for help): n  
  2. Partition type  
  3. primary (0 primary, 0 extended, 4 free 
  4. e extended (container for logical partitions)  
  5. Select (default p): 

选择分区类型。想创建主分区,使用‘p’命令,使用‘e’代表扩展类型。

  1. Select (default p): p  
  2. Partition number (1-4, default 1): 

下一步,它会要求提供分区号。使用默认值1。按回车键。

  1. First sector (2048-12431135, default 2048): 2048  
  2. Last sector, +sectors or +size{K,M,G,T,P} (2048-12431135, default 12431135): +2000M  
  3. Created a new partition 1 of type 'Linux' and of size 2 GiB.  
  4. Command (m for help): 

然后选择新分区的大小。第一个扇区选择默认值,最后一个扇区输入以M为单位的值。比如说,想创建一个2GB大小的分区,输入+2000M。

最后,它会显示消息如下:'​Created a new partition 1 of type 'Linux' and of size 2 GiB. '

但分区仍未创建。想把变更写入到磁盘,使用‘w’命令。它会显示下列消息:"The kernel still uses the old partitions. The new table will be used at the next reboot. Syncing disks."。

​ 若要让新分区出现,需要重启系统。

调整Linux分区大小

1. Gparted调整分区大小

Gparted中调整分区大小很简单。打开该应用程序,选择想要调整大小的分区,右击鼠标,选择“调整大小/移动”。

resize linux partition gparted

图3

输入新分区大小,点击“调整大小/移动”按钮。最后,点击“应用”按钮,使刚完成的变更生效。

2. cfdisk调整分区大小

以sudo权限开启cfdisk。

  1. sudo cfdisk 

选择想要调整大小的分区。选择“调整大小”选项,按回车键。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. >> /dev/sda3 50782208 63211519 12429312 5.9G 83 Linux 
  10.  
  11. ┌──────────────────────────────────────────────────────────┐ 
  12.  
  13. │Partition type: Linux (83) 
  14.  
  15. │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763 
  16.  
  17. │Filesystem: ext4 
  18.  
  19. └──────────────────────────────────────────────────────────┘ 
  20.  
  21. [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]  
  22. Reduce or enlarge the current partition 

输入新分区大小。

  1. ┌─────────────────────────────────────────────────────┐ 
  2.  
  3. │Partition type: Linux (83) 
  4.  
  5. │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763 
  6.  
  7. │ Filesystem: ext4 
  8.  
  9. └─────────────────────────────────────────────────────┘ 
  10.  
  11. New size: 5.9G_  
  12. May be followed by M for MiB, G for GiB, T for TiB, or S for sectors. 

最后,选择写入选项,将变更写入到磁盘。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. >> /dev/sda3 50782208 57073663 6291456 3G 83 Linux  
  10. Free space 57073664 63213343 6139680 2.9G 
  11.  
  12. ┌───────────────────────────────────────────────────────────┐ 
  13.  
  14. │Partition type: Linux (83) 
  15.  
  16. │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763 
  17.  
  18. │Filesystem: ext4 
  19.  
  20. └───────────────────────────────────────────────────────────┘ 
  21.  
  22. [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]  
  23. Write partition table to disk (this might destroy data) 

删除Linux分区

1. Gparted删除分区

想在Linux中使用GParted删除分区,只要选择分区,右击鼠标,选择删除选项,即可删除分区。

delete linux partition gparted

图4

别忘了删除分区后使变更生效。

2. cfdisk删除分区

想使用cfdisk删除分区,以sudo权限开启cfdisk。

  1. sudo cfdisk 

高亮选中想删除的分区,选择“删除”选项。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. >> /dev/sda3 50782208 63211519 12429312 5.9G 83 Linux 
  10.  
  11. ┌──────────────────────────────────────────────────────────┐ 
  12.  
  13. │Partition type: Linux (83) 
  14.  
  15. │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763 
  16.  
  17. │Filesystem: ext4 
  18.  
  19. └──────────────────────────────────────────────────────────┘ 
  20.  
  21. [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]  
  22. Delete the current partition 

最后,选择“写入”选项,将变更写入到磁盘。

  1. Disk: /dev/sda  
  2. Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors  
  3. Label: dos, identifier: 0xa51a596a  
  4. Device Boot Start End Sectors Size Id Type  
  5. /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux  
  6. /dev/sda2   19533822 50782207 31248386 14.9G 5 Extended  
  7. ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris  
  8. └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux  
  9. >> Free space 50782208 63213343 12431136 5.9G 
  10.  
  11. ┌───────────────────────────────────────────────────────────┐ 
  12.  
  13. │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763 
  14.  
  15. │Filesystem: ext4 
  16.  
  17. └───────────────────────────────────────────────────────────┘ 
  18.  
  19. [ New ] [ Quit ] [ Help ] [ Write ] [ Dump ]  
  20. Write partition table to disk (this might destroy data) 

3. fdisk删除分区

想使用fdisk删除分区,先运行fdisk,选择想要删除的分区。

  1. sandy@LinuxAndPeppermint ~ $ sudo fdisk /dev/sda3  
  2. Welcome to fdisk (util-linux 2.31.1).  
  3. Changes will remain in memory only, until you decide to write them.  
  4. Be careful before using the write command.  
  5. Command (m for help): 

然后使用‘d’命令删除所选择的分区。

  1. Command (m for help): d 

删除后,你可以使用‘n’命令创建一个新分区,最后输入‘w’命令,将所有变更写入到磁盘。

结束语

以上就是管理Linux分区的方法。本文介绍了在Linux中使用三大分区管理工具,列出Linux分区、创建Linux分区、调整分区大小以及删除分区。

原文标题:Linux Partition Management

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:张燕妮 来源: 51CTO
相关推荐

2009-11-30 16:46:29

学习Linux

2011-07-04 10:39:57

Web

2021-03-16 08:54:35

AQSAbstractQueJava

2022-09-17 08:09:25

MGR故障检测网络

2009-12-25 15:49:43

Linux rescu

2019-01-07 15:29:07

HadoopYarn架构调度器

2017-07-02 18:04:53

块加密算法AES算法

2012-05-21 10:06:26

FrameworkCocoa

2021-07-20 15:20:02

FlatBuffers阿里云Java

2022-09-26 09:01:15

语言数据JavaScript

2022-01-18 11:06:44

Wi-Fi路由器网络技术

2018-11-09 16:24:25

物联网云计算云系统

2019-11-11 14:51:19

Java数据结构Properties

2012-02-21 13:55:45

JavaScript

2022-12-02 09:13:28

SeataAT模式

2021-04-27 08:54:43

ConcurrentH数据结构JDK8

2022-01-11 07:52:22

CSS 技巧代码重构

2019-12-04 10:13:58

Kubernetes存储Docker

2009-11-18 13:30:37

Oracle Sequ

2022-10-31 09:00:24

Promise数组参数
点赞
收藏

51CTO技术栈公众号