知识学习

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3553|回复: 0

Linux格式化数据盘Linux挂载数据盘

[复制链接]

升级   100%

152

主题

0

回帖

752

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
752
发表于 2020-5-2 20:17:41 | 显示全部楼层 |阅读模式
一块全新的数据盘挂载到ECS实例后,您必须创建并挂载至少一个文件系统。本示例使用I/O优化实例,操作系统为CentOS 7.6,为一块新的20GiB数据盘(设备名为/dev/vdb)创建一个MBR格式的单分区,挂载的是ext4文件系统。
前提条件
随实例一起购买的数据盘,无需挂载。单独购买的数据盘必须挂载数据盘后才能格式化,详情请参见挂载数据盘

背景信息
本文操作仅适用小于等于2TiB的数据盘。大于2TiB的数据盘分区必须使用GPT格式,请参见分区格式化大于2 TiB数据盘
数据盘的设备名默认由系统分配。I/O优化实例的数据盘设备名从/dev/vdb递增排列,包括/dev/vdb−/dev/vdz。非I/O优化实例的数据盘设备名从/dev/xvdb递增排列,包括/dev/xvdb−/dev/xvdz。
格式化操作可能存在如下风险:
  • 磁盘分区和格式化是高风险行为,请慎重操作。本文操作仅适用处理一块全新的数据盘,如果您的数据盘上有数据,请务必为数据盘创建快照,避免数据丢失。详情请参见创建快照
  • 云服务器ECS仅支持数据盘分区操作,不支持系统盘分区操作。如果您强行使用第三方工具对系统盘做分区操作,可能引发系统崩溃和数据丢失等未知风险。仅允许在扩容系统盘后做扩展分区或新增分区操作,具体操作请参见扩展分区和文件系统_Linux系统盘

