【Linux 之五】 Linux中使用fdisk命令实现磁盘分区

这篇具有很好参考价值的文章主要介绍了【Linux 之五】 Linux中使用fdisk命令实现磁盘分区。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  最近由于工作的需要,初步研究了uboot中的fastboot实现方式。研究fastboot不可避免的需要了解磁盘分区的相关知识点,在linux下可以使用fdisk命令实现磁盘的分区。好了,下面步入正题。

1. 查看帮助信息(fdisk --help)

linux@linux-System-Product-Name:~$ fdisk --help

Usage:
 fdisk [options] <disk>      change partition table
 fdisk [options] -l [<disk>] list partition table(s)

Display or manipulate a disk partition table.

Options:
 -b, --sector-size <size>      physical and logical sector size
 -B, --protect-boot            don't erase bootbits when creating a new label
 -c, --compatibility[=<mode>]  mode is 'dos' or 'nondos' (default)
 -L, --color[=<when>]          colorize output (auto, always or never)
                                 colors are enabled by default
 -l, --list                    display partitions and exit
 -o, --output <list>           output columns
 -t, --type <type>             recognize specified partition table type only
......

2. 查看磁盘分区

  通过命令 sudo fdisk -l可以查看磁盘的详细分区情况,如下所示,我电脑目前存在两个磁盘:分别为sda和sdb(其中sda即为240G的固态硬盘,为系统盘,而sdb则为一个容量为16G的SD卡)。

linux@linux-System-Product-Name:~$ sudo fdisk -l
[sudo] password for linux:
Disk /dev/sda: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: DE19ADF1-DDE6-4899-BC1B-C6B608AD5C31

Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 488396799 487346176 232.4G Linux filesystem

Disk /dev/sdb: 14.6 GiB, 15646851072 bytes, 30560256 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
Disklabel type: dos
Disk identifier: 0x9b9208de

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 2099199 2097152   1G 83 Linux
linux@linux-System-Product-Name:~$

以sdb为例进行详细的说明如下:

  1. 磁盘容量为14.6GiB, 合计共15646851072 字节,每个“扇区”的大小为512字节,总共30560256个扇区;
  2. 扇区的大小(物理 / 逻辑):512字节 / 512字节
  3. I / O大小(最小值 / 典型值):512字节 / 512字节
  4. 磁盘标签类型:dos (sda的磁盘标签类型为gpt)
  5. 磁盘标识符:0x9b9208de
  6. 该磁盘共分了一个“分区”,分区名称为sdb1, 该分区的起始扇区号为2048, 结束扇区号为2099199,共2097152个扇区(sector),大小为1GiB, 分区ID为0x83(linux native分区),类型为Linux

3. 磁盘分区

需求举例:

  1. 将sdb磁盘分为4个区,其中1,2,3分区为主分区,4分区为扩展分区;
  2. 分区1的大小为512M字节, 分区2的大小为2G字节,分区3的大小为4G字节,分区4的大小为剩余的磁盘空间;
  3. 创建DOS类型的分区表(MBR)

步骤1:执行命令 sudo fdisk /dev/sdb,输入 m 获取帮助信息,如下:

linux@linux-System-Product-Name:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help):

步骤2:输入 p,查看磁盘当前的分区表

Command (m for help): p
Disk /dev/sdb: 14.6 GiB, 15646851072 bytes, 30560256 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
Disklabel type: dos
Disk identifier: 0x9b9208de

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 2099199 2097152   1G 83 Linux

当前磁盘共分了一个“分区”,分区名称为sdb1, 该分区的起始扇区号为2048, 结束扇区号为2099199,共2097152个扇区(sector),大小为1GiB, 分区ID为0x83(linux native分区),类型为Linux

步骤3:输入 o,创建一个新的GPT分区表

Command (m for help): o

Created a new DOS disklabel with disk identifier 0x6ade9f76.
The old dos signature will be removed by a write command.

步骤4:输入 n,添加一个新的分区(添加我们的第1个分区,大小为512M)

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p   // 主分区
Partition number (1-4, default 1):	// 分区号为1,直接按“回车”即可
First sector (2048-30560255, default 2048): // 直接按“回车”即可
Last sector, +sectors or +size{K,M,G,T,P} (2048-30560255, default 30560255): +512M // 分配的空间大小为512M

