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

news/2024/5/18 22:35:36 标签: linux, 运维, 服务器, fdisk, 磁盘分区

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

fdisk_help_1">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)”。
在这里插入图片描述


http://www.niftyadmin.cn/n/342651.html

相关文章

如果一个用户查询不到自己的购买记录了,这个时候作为测试,应该怎么找问题

当一个用户查询不到自己的购买记录时&#xff0c;我们可以进行以下测试步骤来找问题&#xff1a; 了解具体情况&#xff1a;与用户确认购买记录的具体信息&#xff0c;例如购买时间、购买方式、付款方式等等&#xff0c;以便更好地分析问题。 检查系统日志&#xff1a;检查系统…

LeeCode-1.两数之和

题目链接:跳转链接 题目描述 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答案里不能重复…

chatgpt赋能Python-python3_取余

Python3 取余 在Python3中&#xff0c;取余运算是使用%符号进行实现的。它可以用于整数和浮点数&#xff0c;并返回一个余数。Python3是一种开放源码、高级编程语言。它是一种解释型语言&#xff0c;由于其简洁的语法和易于阅读的特性而受到广泛的欢迎。 取余基础 取余运算就…

性能测试的时机

最近接触了一个团队。在查看APM的时候发现团队交付的服务响应时间超长&#xff0c;随即去性能平台找性能测试执行留痕的过程中发现已经很久没有做性能测试了&#xff0c;就这个问题和团队开发负责人、团队测试负责人进行深度讨论&#xff0c;发现两人对性能测试的必要性提出了质…

「移动端自动化测试|环境配置」安装和配置adb环境

本文主要介绍配置移动端自动化测试需要使用到的adb环境 文章目录 adb 介绍下载压缩包加入环境变量验证 adb 介绍 adb 全称是 Android Debug Bridge&#xff0c;是一个在移动端自动化测试中&#xff0c;用来连接手机设备的命令行工具。无论使用Appium还是Airtest进行移动端测试…

跨平台应用开发进阶(六十五):小程序分包策略及实战讲解

文章目录 一、前言二、为什么要使用分包&#xff1f;三、分包大小查看四、如何使用分包&#xff1f;五、独立分包六、分包预下载七、项目实战八、拓展阅读 一、前言 微信小程序开发过程中&#xff0c;随着业务不断迭代&#xff0c;程序包的体积越来越大&#xff0c;使用分包加…

RTC实时时钟源码分析

1.先来看一下RTC的配置过程 2.RTC源码讲解 我们的工程中加入了 rtc.c 源文件和 rtc.h头文件&#xff0c;同时&#xff0c;引入了 stm32f10x_rtc.c 和 stm32f10x_bkp.c 库文件。 说明&#xff0c;首先是 RTC_Init&#xff0c;其代码如下&#xff1a; //实时时钟配置 //初始化…

玄子Share - mybatis-3.5.13 学习笔记

玄子Share - mybatis-3.5.13 学习笔记 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-edDRdx4N-1684580838688)(./assets/68747470733a2f2f6d7962617469732e6f72672f696d616765732f6d7962617469732d6c6f676f2e706e67.png)] mybatis 官网 官网 htt…