[Linux Five] Use fdisk command in Linux to realize disk partition

  Recently, due to the needs of work, the fastboot implementation in uboot has been initially studied. Studying fastboot inevitably requires knowledge about disk partitioning. Under Linux, you can use the fdisk command to realize disk partitioning. Ok, let's get down to business.

1. View help information (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. View disk partitions

  You can view the detailed partition of the disk through the command sudo fdisk -l, as shown below, there are currently two disks in my computer: sda and sdb (where sda ​​is a 240G solid-state hard disk, which is the system disk, and sdb is a 16G solid-state disk SD card).

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:~$

Taking sdb as an example, the detailed description is as follows:

  1. The disk capacity is 14.6GiB, a total of 15646851072 bytes, and the size of each "sector" is 512 bytes, a total of 30560256 sectors;
  2. Sector size (physical/logical): 512 bytes/512 bytes
  3. I/O size (min/typ): 512 bytes/512 bytes
  4. Disk label type: dos (the disk label type of sda is gpt)
  5. Disk identifier: 0x9b9208de
  6. The disk is divided into a "partition", the partition name is sdb1, the starting sector number of the partition is 2048, the ending sector number is 2099199, a total of 2097152 sectors (sector), the size is 1GiB, and the partition ID is 0x83 (linux native partition), the type is Linux

3. Disk Partition

Examples of requirements:

  1. Divide the sdb disk into 4 areas, of which 1, 2, and 3 are the main partitions, and 4 is the extended partition;
  2. The size of partition 1 is 512M bytes, the size of partition 2 is 2G bytes, the size of partition 3 is 4G bytes, and the size of partition 4 is the remaining disk space;
  3. Create a DOS-type partition table (MBR)

Step 1 : Execute the command sudo fdisk /dev/sdb, enter mto get help information, as follows:

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):

Step 2 : Enter pto view the current partition table of the disk

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

The current disk is divided into a "partition", the partition name is sdb1, the starting sector number of this partition is 2048, the ending sector number is 2099199, a total of 2097152 sectors (sectors), the size is 1GiB, and the partition ID is 0x83 (linux native partition), the type is Linux

Step 3 : Enter o, create a new GPT partition table

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.

Step 4 : Enter n, add a new partition (add our first partition with a size of 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

Step 5 : Input n, add 2, 3, 4 partitions in turn

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

Step 6 : Enter w, write the partition table to the disk, and the disk partition is completed.

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:~$  // 退出了磁盘的命令模式

Step 7 : Confirm again whether the "disk partition table" is actually written to the disk

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):

As above, it is consistent with our expectation, and the disk partition is successful.

4. Other commands

First execute the command:sudo fdisk /dev/sdb

4.1 Change partition type

Change the type of partition 1 from "Linux" to "W95 FAT32 (LBA)".
insert image description here

Guess you like

Origin blog.csdn.net/KXue0703/article/details/130689997