- A+
一.划分分区并挂载磁盘
使用fdisk分区工具将该数据盘设为主分区,分区形式默认设置为MBR,文件系统设为ext4格式,挂载在“/mydata”下,并设置开机启动自动挂载。
1.查看新增数据盘
- [root@ecs-79ae-0002 ~]# fdisk -l
- Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000a952a
- Device Boot Start End Blocks Id System
- /dev/vda1 * 2048 104857566 52427759+ 83 Linux
- Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 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
- [root@ecs-79ae-0002 ~]#
上面信息表示当前的云服务器有两块磁盘,“/dev/vda”是系统盘,“/dev/vdb”是新增数据盘。
2.对新增数据盘执行分区操作
- [root@ecs-79ae-0002 ~]# fdisk /dev/vdb
- 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.
- Device does not contain a recognized partition table
- Building a new DOS disklabel with disk identifier 0x2f603b5b.
- Command (m for help):
3.开始新建分区
- Command (m for help): n
- Partition type:
- p primary (0 primary, 0 extended, 4 free)
- e extended
- Select (default p):
表示磁盘有两种分区类型:
- “p”表示主分区。
- “e”表示扩展分区。
4.开始创建一个主分区
- Partition type:
- p primary (0 primary, 0 extended, 4 free)
- e extended
- Select (default p): p
- Partition number (1-4, default 1):
“Partition number”表示主分区编号,可以选择1-4。
5.输入主分区编号
以分区编号选择“1”为例,输入主分区编号“1”,按“Enter”。
- Partition number (1-4, default 1): 1
- First sector (2048-1048575999, default 2048):
“First sector”表示起始磁柱值,可以选择2048-1048575999,默认为2048。
6.选择起始磁柱值
以选择默认起始磁柱值2048为例,按“Enter”。
系统会自动提示分区可用空间的起始磁柱值和截止磁柱值,可以在该区间内自定义,或者使用默认值。起始磁柱值必须小于分区的截止磁柱值。
- First sector (2048-1048575999, default 2048):
- Using default value 2048
- Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999):
“Last sector”表示截止磁柱值,可以选择2048-1048575999,默认为1048575999。
7.选择截止磁柱值
以选择默认截止磁柱值1048575999为例,按“Enter”。
系统会自动提示分区可用空间的起始磁柱值和截止磁柱值,可以在该区间内自定义,或者使用默认值。起始磁柱值必须小于分区的截止磁柱值。
- Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999):
- Using default value 1048575999
- Partition 1 of type Linux and of size 500 GiB is set
- Command (m for help):
表示分区完成,即为数据盘新建了1个分区。
8.查看新建分区的详细信息
输入“p”,按“Enter”,查看新建分区的详细信息。
- Command (m for help): p
- Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 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: 0x2f603b5b
- Device Boot Start End Blocks Id System
- /dev/vdb1 2048 1048575999 524286976 83 Linux
- Command (m for help):
表示新建分区“/dev/vdb1”的详细信息。
9.将分区结果写入分区表中
- Command (m for help): w
- The partition table has been altered!
- Calling ioctl() to re-read partition table.
- Syncing disks.
- [root@ecs-79ae-0002 ~]#
表示分区创建完成。
10.将新的分区表变更同步至操作系统
执行以下命令,将新的分区表变更同步至操作系统。
- [root@ecs-79ae-0002 ~]# partprobe
11.将新建分区文件系统设为系统所需格式
执行以下命令,将新建分区文件系统设为系统所需格式。
- [root@ecs-79ae-0002 ~]# mkfs -t ext4 /dev/vdb1
- mke2fs 1.42.9 (28-Dec-2013)
- Filesystem label=
- OS type: Linux
- Block size=4096 (log=2)
- Fragment size=4096 (log=2)
- Stride=0 blocks, Stripe width=0 blocks
- 32768000 inodes, 131071744 blocks
- 6553587 blocks (5.00%) reserved for the super user
- First data block=0
- Maximum filesystem blocks=2279604224
- 4000 block groups
- 32768 blocks per group, 32768 fragments per group
- 8192 inodes per group
- Superblock backups stored on blocks:
- 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
- 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
- 102400000
- Allocating group tables: done
- Writing inode tables: done
- Creating journal (32768 blocks): done
- Writing superblocks and filesystem accounting information: done
12.新建挂载目录
- [root@ecs-79ae-0002 ~]# mkdir /mydata
13.将新建分区挂载到创建的目录下
- [root@ecs-79ae-0002 ~]# mount /dev/vdb1 /mydata
14.查看挂载结果
- [root@ecs-79ae-0002 ~]# df -TH
- Filesystem Type Size Used Avail Use% Mounted on
- devtmpfs devtmpfs 34G 0 34G 0% /dev
- tmpfs tmpfs 34G 0 34G 0% /dev/shm
- tmpfs tmpfs 34G 9.0M 34G 1% /run
- tmpfs tmpfs 34G 0 34G 0% /sys/fs/cgroup
- /dev/vda1 ext4 53G 2.4G 48G 5% /
- tmpfs tmpfs 6.8G 0 6.8G 0% /run/user/0
- /dev/vdb1 ext4 529G 76M 502G 1% /mydata
- [root@ecs-79ae-0002 ~]#
表示新建分区“/dev/vdb1”已挂载至“/mydata”。
但是上面的手动挂载当云服务器重启后失效,又需要挂载,下面设置开机自动挂载。
二.设置开机自动挂载磁盘分区
下面介绍如何在fstab
文件中使用UUID
来设置自动挂载磁盘分区。
注:UUID(universally unique identifier)
是Linux系统为磁盘分区提供的唯一的标识字符串。
1.查询磁盘分区的UUID
- [root@ecs-79ae-0002 ~]# blkid /dev/vdb1
- /dev/vdb1: UUID="ee09a54c-4132-4578-8040-7bc86768a057" TYPE="ext4"
2.编辑器fstab
文件
- [root@ecs-79ae-0002 ~]# vim /etc/fstab
3.编辑的内容
- # /etc/fstab
- # Created by anaconda on Thu Feb 10 07:01:09 2022
- #
- # 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=6c533615-cacd-47a7-844e-10013da6d35b / ext4 defaults 1 1
- UUID=ee09a54c-4132-4578-8040-7bc86768a057 /mydata ext4 defaults 0 2
- [root@ecs-79ae-0002 ~]#
关于fstab相关参数的解释如下:
- 第一列为UUID,此处填写1中查询到的磁盘分区的UUID。
- 第二列为磁盘分区的挂载目录,可以通过df -TH命令查询。
- 第三列为磁盘分区的文件系统格式, 可以通过df -TH命令查询。
- 第四列为磁盘分区的挂载选项,此处通常设置为defaults即可。
- 第五列为Linux dump备份选项。
- 0表示不使用Linux dump备份。现在通常不使用dump备份,此处设置为0即可。
- 1表示使用Linux dump备份。
- 第六列为fsck选项,即开机时是否使用fsck检查磁盘。
- 0表示不检验。
- 挂载点为(/)根目录的分区,此处必须填写1。
- 根分区设置为1,其他分区只能从2开始,系统会按照数字从小到大依次检查下去。
4.重新加载“/etc/fstab”文件并查看挂载
- [root@ecs-79ae-0002 ~]# mount -a
- [root@ecs-79ae-0002 ~]# df -TH
- Filesystem Type Size Used Avail Use% Mounted on
- devtmpfs devtmpfs 34G 0 34G 0% /dev
- tmpfs tmpfs 34G 0 34G 0% /dev/shm
- tmpfs tmpfs 34G 9.1M 34G 1% /run
- tmpfs tmpfs 34G 0 34G 0% /sys/fs/cgroup
- /dev/vda1 ext4 53G 2.4G 48G 5% /
- /dev/vdb1 ext4 529G 76M 502G 1% /mydata
- tmpfs tmpfs 6.8G 0 6.8G 0% /run/user/0
- [root@ecs-79ae-0002 ~]#