Using fdisk to partition disk in Linux

Philosophy of life 2020-11-07 20:19:28
using fdisk partition disk linux


One . Preface

​ When the server inserts a hard disk , If we want to use the hard disk , You need to use the disk partition management tool first , Then format the partition , Mount the partition to the directory

On , Before you can officially use the hard disk to store files . There are many disk partition management tools , This paper mainly introduces fdisk,gdisk,parted, And compare .

Two . About the structure of the partition

1. There are two kinds of partition structure of disk , One is MBR Partition , One is GPT Partition , The characteristics of the two structures are described one by one .

2.MBR(Master Boot Record)

  • ​ It's the traditional zoning mechanism , Apply to most uses BIOS Guided PC equipment ( Apple uses EFI The way ), quite a lot Server The server supports BIOS Also support EFI How to guide ;
  • ​ MBR Support 32bit and 64bit System ;
  • ​ MBR The division is divided into the main section ( A hard disk can only be created at most 4 Primary partitions )、 Extended partitions ( An extended partition will occupy the location of a primary partition )、 A logical partition ( Logical partitions are created based on extended partitions , First there's an extended partition , Then create logical partition on the basis of extended partition ; In other words, we're going to use logical partitions , You must first create an extended partition , The space of the extended partition cannot be used directly , We have to build logical partitions on top of extended partitions , To be used ). stay Linux Using extended partitions and logical partitions on, you can create at most 15 Zones ;
  • ​ MBR Only support no more than 2TB The hard disk of .

3.GPT(GUID Partition Table)

  • ​ It's a newer partitioning mechanism , It's solved MBR Many shortcomings of ;
  • ​ Support over 2TB Of disks ;
  • ​ Backward compatibility MBR;
  • ​ Use GPT The premise of partitioning is that the underlying hardware must support UEFI(Intel The proposed replacement BIOS A new generation of boot system for ) Can be used , That is, the underlying hardware must use UEFI;
  • ​ GPT Partitions only support 64 Bit operating system ;
  • ​ GPT Partition list supports maximum 128PB(1PB=1024TB), Can define 128 Zones , There is no primary partition , Extend the concept of partition and logical partition , All partitions can be formatted .

4. Judge the partition is GPT still MBR The method is as follows :

[root@node5 ~]# fdisk -l
Disk /dev/sda: 37.6 GB, 37580963840 bytes, 73400320 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: 0x000b606a
#Disk label type: dos This line of information indicates that the partition is MBR Partition
# If it shows Disk label type: gpt Indicates that the partition is GPT Partition
# The following commands can also be viewed
[root@node5 ~]# gdisk -l /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
# You can also use the following methods
[root@node5 ~]# parted -l
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 37.6GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary xfs boot
2 1075MB 21.5GB 20.4GB primary lvm
# Show Partition Table: msdos, It is MBR Partition
# Show Partition Table: gpt, It is GPT Partition

3、 ... and .fdisk Detailed command

Get into fdisk After the command window , Input m, You can see all of fdisk function .

[root@node5 ~]# fdisk /dev/sdb
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 action
a toggle a bootable flag # Switch to start flag
b edit bsd disklabel # Edit disk tags
c toggle the dos compatibility flag # Switch dos Compatible logo
d delete a partition # Delete a partition
g create a new empty GPT partition table # Create an empty GPT Partition table
G create an IRIX (SGI) partition table # Create a IRIX Partition table
l list known partition types # List known partition types
m print this menu # Print menu
n add a new partition # Add a partition
o create a new empty DOS partition table # Create an empty DOS Partition table
p print the partition table # Print partition table
q quit without saving changes # Exit without saving
s create a new empty Sun disklabel # Create an empty sun Disk label
t change a partition's system id # Change the type of a partition
u change display/entry units # Change the displayed units
v verify the partition table # Verify partition table
w write table to disk and exit # Write partition table and exit
x extra functionality (experts only) # Advanced features

Four . Use fdisk Partition the disk

4.1 Partition planning

The partition plan is as follows : Create a primary partition , An extended partition , The extended partition contains two logical partitions .

4.2 fdisk Partition the disk

Use fdisk Yes /dev/sdb Partition the hard disk .

# Use lsblk Check to see ,sdb The size of this disk is 1G
[root@node5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 35G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
└─sdb1 8:17 0 200M 0 part
# Yes /dev/sdb This disk is partitioned
[root@node5 ~]# fdisk /dev/sdb
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 # view help
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)
Command (m for help): p # Print partition table
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x56e11754
Device Boot Start End Blocks Id System
/dev/sdb1 2048 411647 204800 83 Linux
# See that a partition already exists /dev/sdb1, Let's put the /dev/sdb1 Delete for
Command (m for help): d # Delete partition
Selected partition 1
Partition 1 is deleted
Command (m for help): p # Print partition table , Check the partition table again , Find out /dev/sdb1 It has been deleted
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x56e11754
Device Boot Start End Blocks Id System
Command (m for help): n # New partition
Partition type:
p primary (0 primary, 0 extended, 4 free) # Primary partition
e extended # Extended partitions
Select (default p): p # Select new primary partition
Partition number (1-4, default 1): # Main area code , Will generate /dev/sdb1
First sector (2048-2097151, default 2048): # Starting Sector , Enter by default 2048 Start
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +50M # Assign primary partition size , Here is 50M
Partition 1 of type Linux and of size 50 MiB is set
Command (m for help): p # Print partition table , You can see that the primary partition has been created /dev/sdb1
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x56e11754
Device Boot Start End Blocks Id System
/dev/sdb1 2048 104447 51200 83 Linux
Command (m for help): n # New partition
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e # Select Create extended partition
Partition number (2-4, default 2): # Extended partition number , Here we go straight back , The default is /dev/sdb2
First sector (104448-2097151, default 104448): # Default carriage return , Start with the current sector
Using default value 104448
Last sector, +sectors or +size{K,M,G} (104448-2097151, default 2097151): +500M # Allocate extended partition size , Here is 500M
Partition 2 of type Extended and of size 500 MiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l # New logical partition
Adding logical partition 5 # The default logical partition number is 5
First sector (106496-1128447, default 106496): # Logical partition starting position
Using default value 106496
Last sector, +sectors or +size{K,M,G} (106496-1128447, default 1128447): +200M # Allocate logical partition size , Here is 200M
Partition 5 of type Linux and of size 200 MiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l # Create a second logical partition
Adding logical partition 6
First sector (518144-1128447, default 518144):
Using default value 518144
Last sector, +sectors or +size{K,M,G} (518144-1128447, default 1128447): # Directly enter , Default allocation of remaining space
Using default value 1128447
Partition 6 of type Linux and of size 298 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x56e11754
Device Boot Start End Blocks Id System
/dev/sdb1 2048 104447 51200 83 Linux
/dev/sdb2 104448 1128447 512000 5 Extended
/dev/sdb5 106496 516095 204800 83 Linux
/dev/sdb6 518144 1128447 305152 83 Linux
# From the above output we can see that ,/dev/sdb1 Main area ,/dev/sdb2 Partition for expansion , The extended partition contains two logical partitions /dev/sdb5 and /dev/sdb6
Command (m for help): w # Save partition information and exit
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@node5 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 35G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─centos-root 253:0 0 17G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
├─sdb1 8:17 0 50M 0 part
├─sdb2 8:18 0 1K 0 part
├─sdb5 8:21 0 200M 0 part
└─sdb6 8:22 0 298M 0 part
sr0 11:0 1 1024M 0 rom
[root@node5 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5 /dev/sdb6

4.3 format partition

Format the partition into the file system you want .

# You can format partitions into various file systems
[root@node5 ~]# mkfs
mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.fat mkfs.minix mkfs.msdos mkfs.vfat mkfs.xfs
# hold /dev/sdb1 Format as xfs file system
[root@node5 ~]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@node5 ~]# mkfs.xfs -f /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=2, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=12800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# hold /dev/sdb5 Format as ext4 file system
[root@node5 ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

4.4 Create mount point / Mount Directory

[root@node5 ~]# mkdir /sdb1
[root@node5 ~]# mkdir /sdb5

4.5 Mount partition

[root@node5 ~]# mount /dev/sdb1 /sdb1
[root@node5 ~]# mount /dev/sdb5 /sdb5
[root@node5 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 11G 6.1G 65% /
devtmpfs devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs tmpfs 3.9G 8.0K 3.9G 1% /dev/shm
tmpfs tmpfs 3.9G 8.8M 3.9G 1% /run
tmpfs tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 125M 890M 13% /boot
tmpfs tmpfs 785M 0 785M 0% /run/user/0
/dev/sdb1 xfs 47M 2.7M 45M 6% /sdb1
/dev/sdb5 ext4 190M 1.6M 175M 1% /sdb5

4.6 Set auto mount partition when power on

Partition use mount After manual mount , You also need to write mount information to /etc/fstab In file , Otherwise, after the restart , Need to mount again . When the system is turned on, it will read

take /etc/fstab What's in this file , Mount the disk according to the configuration in the file . In this way, we only need to write the disk mount information to this file, and we don't need to open every time

After the machine started, it was mounted manually .

[root@node5 ~]# echo "/dev/sdb1 /sdb1 xfs defaults 0 0 " >> /etc/fstab
[root@node5 ~]# echo "/dev/sdb5 /sdb5 ext4 defaults 0 0 " >> /etc/fstab
# notes ,/etc/fstab File,
# An example is : /dev/sda3 /home ext4 defaults,noatime 0 2
/etc/fstab The documents mainly include 6 paragraph , In turn, is :
<file system> <dir> <type> <options> <dump> <pass>
<file system> Partition or storage device to mount
<dir> The location of the mounted Directory
<type> The file system type of the mount partition , such as :ext3、ext4、xfs、swap
<options> What parameters are used for mounting . Examples are as follows :
auto - At startup or when you type mount -a Automatically mount on command .
noauto - Only mounted at your command .
exec - Binaries that allow execution of this partition .
noexec - Binaries on this file system... Are not allowed to execute .
ro - Mount the file system in read-only mode .
rw - Mount the file system in read-write mode .
user - Allow any user to mount this file system , If there is no display definition , Implicitly enable noexec, nosuid, nodev Parameters .
users - Allow all users Users in the group mount file systems .
nouser - Can only be root mount .
owner - Allow device owners to mount .
sync - I/O A simultaneous .
async - I/O asynchronous .
dev - Parsing block special devices on file systems .
nodev - Do not parse block special devices on the file system .
suid - allow suid Operation and setting sgid position . This parameter is usually used for some special tasks , Enable general users to temporarily upgrade permissions when running programs .
nosuid - prohibit suid Operation and setting sgid position .
noatime - Do not update file system inode Access records , Can improve performance .
nodiratime - Do not update directories on the file system inode Access records , Can improve performance ( See atime Parameters ).
relatime - Real time updates inode access Record . Only when the access time in the record is earlier than the current access will it be updated .( And noatime be similar , But will not interrupt such as mutt Or other programs to detect whether the file has been modified since the last access .), Can improve performance .
flush - vfat The option to , Refresh data more frequently , The copy dialog or progress bar does not disappear until all data has been written .
defaults - Use the file system's default mount parameters , for example ext4 The default parameter for is :rw, suid, dev, exec, auto, nouser, async.
<dump> dump Tools use it to decide when to back up . dump Will check its contents , And use numbers to decide whether to back up the file system . The allowed number is 0 and 1 .0 Said to ignore , 1 Back up . Most of the users are not installed dump Of , For them <dump> It should be set to 0.
<pass> fsck Read <pass> To determine the order in which file systems need to be checked . The allowed number is 0, 1, and 2. The root directory should have the highest priority 1, All other devices to be checked are set to 2. 0 Indicates that the device will not be fsck Checked .

4.7 Check the partition effect

[root@node5 ~]# df -hT /sdb1
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 xfs 47M 2.7M 45M 6% /sdb1
[root@node5 ~]# df -hT /sdb5
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb5 ext4 190M 1.6M 175M 1% /sdb5
版权声明
本文为[Philosophy of life]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云