MENU

CentOS 7.6磁盘分区并开机自动挂载

January 16, 2019 • Read: 3097 • 笔记,网站

CentOS 7.6磁盘分区并开机自动挂载


修改日期:2019年01月16日
版本:v1.0
作者:whl


[toc]


一、 磁盘分区

1、 查看现有分区

fdisk  -l
[root@whl ~]# fdisk  -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/nvme0n1: 256.1 GB, 256060514304 bytes, 500118192 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: gpt
Disk identifier: C94BAA39-F935-457A-97E9-EF9FD01B004B


#         Start          End    Size  Type            Name
 1         2048       411647    200M  EFI System      EFI System Partition
 2       411648      2508799      1G  Microsoft basic 
 3      2508800    212223999    100G  Microsoft basic 
 4    212224000    254167039     20G  Linux swap      
 5    254167040    463882239    100G  Microsoft basic 
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: gpt
Disk identifier: 6B01A4CC-0F11-41F0-801D-7D3C59693C81

2、进行磁盘分区

需分区的盘2T机械硬盘(/dev/sda)

# 对/dev/sda分区
fdisk /dev/sda

输出信息:

[root@whl ~]# fdisk /dev/sda 
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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
   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
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): 

输入p列出所有分区

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: gpt
Disk identifier: 6B01A4CC-0F11-41F0-801D-7D3C59693C81


#         Start          End    Size  Type            Name

Command (m for help): 

输入n创建新分区

# 输入分区序号,直接回车使用默认分配即可。
Command (m for help): n
Partition number (1-128, default 1): 

# 输入起始扇区,直接回车使用默认分配即可。
Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-3907029134, default 2048): 

# 输入终止扇区,可输入扇区数或实际容量。
Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-3907029134, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-3907029134, default 3907029134): +1000G
Created partition 1


Command (m for help): 

输入p查看所有分区

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: gpt
Disk identifier: 6B01A4CC-0F11-41F0-801D-7D3C59693C81


#         Start          End    Size  Type            Name
 1         2048   2097154047   1000G  Linux filesyste 

Command (m for help): 

以相同方式常见第二个分区

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: gpt
Disk identifier: 6B01A4CC-0F11-41F0-801D-7D3C59693C81


#         Start          End    Size  Type            Name
 1         2048   2097154047   1000G  Linux filesyste 
 2   2097154048   3907029134    863G  Linux filesyste 

Command (m for help): 

输入w保存

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: gpt
Disk identifier: 6B01A4CC-0F11-41F0-801D-7D3C59693C81


#         Start          End    Size  Type            Name
 1         2048   2097154047   1000G  Linux filesyste 
 2   2097154048   3907029134    863G  Linux filesyste 

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@whl ~]# 

使磁盘分区生效

partprobe  /dev/sda

3、格式化新建分区

列出所有分区

fdisk -l
[root@whl ~]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/nvme0n1: 256.1 GB, 256060514304 bytes, 500118192 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: gpt
Disk identifier: C94BAA39-F935-457A-97E9-EF9FD01B004B


#         Start          End    Size  Type            Name
 1         2048       411647    200M  EFI System      EFI System Partition
 2       411648      2508799      1G  Microsoft basic 
 3      2508800    212223999    100G  Microsoft basic 
 4    212224000    254167039     20G  Linux swap      
 5    254167040    463882239    100G  Microsoft basic 
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/sda: 2000.4 GB, 2000398934016 bytes, 3907029168 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
Disk label type: gpt
Disk identifier: 6B01A4CC-0F11-41F0-801D-7D3C59693C81


#         Start          End    Size  Type            Name
 1         2048   2097154047   1000G  Linux filesyste 
 2   2097154048   3907029134    863G  Linux filesyste 
[root@whl ~]# 

格式化磁盘分区

# 格式化为xfs
mkfs.xfs /dev/sda1
mkfs.xfs /dev/sda2
[root@whl ~]# mkfs.xfs /dev/sda1 
meta-data=/dev/sda1              isize=512    agcount=4, agsize=65536000 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=128000, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@whl ~]# 
[root@whl ~]# mkfs.xfs /dev/sda2
meta-data=/dev/sda2              isize=512    agcount=4, agsize=56558597 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=226234385, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=110466, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@whl ~]# 

二、 挂载分区

1、手动挂载

# 创建挂载目录
mkdir /data1
mkdir /data2

# 挂载分区至目录
mount  /dev/sda1  /data1
mount  /dev/sda2  /data2

2、开机自动挂载

编辑/etc/fstab文件

vi /etc/fstab

新增两行命令

# 新增挂载命令
/dev/sda1  /data1  xfs  defaults  0  0
/dev/sda2  /data2  xfs  defaults  0  0

保存退出即可。

#
# /etc/fstab
# Created by anaconda on Mon Jan 14 20:10:02 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=50fe6c07-6936-4ff5-bec2-674c7ce195b4 /                       xfs     defaults        0 0
UUID=5a696cf0-2cc0-41c9-a8bd-f5bd2b74b684 /boot                   xfs     defaults        0 0
UUID=C163-2C2E          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
UUID=e36026ba-0711-447d-885d-b737cdd2b3a4 /home                   xfs     defaults        0 0
UUID=d5bc3a97-ff95-463c-8eae-026a35fcbfb1 swap                    swap    defaults        0 0
/dev/sda1  /data1  xfs  defaults  0  0
/dev/sda2  /data2  xfs  defaults  0  0
Tags: None