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 :
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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 .
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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.
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# mkdir /sdb1
[[email protected] ~]# mkdir /sdb5
4.5 Mount partition
[[email protected] ~]# mount /dev/sdb1 /sdb1
[[email protected] ~]# mount /dev/sdb5 /sdb5
[[email protected] ~]# 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 .
[[email protected] ~]# echo "/dev/sdb1 /sdb1 xfs defaults 0 0 " >> /etc/fstab
[[email protected] ~]# 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
[[email protected] ~]# df -hT /sdb1
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb1 xfs 47M 2.7M 45M 6% /sdb1
[[email protected] ~]# df -hT /sdb5
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb5 ext4 190M 1.6M 175M 1% /sdb5