云服务器ECS挂载数据盘

  • A+

一.划分分区并挂载磁盘

使用fdisk分区工具将该数据盘设为主分区,分区形式默认设置为MBR,文件系统设为ext4格式,挂载在“/mydata”下,并设置开机启动自动挂载。

1.查看新增数据盘

  1. [root@ecs-79ae-0002 ~]# fdisk -l  
  2.   
  3. Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors  
  4. Units = sectors of 1 * 512 = 512 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  7. Disk label type: dos  
  8. Disk identifier: 0x000a952a  
  9.   
  10.    Device Boot      Start         End      Blocks   Id  System  
  11. /dev/vda1   *        2048   104857566    52427759+  83  Linux  
  12.   
  13. Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 sectors  
  14. Units = sectors of 1 * 512 = 512 bytes  
  15. Sector size (logical/physical): 512 bytes / 512 bytes  
  16. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  17.   
  18. [root@ecs-79ae-0002 ~]  

上面信息表示当前的云服务器有两块磁盘,“/dev/vda”是系统盘,“/dev/vdb”是新增数据盘。

2.对新增数据盘执行分区操作

  1. [root@ecs-79ae-0002 ~]# fdisk /dev/vdb  
  2. Welcome to fdisk (util-linux 2.23.2).  
  3.   
  4. Changes will remain in memory only, until you decide to write them.  
  5. Be careful before using the write command.  
  6.   
  7. Device does not contain a recognized partition table  
  8. Building a new DOS disklabel with disk identifier 0x2f603b5b.  
  9.   
  10. Command (m for help):   

3.开始新建分区

  1. Command (m for help): n  
  2. Partition type:  
  3.    p   primary (0 primary, 0 extended, 4 free)  
  4.    e   extended  
  5. Select (default p):   

表示磁盘有两种分区类型:

  • “p”表示主分区。
  • “e”表示扩展分区。

4.开始创建一个主分区

  1. Partition type:  
  2.    p   primary (0 primary, 0 extended, 4 free)  
  3.    e   extended  
  4. Select (default p): p  
  5. Partition number (1-4, default 1):   

“Partition number”表示主分区编号,可以选择1-4。

5.输入主分区编号

以分区编号选择“1”为例,输入主分区编号“1”,按“Enter”。

  1. Partition number (1-4, default 1): 1  
  2. First sector (2048-1048575999, default 2048):   

“First sector”表示起始磁柱值,可以选择2048-1048575999,默认为2048。

6.选择起始磁柱值

以选择默认起始磁柱值2048为例,按“Enter”。

系统会自动提示分区可用空间的起始磁柱值和截止磁柱值,可以在该区间内自定义,或者使用默认值。起始磁柱值必须小于分区的截止磁柱值。

  1. First sector (2048-1048575999, default 2048):   
  2. Using default value 2048  
  3. Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999):   

“Last sector”表示截止磁柱值,可以选择2048-1048575999,默认为1048575999。

7.选择截止磁柱值

以选择默认截止磁柱值1048575999为例,按“Enter”。

系统会自动提示分区可用空间的起始磁柱值和截止磁柱值,可以在该区间内自定义,或者使用默认值。起始磁柱值必须小于分区的截止磁柱值。

  1. Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999):   
  2. Using default value 1048575999  
  3. Partition 1 of type Linux and of size 500 GiB is set  
  4.   
  5. Command (m for help):   

表示分区完成,即为数据盘新建了1个分区。

8.查看新建分区的详细信息

输入“p”,按“Enter”,查看新建分区的详细信息。

  1. Command (m for help): p  
  2.   
  3. Disk /dev/vdb: 536.9 GB, 536870912000 bytes, 1048576000 sectors  
  4. Units = sectors of 1 * 512 = 512 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  7. Disk label type: dos  
  8. Disk identifier: 0x2f603b5b  
  9.   
  10.    Device Boot      Start         End      Blocks   Id  System  
  11. /dev/vdb1            2048  1048575999   524286976   83  Linux  
  12.   
  13. Command (m for help):  

表示新建分区“/dev/vdb1”的详细信息。

9.将分区结果写入分区表中

  1. Command (m for help): w  
  2. The partition table has been altered!  
  3.   
  4. Calling ioctl() to re-read partition table.  
  5. Syncing disks.  
  6. [root@ecs-79ae-0002 ~]#  

表示分区创建完成。

10.将新的分区表变更同步至操作系统

执行以下命令,将新的分区表变更同步至操作系统。

  1. [root@ecs-79ae-0002 ~]# partprobe  

11.将新建分区文件系统设为系统所需格式

