linux下fdisk创建主分区、逻辑分区和扩展分区

1 主分区、逻辑分区和扩展分区简介

在Linux系统中,主分区、逻辑分区和扩展分区都是磁盘分区的方式,它们各自有不同的特点和应用场景。

主分区:主分区是硬盘的启动分区,也称为引导分区,通常对应于计算机的第一个分区。主分区必须是可用的,也就是说它必须包含有效的文件系统,并且该文件系统必须是活动的。在Linux中,主分区可以有多种文件系统类型,如ext4、ntfs、vfat等。主分区通常用于存储操作系统、应用程序和数据,并且是计算机启动时所加载的第一个分区。

逻辑分区:逻辑分区是在扩展分区内部创建的多个分区。扩展分区不能直接使用,必须划分成多个逻辑分区。逻辑分区可以有多种文件系统类型,并用于存储用户数据、应用程序或系统临时文件等。在Linux中,逻辑分区的创建和管理通常使用fdisk、parted等工具进行。

扩展分区:扩展分区是一种特殊类型的分区,用于容纳逻辑分区。扩展分区本身不能直接使用,必须进一步划分成多个逻辑分区。扩展分区的容量等于其包含的所有逻辑分区的容量之和。在Linux中,扩展分区的创建和管理可以使用fdisk、parted等工具进行。
总结来说,主分区是硬盘的启动分区,必须包含有效的文件系统,并且该文件系统必须是活动的;逻辑分区是在扩展分区内部创建的多个分区,用于存储用户数据、应用程序或系统临时文件等;而扩展分区是一种特殊类型的分区,用于容纳逻辑分区,其容量等于其包含的所有逻辑分区的容量之和。

2 查看分区

2.1 查看分区使用情况

  • fdisk -l :输出所有的分区内容。
[root@logicfeng ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009caea

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1c42ed69

                  Device Boot      Start         End      Blocks   Id  System
/dev/mapper/centos-root1            2048     8390655     4194304   83  Linux

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
  • fdisk -l /dev/mapper/centos-root 观察某个分区/硬盘之实体使用情形
[root@logicfeng ~]# fdisk -l /dev/mapper/centos-root

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1c42ed69

                 Device Boot      Start         End      Blocks   Id  System
/dev/mapper/centos-root1            2048     8390655     4194304   83  Linux
  • 说明
    Device Boot(引导):表示引导分区
    Start(开始):表示一个分区从哪个柱面开始
    End(结束):表示一个分区到哪个柱面结束
    Blocks(容量):表示分区容量,单位KB
    IdSystem:确认分区类型

2.2 进入分区,进入分割硬盘模式

  • fdisk /dev/mapper/centos-root 进入根目录所在磁盘,进入分割硬盘模式
[root@logicfeng ~]# fdisk /dev/mapper/centos-root
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): 
  • 输入m,查看命令介绍
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
  • 说明
  1. 输入 m 显示所有命令列示。
  2. 输入 p 显示硬盘分割情形。
  3. 输入 a 设定硬盘启动区。
  4. 输入 n 设定新的硬盘分割区。
     4.1. 输入 e 硬盘为[延伸]分割区(extend)。
     4.2. 输入 p 硬盘为[主要]分割区(primary)。
  5. 输入 t 改变硬盘分割区属性。
  6. 输入 d 删除硬盘分割区属性。
  7. 输入 q 结束不存入硬盘分割区属性。
  8. 输入 w 结束并写入硬盘分割区属性
  • 输入p
Command (m for help): p

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1c42ed69

                  Device Boot      Start         End      Blocks   Id  System
/dev/mapper/centos-root1            2048     8390655     4194304   83  Linux

Command (m for help): 

3 创建主分区

  • 输入n
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)  # 主分区
   e   extended									# 扩展分区
Select (default p): p                           # 输入p选择主分区,注意到括号中的4free,最多只能建四个主分区(包括扩展分区)
Partition number (1-4, default 1): 1			# 默认选择分区号为1
First sector (2048-35643391, default 2048):     # 直接回车,从默认柱面开始划分
Using default value 2048						# 从2048开始划分
Last sector, +sectors or +size{K,M,G} (2048-35643391, default 35643391): +4G # 大小4个G,不能超过该磁盘剩余的空间否则无效
Partition 1 of type Linux and of size 4 GiB is set
  • 分好后查看分区信息
Command (m for help): p

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1c42ed69

               Device Boot          Start         End      Blocks   Id  System
/dev/mapper/centos-root1            2048     8390655     4194304   83  Linux

4 创建扩展分区

  • 输入n
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e                                          # 选择e为扩展分区
Partition number (2-4, default 2): 2						   # 默认第2
First sector (8390656-35643391, default 8390656):              # 从默认开始8390656
Using default value 8390656									   
Last sector, +sectors or +size{K,M,G} (8390656-35643391, default 35643391): +4G  # 大小4个G
Partition 2 of type Extended and of size 4 GiB is set
  • 查看p
Command (m for help): p

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1c42ed69

                  Device Boot      Start         End      Blocks   Id  System
/dev/mapper/centos-root1            2048     8390655     4194304   83  Linux      # 这个是主分区
/dev/mapper/centos-root2         8390656    16779263     4194304    5  Extended   # 这个是扩展分区

5 创建逻辑分区

  • 扩展分区建好后,我们就可以在扩展分区建立逻辑分区了。
Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l                                # 选择逻辑分区
Adding logical partition 5
First sector (8392704-16779263, default 8392704):    # 默认开始位置
Using default value 8392704
Last sector, +sectors or +size{K,M,G} (8392704-16779263, default 16779263): +2G  # 大小2个G,不能超过扩展分区的大小
Partition 5 of type Linux and of size 2 GiB is set
  • 查看 p
Command (m for help): p

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x1c42ed69

                  Device Boot      Start         End      Blocks   Id  System
/dev/mapper/centos-root1            2048     8390655     4194304   83  Linux
/dev/mapper/centos-root2         8390656    16779263     4194304    5  Extended
/dev/mapper/centos-root5         8392704    12587007     2097152   83  Linux
  • 上面显示已经建好一个主分区,一个逻辑分区,但是这些现在还没有生效我们需要保存退出。
  • 输入w
Command (m for help): w #保存退出
 The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 Syncing disks.
  • 21
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逻辑峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值