Created a new partition 1 of type 'Linux' and of size 512 MiB.

Command (m for help): p // 查询分区表
Disk /dev/sdb: 14.6 GiB, 15646851072 bytes, 30560256 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
Disklabel type: dos
Disk identifier: 0x6ade9f76

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1        2048 1050623 1048576  512M 83 Linux

步骤5:输入 n,依次添加2,3,4分区

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):	// 创建第2个分区
First sector (1050624-30560255, default 1050624):
Last sector, +sectors or +size{K,M,G,T,P} (1050624-30560255, default 30560255): +2G // 分区大小为2G

Created a new partition 2 of type 'Linux' and of size 2 GiB.

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): // 创建第3个分区
First sector (5244928-30560255, default 5244928):
Last sector, +sectors or +size{K,M,G,T,P} (5244928-30560255, default 30560255): +4G // 分区大小为4G

Created a new partition 3 of type 'Linux' and of size 4 GiB.

Command (m for help): n
Partition type
   p   primary (3 primary, 0 extended, 1 free)
   e   extended (container for logical partitions)
Select (default e): e // 创建扩展分区

Selected partition 4	// 创建第3个分区,MBR最多只能创建4个分区
First sector (13633536-30560255, default 13633536):
Last sector, +sectors or +size{K,M,G,T,P} (13633536-30560255, default 30560255):

Created a new partition 4 of type 'Extended' and of size 8.1 GiB. // 分区大小为“磁盘剩余空间”

Command (m for help): p
Disk /dev/sdb: 14.6 GiB, 15646851072 bytes, 30560256 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
Disklabel type: dos
Disk identifier: 0x6ade9f76

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1           2048  1050623  1048576  512M 83 Linux
/dev/sdb2        1050624  5244927  4194304    2G 83 Linux
/dev/sdb3        5244928 13633535  8388608    4G 83 Linux
/dev/sdb4       13633536 30560255 16926720  8.1G  5 Extended

步骤6:输入 w,将分区表写入磁盘,至此磁盘分区完成。

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
linux@linux-System-Product-Name:~$  // 退出了磁盘的命令模式

步骤7:再次确认“磁盘分区表”是否真正的写入磁盘

linux@linux-System-Product-Name:~$ sudo fdisk /dev/sdb
[sudo] password for linux:

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 14.6 GiB, 15646851072 bytes, 30560256 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
Disklabel type: dos
Disk identifier: 0x6ade9f76

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1           2048  1050623  1048576  512M 83 Linux
/dev/sdb2        1050624  5244927  4194304    2G 83 Linux
/dev/sdb3        5244928 13633535  8388608    4G 83 Linux
/dev/sdb4       13633536 30560255 16926720  8.1G  5 Extended

Command (m for help):

如上,与我们预期的是一致的,磁盘分区成功。

4. 其它命令

首先执行命令:sudo fdisk /dev/sdb

4.1 更改分区类型

将分区1的类型由“Linux”改为“W95 FAT32 (LBA)”。
linuxfdisk分区步骤,Linux,linux,运维,服务器,fdisk,磁盘分区文章来源地址https://www.toymoban.com/news/detail-753146.html