执行以下命令,将新建分区文件系统设为系统所需格式。

  1. [root@ecs-79ae-0002 ~]# mkfs -t ext4 /dev/vdb1  
  2. mke2fs 1.42.9 (28-Dec-2013)  
  3. Filesystem label=  
  4. OS type: Linux  
  5. Block size=4096 (log=2)  
  6. Fragment size=4096 (log=2)  
  7. Stride=0 blocks, Stripe width=0 blocks  
  8. 32768000 inodes, 131071744 blocks  
  9. 6553587 blocks (5.00%) reserved for the super user  
  10. First data block=0  
  11. Maximum filesystem blocks=2279604224  
  12. 4000 block groups  
  13. 32768 blocks per group, 32768 fragments per group  
  14. 8192 inodes per group  
  15. Superblock backups stored on blocks:   
  16.     32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,   
  17.     4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,   
  18.     102400000  
  19.   
  20. Allocating group tables: done                              
  21. Writing inode tables: done                              
  22. Creating journal (32768 blocks): done  
  23. Writing superblocks and filesystem accounting information: done   

12.新建挂载目录

  1. [root@ecs-79ae-0002 ~]# mkdir /mydata  

13.将新建分区挂载到创建的目录下

  1. [root@ecs-79ae-0002 ~]# mount /dev/vdb1 /mydata  

14.查看挂载结果

  1. [root@ecs-79ae-0002 ~]# df -TH  
  2. Filesystem     Type      Size  Used Avail Use% Mounted on  
  3. devtmpfs       devtmpfs   34G     0   34G   0% /dev  
  4. tmpfs          tmpfs      34G     0   34G   0% /dev/shm  
  5. tmpfs          tmpfs      34G  9.0M   34G   1% /run  
  6. tmpfs          tmpfs      34G     0   34G   0% /sys/fs/cgroup  
  7. /dev/vda1      ext4       53G  2.4G   48G   5% /  
  8. tmpfs          tmpfs     6.8G     0  6.8G   0% /run/user/0  
  9. /dev/vdb1      ext4      529G   76M  502G   1% /mydata  
  10. [root@ecs-79ae-0002 ~]  

表示新建分区“/dev/vdb1”已挂载至“/mydata”。

但是上面的手动挂载当云服务器重启后失效,又需要挂载,下面设置开机自动挂载。

二.设置开机自动挂载磁盘分区

下面介绍如何在fstab文件中使用UUID来设置自动挂载磁盘分区。

注:UUID(universally unique identifier)是Linux系统为磁盘分区提供的唯一的标识字符串。

1.查询磁盘分区的UUID

  1. [root@ecs-79ae-0002 ~]# blkid /dev/vdb1  
  2. /dev/vdb1: UUID="ee09a54c-4132-4578-8040-7bc86768a057" TYPE="ext4"   

2.编辑器fstab文件

  1. [root@ecs-79ae-0002 ~]# vim /etc/fstab  

3.编辑的内容

  1. # /etc/fstab  
  2. # Created by anaconda on Thu Feb 10 07:01:09 2022  
  3. #  
  4. # Accessible filesystems, by reference, are maintained under '/dev/disk'  
  5. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info  
  6. #  
  7. UUID=6c533615-cacd-47a7-844e-10013da6d35b /                       ext4    defaults        1 1  
  8. UUID=ee09a54c-4132-4578-8040-7bc86768a057 /mydata                 ext4    defaults        0 2  
  9. [root@ecs-79ae-0002 ~]  

关于fstab相关参数的解释如下:

  1. 第一列为UUID,此处填写1中查询到的磁盘分区的UUID。  
  2. 第二列为磁盘分区的挂载目录,可以通过df -TH命令查询。  
  3. 第三列为磁盘分区的文件系统格式, 可以通过df -TH命令查询。  
  4. 第四列为磁盘分区的挂载选项,此处通常设置为defaults即可。  
  5. 第五列为Linux dump备份选项。  
  6. 0表示不使用Linux dump备份。现在通常不使用dump备份,此处设置为0即可。  
  7. 1表示使用Linux dump备份。  
  8. 第六列为fsck选项,即开机时是否使用fsck检查磁盘。  
  9. 0表示不检验。  
  10. 挂载点为(/)根目录的分区,此处必须填写1。  
  11. 根分区设置为1,其他分区只能从2开始,系统会按照数字从小到大依次检查下去。  

4.重新加载“/etc/fstab”文件并查看挂载

  1. [root@ecs-79ae-0002 ~]# mount -a  
  2. [root@ecs-79ae-0002 ~]# df -TH  
  3. Filesystem     Type      Size  Used Avail Use% Mounted on  
  4. devtmpfs       devtmpfs   34G     0   34G   0% /dev  
  5. tmpfs          tmpfs      34G     0   34G   0% /dev/shm  
  6. tmpfs          tmpfs      34G  9.1M   34G   1% /run  
  7. tmpfs          tmpfs      34G     0   34G   0% /sys/fs/cgroup  
  8. /dev/vda1      ext4       53G  2.4G   48G   5% /  
  9. /dev/vdb1      ext4      529G   76M  502G   1% /mydata  
  10. tmpfs          tmpfs     6.8G     0  6.8G   0% /run/user/0  
  11. [root@ecs-79ae-0002 ~]  
moonrong
  • 版权声明:本站原创文章,于2022年4月27日11:43:39,由 发表,共 6315 字。
  • 版权声明: 本文由于2022年4月27日11:43:39 发表在 好派笔记,共 6315 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: