阿里云服务格式化和挂载数据盘

原文路径 :https://help.aliyun.com/document_detail/25426.html?spm=5176.doc25446.2.6.VXEZ2Rhtml

若是您已经为 ECS 实例配了数据盘,您须要先格式化数据盘并挂载文件系统后才能正常使用数据盘。node

注意服务器

  • 磁盘分区和格式化是高风险行为,请慎重操做。本文档描述如何处理一个新买的数据盘,若是您的数据盘上有数据,请务必对数据盘建立快照以免可能的数据丢失。ide

  • 云服务器 ECS 仅支持对 数据盘 进行分区,而不支持对 系统盘 进行分区。若是您强行使用第三方工具对系统盘进行分区操做,可能引起未知风险,如系统崩溃、数据丢失等工具

本文描述如何用一个新的数据盘建立一个单分区数据盘并挂载文件系统。本文仅适用于使用 fdisk 命令对一个不大于 2 TB 的数据盘执行分区操做。若是须要分区的数据盘大于 2 TB,请参考 32TB 块存储分区优化

您还能够根据业务须要,对数据盘进行多分区配置。建议使用系统自带的工具进行分区操做。ui

操做须知

单独 购买的数据盘 须要先 挂载数据盘,而后才能格式化。spa

随实例时一块儿购买的数据盘,无需挂载,直接格式化。操作系统

您须要知道挂载到实例的数据盘的设备名:您能够经过 ECS 管理控制台 > 磁盘详情 > 磁盘挂载信息 查到数据盘的 设备名。数据盘的设备名由系统默认分配,从 /dev/xvdb 开始日后顺序排列,分布范围包括 /dev/xvdb/dev/xvdz3d

操做步骤

在本示例中,咱们用一个新的 20 GB 数据盘(设备名为 /dev/xvdb)建立一个单分区数据盘并挂载一个 ext3 文件系统。使用的实例是 I/O 优化实例,操做系统为 CentOS 6.8。

  1. 远程链接实例

  2. 运行 fdisk -l 命令查看实例是否有数据盘。若是执行命令后,没有发现 /dev/vdb,表示您的实例没有数据盘,无需格式化数据盘,请忽略本文后续内容。

    • 若是您的数据盘显示的是 dev/xvd?,表示您使用的是非 I/O 优化实例。

    • 其中 ? 是 a−z 的任一个字母。

  3. 建立一个单分区数据盘,依次执行如下命令:

    1. 运行 fdisk /dev/vdb:对数据盘进行分区。

    2. 输入 n 并按回车键:建立一个新分区。

    3. 输入 p 并按回车键:选择主分区。由于建立的是一个单分区数据盘,因此只须要建立主分区。

      说明:若是要建立 4 个以上的分区,您应该建立至少一个扩展分区,即选择 e

    4. 输入分区编号并按回车键。由于这里仅建立一个分区,能够输入 1。

    5. 输入第一个可用的扇区编号:按回车键采用默认值 1。

    6. 输入最后一个扇区编号:由于这里仅建立一个分区,因此按回车键采用默认值。

    7. 输入 wq 并按回车键,开始分区。

       
      1. [root@iXXXXXXX ~]# fdisk /dev/vdb
      2. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
      3. Building a new DOS disklabel with disk identifier 0x5f46a8a2.
      4. Changes will remain in memory only, until you decide to write them.
      5. After that, of course, the previous content won't be recoverable.
      6. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
      7. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
      8. switch off the mode (command 'c') and change display units to
      9. sectors (command 'u').
      10. Command (m for help): n
      11. Command action
      12. e extended
      13. p primary partition (1-4)
      14. p
      15. Partition number (1-4): 1
      16. First cylinder (1-41610, default 1): 1
      17. Last cylinder, +cylinders or +size{K,M,G} (1-41610, default 41610):
      18. Using default value 41610
      19. Command (m for help): wq
      20. The partition table has been altered!
      21. Calling ioctl() to re-read partition table.
      22. Syncing disks.
  4. 查看新的分区:运行命令 fdisk -l。若是出现如下信息,说明已经成功建立了新分区 /dev/vdb1。

     
    1. [root@iXXXXXXX ~]# fdisk -l
    2. Disk /dev/vda: 42.9 GB, 42949672960 bytes
    3. 255 heads, 63 sectors/track, 5221 cylinders
    4. Units = cylinders of 16065 * 512 = 8225280 bytes
    5. Sector size (logical/physical): 512 bytes / 512 bytes
    6. I/O size (minimum/optimal): 512 bytes / 512 bytes
    7. Disk identifier: 0x00053156
    8. Device Boot Start End Blocks Id System
    9. /dev/vda1 * 1 5222 41942016 83 Linux
    10. Disk /dev/vdb: 21.5 GB, 21474836480 bytes
    11. 16 heads, 63 sectors/track, 41610 cylinders
    12. Units = cylinders of 1008 * 512 = 516096 bytes
    13. Sector size (logical/physical): 512 bytes / 512 bytes
    14. I/O size (minimum/optimal): 512 bytes / 512 bytes
    15. Disk identifier: 0x5f46a8a2
    16. Device Boot Start End Blocks Id System
    17. /dev/vdb1 1 41610 20971408+ 83 Linux
  5. 在新分区上建立一个文件系统:运行命令 mkfs.ext3 /dev/vdb1

    • 本示例要建立一个 ext3 文件系统。您也能够根据本身的须要,选择建立其余文件系统,例如,若是须要在 Linux、Windows 和 Mac 系统之间共享文件,您可使用 mkfs.vfat 建立 VFAT 文件系统。

    • 建立文件系统所需时间取决于数据盘大小。

       
      1. [root@iXXXXXXX ~]# mkfs.ext3 /dev/vdb1
      2. mke2fs 1.41.12 (17-May-2010)
      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, 5242852 blocks
      9. 262142 blocks (5.00%) reserved for the super user
      10. First data block=0
      11. Maximum filesystem blocks=4294967296
      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. Writing inode tables: done
      19. Creating journal (32768 blocks): done
      20. Writing superblocks and filesystem accounting information: done
      21. This filesystem will be automatically checked every 37 mounts or
      22. 180 days, whichever comes first. Use tune2fs -c or -i to override.
  6. (建议)备份 etc/fstab:运行命令 cp /etc/fstab /etc/fstab.bak

  7. 向 /etc/fstab 写入新分区信息:运行命令 echo /dev/vdb1 /mnt ext3 defaults 0 0 >> /etc/fstab

    注意:Ubuntu 12.04 不支持 barrier,因此对该系统正确的命令是:echo '/dev/vdb1 /mnt ext3 barrier=0 0 0' >> /etc/fstab

    若是须要把数据盘单独挂载到某个文件夹,好比单独用来存放网页,请将以上命令 /mnt 替换成所需的挂载点路径。

  8. 查看 /etc/fstab 中的新分区信息:运行命令 cat /etc/fstab

     
    1. [root@iXXXXXXX ~]# cat /etc/fstab
    2. #
    3. # /etc/fstab
    4. # Created by anaconda on Thu Feb 23 07:28:22 2017
    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=3d083579-f5d9-4df5-9347-8d27925805d4 / ext4 defaults 1 1
    10. tmpfs /dev/shm tmpfs defaults 0 0
    11. devpts /dev/pts devpts gid=5,mode=620 0 0
    12. sysfs /sys sysfs defaults 0 0
    13. proc /proc proc defaults 0 0
    14. /dev/vdb1 /mnt ext3 defaults 0 0
  9. 挂载文件系统:运行命令 mount /dev/vdb1 /mnt

  10. 查看目前磁盘空间和使用状况:运行命令 df -h。若是出现新建文件系统的信息,说明挂载成功,可使用新的文件系统了。

    挂载操做完成后,不须要重启实例便可开始使用新的文件系统。

     
    1. [root@iXXXXXXX ~]# mount /dev/vdb1 /mnt
    2. [root@iXXXXXXX ~]# df -h
    3. Filesystem Size Used Avail Use% Mounted on
    4. /dev/vda1 40G 6.6G 31G 18% /
    5. tmpfs 499M 0 499M 0% /dev/shm
    6. /dev/vdb1 20G 173M 19G 1% /mnt