到了这里,关于【Linux 之五】 Linux中使用fdisk命令实现磁盘分区的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【RedHat9.0】磁盘fdisk命令分区的增、删、改、查

    使用fdisk进入交互模式,通过输入fdisk程序所提供的子命令可以对磁盘的分区进行有效管理。 根分区、swap分区、引导分区的详细教程在这个博客的基础上我们详细介绍fdisk命令。 fdisk交互命令 命令 说明 增 n、o、g、G、s n 创建一个新的MBR分区 o 创建一个空的DOS分区表 g 创建一

    2024年04月11日
    浏览(30)
  • fdisk 命令 创建分区 实现扩容

    Linux fdisk 是一个创建和维护分区表的程序,它兼容 DOS 类型的分区表、BSD 或者 SUN 类型的磁盘列表。 在VMware vsphere的虚拟化平台下,为了快速部署虚拟服务器,我们常常使用模板部署虚拟机。但真实业务的资源需求常常与模板不同,这时便需要自定义硬件资源。在定义硬盘的

    2024年02月15日
    浏览(31)
  • 【玩转Linux操作】详细讲解 Linux分区&&磁盘 操作以及相关的命令

    🎊专栏【玩转Linux操作】 🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。 🎆音乐分享【Counting Stars 】 欢迎并且感谢大家指出小吉的问题🥰 在Linux中,分区是将硬盘或其他存储设备划分为逻辑部分的过程。每个分区都被视为一个独立的存储空间,可以用于存储文件系统、

    2024年02月16日
    浏览(39)
  • VMware中Ubuntu拓展磁盘容量的两种方式 &图形化方式&命令行磁盘分区方式(亲测有效&简单且详细)&linux磁盘分区

    在用VMware虚拟机的情况下,一开始分配的容量在使用过程中,出现不够的情况。针对这种情况,如何拓展磁盘容量? 下面整理归纳了两种方式进行拓展。一种是Ubuntu自带的磁盘工具,另一种是命令行方式的磁盘分区方式。同时满足不同扩容需求,这里也进行了扩容方式的拓展

    2024年02月14日
    浏览(31)
  • 【Linux】磁盘分区与永久挂载,实操模拟损坏文件系统,只要一个命令即可修复!!!

    目录 一、磁盘基础内容 1、了解扇区的大小,以及与块的关系 2、磁盘的第一个扇区的内容 二、在Linux中使用硬盘的三大步骤 1、分区 ----分区的作用:提高硬盘的性能 ----分区的命令 ----分区的分类: ----实操分区实验 2、创建文件系统 ----文件系统类型: ----创建文件系统的命

    2024年02月04日
    浏览(39)
  • Linux中使用磁盘和磁盘的分区试验

    柱面 扇区:是硬盘最小存储单位,为512字节,8个扇区组成一块为4k 磁道 /dev/sda   第一块硬盘 s:代表硬盘接口类型,接口类型决定了数据传输的速率 d:disk(磁盘) a:第一块盘 b:第二块盘 /dev/vd----虚拟的磁盘 分区:为了提高性能 作用: 1.优化I/O性能------读写性能 2.隔离系统

    2024年02月04日
    浏览(31)
  • Linux磁盘查看,使用(分区、格式化、挂载)

    目录 0、观察磁盘分区状态:lsblk、blkid、parted 0.1  lsblk列出系统上的所有磁盘列表 0.2  blkid列出设备的UUID等参数 0.3  parted列出磁盘的分区表类型与分区信息 1、磁盘分区:gdisk、fdisk 1.1  fdisk 2、磁盘格式化(创建文件系统):mkfs 3、文件系统挂载 3.1.1  手动挂载:mount 3.1.2

    2023年04月24日
    浏览(40)
  • Linux 磁盘管理及磁盘分区(主分区、扩展分区、逻辑分区)

    目录 1 MBR与磁盘分区 1.1 分区的意义  1.2 MBR分区 1.2 磁盘分区的表示 1.3 磁盘分区结构 2 文件系统 2.1 XFS  2.2 swap 2.3 FAT16、FAT32 2.4 EXT4 2.5 JFS 3 磁盘分区工具 3.1 fdisk  3.2 添加磁盘  3.3 磁盘分区 3.4 blkid ​编辑 3.5 mkfs 4 挂载及永久挂载 4.1 mount 4.2 临时挂载  4.3 永久挂载 wind

    2024年02月08日
    浏览(38)
  • 【Linux】fdisk命令参数详解(图文超详细,内容来自官方文档)

    目录 0.环境 1.背景 2.内容--官方文档对fdisk的介绍 1)名称 2)说明  3)具体参数/选项 4)举个栗子,我要查fdisk的版本 windows + linux虚拟机 之前发表了一篇文章Linux 用fdisk进行磁盘分区(图文过程),里面用到了fdisk命令来给磁盘分区,用到了很多fdisk的内置命令。 今天(2023

    2024年02月11日
    浏览(35)
  • Linux存储管理磁盘分区逻辑分区

    目录 前言 一、逻辑分区 1、简介 2、演示创建四个主分区后的效果 1)创建四个分区(具体步骤见上一篇文章) 2)尝试创建第五个分区 3)删除第四个主分区,将其分为扩展分区  4)创建逻辑分区 3、刷新分区表、格式化、进行挂载 4、卸载(取消分区挂载) 总结 前言 一、

    2023年04月26日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包