操作步骤
  • 远程连接ECS实例。具体步骤请参见连接ECS实例
  • 运行fdisk -l命令查看实例上的数据盘。
    说明 执行命令后,如果不存在/dev/vdb,表示您的实例没有数据盘。确认数据盘是否已挂载。
  • 依次运行以下命令,创建一个分区。
    • 运行fdisk -u /dev/vdb命令分区数据盘。
    • 输入p查看数据盘的分区情况。本示例中,数据盘没有分区。
    • 输入n创建一个新分区。
    • 输入p选择分区类型为主分区。
      说明 创建一个单分区数据盘可以只创建主分区。如果要创建四个以上分区,您应该至少选择一次e(extended),创建至少一个扩展分区。
    • 输入分区编号并按回车键。本示例中,仅创建一个分区,输入1。
    • 输入第一个可用的扇区编号,按回车键采用默认值2048。
    • 输入最后一个扇区编号。本示例中,仅创建一个分区,按回车键采用默认值。
    • 输入p查看该数据盘的规划分区情况。
    • 输入w开始分区,并在完成分区后退出。
      1. [root@ecshost~ ]# fdisk -u /dev/vdb
      2. Welcome to fdisk (util-linux 2.23.2).
      3. Changes will remain in memory only, until you decide to write them.
      4. Be careful before using the write command.
      5. Device does not contain a recognized partition table
      6. Building a new DOS disklabel with disk identifier 0x3e60020e.

      7. Command (m for help): p
      8. Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
      9. Units = sectors of 1 * 512 = 512 bytes
      10. Sector size (logical/physical): 512 bytes / 512 bytes
      11. I/O size (minimum/optimal): 512 bytes / 512 bytes
      12. Disk label type: dos
      13. Disk identifier: 0x3e60020e
      14. Device Boot Start End Blocks Id System

      15. Command (m for help): n
      16. Partition type:
      17. p primary (0 primary, 0 extended, 4 free)
      18. e extended
      19. Select (default p): p
      20. Partition number (1-4, default 1): 1
      21. First sector (2048-41943039, default 2048):
      22. Using default value 2048
      23. Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
      24. Using default value 41943039
      25. Partition 1 of type Linux and of size 20 GiB is set

      26. Command (m for help): p

      27. Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
      28. Units = sectors of 1 * 512 = 512 bytes
      29. Sector size (logical/physical): 512 bytes / 512 bytes
      30. I/O size (minimum/optimal): 512 bytes / 512 bytes
      31. Disk label type: dos
      32. Disk identifier: 0x3e60020e
      33. Device Boot Start End Blocks Id System
      34. /dev/vdb1 2048 41943039 20970496 83 Linux

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

      37. Calling ioctl() to re-read partition table.
      38. Syncing disks.
      复制代码

  • 运行fdisk -lu /dev/vdb命令查看新分区。如果出现/dev/vdb1的相关信息,表示新分区已创建完成。
    1. [root@ecshost~ ]# fdisk -lu /dev/vdb

    2. Disk /dev/vdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
    3. Units = sectors of 1 * 512 = 512 bytes
    4. Sector size (logical/physical): 512 bytes / 512 bytes
    5. I/O size (minimum/optimal): 512 bytes / 512 bytes
    6. Disk label type: dos
    7. Disk identifier: 0x3e60020e

    8. Device Boot Start End Blocks Id System
    9. /dev/vdb1 2048 41943039 20970496 83 Linux
    复制代码

  • 运行mkfs.ext4 /dev/vdb1命令在新分区上创建一个文件系统。
    本示例中,创建一个ext4文件系统。其他文件系统格式请自行修改mkfs.ext4命令。例如,如果您需要在Linux、Windows和Mac系统之间共享文件,可以运行mkfs.vfat /dev/vdb1命令创建VFAT文件系统。
    1. [root@ecshost~ ]# mkfs.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. 1310720 inodes, 5242624 blocks
    9. 262131 blocks (5.00%) reserved for the super user
    10. First data block=0
    11. Maximum filesystem blocks=2153775104
    12. 160 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

    18. Allocating group tables: done
    19. Writing inode tables: done
    20. Creating journal (32768 blocks): done
    21. Writing superblocks and filesystem accounting information: done
    复制代码

  • 向/etc/fstab写入新分区信息,启动开机自动挂载分区。
    • 运行命令cp /etc/fstab /etc/fstab.bak,备份etc/fstab。
    • 运行命令echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab,向/etc/fstab里写入新分区信息。
      如果要把数据盘单独挂载到某个文件夹,例如单独用来存放网页,则将命令中/mnt替换成所需的挂载点路径。

      说明
      • 建议在/etc/fstab中使用全局唯一标识符UUID来引用新分区。您可以使用blkid命令获得新分区的UUID。
      • Ubuntu 12.04系统不支持barrier,您需要运行echo '`blkid /dev/vdb1 | awk '{print $3}' | sed 's/\"//g'` /mnt ext4 barrier=0 0 0' >> /etc/fstab命令。




  • 运行cat /etc/fstab命令查看/etc/fstab中的新分区信息。
    1. [root@ecshost~ ]# cat /etc/fstab
    2. #
    3. # /etc/fstab
    4. # Created by anaconda on Wed Dec 12 07:53:08 2018
    5. #
    6. # Accessible filesystems, by reference, are maintained under '/dev/disk'
    7. # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    8. #
    9. UUID=d67c3b17-255b-4687-be04-f29190d4**** / ext4 defaults 1 1
    10. UUID=a4c73111-3a33-4569-a90c-b6d2e953**** /mnt ext4 defaults 0 0
    复制代码

  • 运行mount /dev/vdb1 /mnt命令挂载文件系统。如果运行df -h命令后出现新建文件系统的信息,表示文件系统挂载成功。
    1. [root@ecshost~ ]# mount /dev/vdb1 /mnt
    2. [root@ecshost~ ]# df -h

    3. Filesystem Size Used Avail Use% Mounted on
    4. /dev/vda1 40G 1.6G 36G 5% /
    5. devtmpfs 234M 0 234M 0% /dev
    6. tmpfs 244M 0 244M 0% /dev/shm
    7. tmpfs 244M 484K 244M 1% /run
    8. tmpfs 244M 0 244M 0% /sys/fs/cgroup
    9. tmpfs 49M 0 49M 0% /run/user/0
    10. /dev/vdb1 20G 45M 19G 1% /mnt
    复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|知识学习 ( 鲁ICP备20015483号 )

GMT+8, 2026-5-13 21:48 , Processed in 0.066391 second(s), 24 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表