当前位置:   article > 正文

云主机挂载云硬盘的操作姿势_使用os::cinder::volumeattachment完成云主机5g硬盘挂载。

使用os::cinder::volumeattachment完成云主机5g硬盘挂载。

1. 准备一台有公网IP的云主机,并通过Xshell或SecureCRT等终端模拟软件登录

  1. ~$ nova list
  2. +--------------------------------------+---------------------------------+--------+------------+-------------+----------------------------------+
  3. | ID | Name | Status | Task State | Power State | Networks |
  4. +--------------------------------------+---------------------------------+--------+------------+-------------+----------------------------------+
  5. | 72aefa0b-f199-4ddb-b721-9e94f233ced8 | vm-demo | ACTIVE | - | Running | cloud-net-check-vpc=192.168.96.2 |
  6. +--------------------------------------+---------------------------------+--------+------------+-------------+----------------------------------+

2. 创建100G云硬盘

  1. ~$ cinder create --availability_zone yiqiao1 --volume_type ceph_sas --display_name volume-test 100
  2. +---------------------+--------------------------------------+
  3. | Property | Value |
  4. +---------------------+--------------------------------------+
  5. | attachments | [] |
  6. | availability_zone | yiqiao1 |
  7. | bootable | false |
  8. | created_at | 2019-06-15T08:08:09.164567 |
  9. | display_description | None |
  10. | display_name | volume-test |
  11. | id | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 |
  12. | metadata | {} |
  13. | size | 100 |
  14. | snapshot_id | None |
  15. | source_volid | None |
  16. | status | creating |
  17. | volume_type | ceph_sas |
  18. +---------------------+--------------------------------------+
  19. # 云硬盘为available状态时代表可挂载
  20. (my_new_env) likailiang@pubbeta1-nova10:~$ nova volume-list
  21. +--------------------------------------+-----------+---------------------------+------+-------------+--------------------------------------+
  22. | ID | Status | Display Name | Size | Volume Type | Attached to |
  23. +--------------------------------------+-----------+---------------------------+------+-------------+--------------------------------------+
  24. | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 | available | volume-test | 100 | ceph_sas | |
  25. +--------------------------------------+-----------+---------------------------+------+-------------+--------------------------------------+

3.挂载云硬盘到云主机

3.1 挂载云硬盘之前云主机外围设备的分区状况

  1. root@virtio-scsi-nodiscard-ceph-sas:~# fdisk -l
  2. Disk /dev/sda: 20 GiB, 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. Disklabel type: dos
  7. Disk identifier: 0x68f54af3
  8. Device Boot Start End Sectors Size Id Type
  9. /dev/sda1 * 2048 41943006 41940959 20G 83 Linux
  10. Disk /dev/sdc: 64 MiB, 67108864 bytes, 131072 sectors
  11. Units: sectors of 1 * 512 = 512 bytes
  12. Sector size (logical/physical): 512 bytes / 512 bytes
  13. I/O size (minimum/optimal): 512 bytes / 512 bytes
  14. Disklabel type: dos
  15. Disk identifier: 0x00000000
  16. root@virtio-scsi-nodiscard-ceph-sas:~#

3.2 挂载云硬盘到云主机

  1. (my_new_env) myy@pubbeta1-nova10:~$ nova volume-attach 72aefa0b-f199-4ddb-b721-9e94f233ced8 c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7
  2. +----------+--------------------------------------+
  3. | Property | Value |
  4. +----------+--------------------------------------+
  5. | device | /dev/sdb |
  6. | id | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 |
  7. | serverId | 72aefa0b-f199-4ddb-b721-9e94f233ced8 |
  8. | volumeId | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 |
  9. +----------+--------------------------------------+

3.3 挂载云硬盘之后云主机外围设备的分区状况

  1. root@virtio-scsi-nodiscard-ceph-sas:~# fdisk -l
  2. Disk /dev/sda: 20 GiB, 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. Disklabel type: dos
  7. Disk identifier: 0x68f54af3
  8. Device Boot Start End Sectors Size Id Type
  9. /dev/sda1 * 2048 41943006 41940959 20G 83 Linux
  10. Disk /dev/sdc: 64 MiB, 67108864 bytes, 131072 sectors
  11. Units: sectors of 1 * 512 = 512 bytes
  12. Sector size (logical/physical): 512 bytes / 512 bytes
  13. I/O size (minimum/optimal): 512 bytes / 512 bytes
  14. Disklabel type: dos
  15. Disk identifier: 0x00000000
  16. Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
  17. Units: sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes

挂载前后的fdisk -l命令执行结果对比,发现挂载云硬盘后会出现一块新的硬盘 /dev/sdb,大小为100G。也可以通过nova show 查看系统扩展卷的信息

  1. (my_new_env) likailiang@pubbeta1-nova10:~$ nova show 72aefa0b-f199-4ddb-b721-9e94f233ced8
  2. +----------------------------------------------+-----------------------------------------------------------------------------------------------------------+
  3. | Property | Value |
  4. +----------------------------------------------+-----------------------------------------------------------------------------------------------------------+
  5. | OS-DCF:diskConfig | MANUAL |
  6. | OS-EXT-AZ:availability_zone | yiqiao1.intel_core_vpc1 |
  7. | OS-EXT-SRV-ATTR:host | pubbeta1-nova90.yq.163.org |
  8. | OS-EXT-SRV-ATTR:hypervisor_hostname | pubbeta1-nova90.yq.163.org |
  9. | OS-EXT-SRV-ATTR:instance_name | instance-00a11c37 |
  10. | OS-EXT-STS:power_state | 1 |
  11. | OS-EXT-STS:task_state | - |
  12. | OS-EXT-STS:vm_state | active |
  13. | OS-SRV-USG:launched_at | 2019-05-23T09:23:07.000000 |
  14. | OS-SRV-USG:terminated_at | - |
  15. | accessIPv4 | |
  16. | accessIPv6 | |
  17. | availability_zone | yiqiao1.intel_core_vpc1 |
  18. | business_type | normal |
  19. | ceph_pool | switch02_sas_vms |
  20. | cloud-net-check-vpc network | 192.168.96.2 |
  21. | config_drive | 1 |
  22. | created | 2019-05-23T09:23:00Z |
  23. | flavor | flavor_54 (54) |
  24. | hostId | 51464028f275ea2bf91cafcb89022951644b8336294a890815b8d83d |
  25. | hypervisor_type | qemu |
  26. | id | 72aefa0b-f199-4ddb-b721-9e94f233ced8 |
  27. | image | debian9.1_virtio_scsi_test (75c0df36-a1cd-4b07-a0a5-fec65d3639c2) |
  28. | instance-qos:read_bps | 104857600 |
  29. | instance-qos:read_iops | 1000 |
  30. | instance-qos:write_bps | 104857600 |
  31. | instance-qos:write_iops | 1000 |
  32. | key_name | id_rsa |
  33. | metadata | {"use-vpc": "true"} |
  34. | name | virtio_scsi_nodiscard_ceph_sas |
  35. | os-extended-volumes:volumes_attached | [{"id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7"}] |
  36. | os-netease-extended-volumes:volumes_attached | [{"delete_on_terminate": false, "id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7", "device_name": "/dev/sdb"}] |
  37. | os-server-status | up |
  38. | os_type | linux |
  39. | progress | 0 |
  40. | status | ACTIVE |
  41. | tenant_id | 1951eb911bf444f299e295ef455bafc4 |
  42. | updated | 2019-05-23T12:14:29Z |
  43. | use_ceph | yes |
  44. | user_id | 7f339ebbc2bb4688ac753c483d97b644 |
  45. +----------------------------------------------+-----------------------------------------------------------------------------------------------------------+

其中 "/dev/sdb"代表设备的名字,

  1. | os-extended-volumes:volumes_attached | [{"id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7"}] |
  2. | os-netease-extended-volumes:volumes_attached | [{"delete_on_terminate": false, "id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7", "device_name": "/dev/sdb"}] |

4.磁盘分区

4.1 创建分区

运行 fdisk /dev/sdb,对数据盘进行分区。根据提示,依次输入 n,p,1,两次回车,wq,分区就开始了。出现Created a new partition 1 of type 'Linux' and of size 100 GiB.代表分区成功。

  1. root@virtio-scsi-nodiscard-ceph-sas:~# fdisk /dev/sdb
  2. Welcome to fdisk (util-linux 2.29.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. Created a new DOS disklabel with disk identifier 0xcf3e7bcc.
  7. Command (m for help): n
  8. Partition type
  9. p primary (0 primary, 0 extended, 4 free)
  10. e extended (container for logical partitions)
  11. Select (default p): p
  12. Partition number (1-4, default 1): 1
  13. First sector (2048-209715199, default 2048):
  14. Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):
  15. Created a new partition 1 of type 'Linux' and of size 100 GiB.
  16. Command (m for help): wq
  17. The partition table has been altered.
  18. Calling ioctl() to re-read partition table.
  19. Syncing disks.
  20. root@virtio-scsi-nodiscard-ceph-sas:~#

4.2 查看新创建的分区

  1. root@virtio-scsi-nodiscard-ceph-sas:~# fdisk -l
  2. Disk /dev/sda: 20 GiB, 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. Disklabel type: dos
  7. Disk identifier: 0x68f54af3
  8. Device Boot Start End Sectors Size Id Type
  9. /dev/sda1 * 2048 41943006 41940959 20G 83 Linux
  10. Disk /dev/sdc: 64 MiB, 67108864 bytes, 131072 sectors
  11. Units: sectors of 1 * 512 = 512 bytes
  12. Sector size (logical/physical): 512 bytes / 512 bytes
  13. I/O size (minimum/optimal): 512 bytes / 512 bytes
  14. Disklabel type: dos
  15. Disk identifier: 0x00000000
  16. Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
  17. Units: sectors of 1 * 512 = 512 bytes
  18. Sector size (logical/physical): 512 bytes / 512 bytes
  19. I/O size (minimum/optimal): 512 bytes / 512 bytes
  20. Disklabel type: dos
  21. Disk identifier: 0xcf3e7bcc
  22. Device Boot Start End Sectors Size Id Type
  23. /dev/sdb1 2048 209715199 209713152 100G 83 Linux

5. 格式化磁盘

格式化所需时间取决于数据盘大小,100G云硬盘格式化时间大概在3s-10s之间。本次格式化选择的文件系统为ext3,也可选用其他文件格式,如 ext4 等。

  1. root@virtio-scsi-nodiscard-ceph-sas:~# mkfs.ext3 /dev/sdb
  2. mke2fs 1.43.4 (31-Jan-2017)
  3. Found a dos partition table in /dev/sdb
  4. Proceed anyway? (y,N) y
  5. Discarding device blocks: done
  6. Creating filesystem with 26214400 4k blocks and 6553600 inodes
  7. Filesystem UUID: eee12366-e32e-4459-b3d0-30bbfed666ae
  8. Superblock backups stored on blocks:
  9. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  10. 4096000, 7962624, 11239424, 20480000, 23887872
  11. Allocating group tables: done
  12. Writing inode tables: done
  13. Creating journal (131072 blocks): done
  14. Writing superblocks and filesystem accounting information: done

运行 echo /dev/sdb /mnt ext3 defaults 0 0 >> /etc/fstab 写入新分区信息。完成后,可以使用 cat /etc/fstab 命令查看。

  1. root@virtio-scsi-nodiscard-ceph-sas:~# echo /dev/sdb /mnt ext3 defaults 0 0 >> /etc/fstab
  2. root@virtio-scsi-nodiscard-ceph-sas:~# cat /etc/fstab
  3. # /etc/fstab: static file system information.
  4. #
  5. # Use 'blkid' to print the universally unique identifier for a
  6. # device; this may be used with UUID= as a more robust way to name devices
  7. # that works even if disks are added and removed. See fstab(5).
  8. #
  9. # <file system> <mount point> <type> <options> <dump> <pass>
  10. # / was on /dev/vda1 during installation
  11. UUID=2287f0e8-f6ef-4c59-95fb-b62ea61f777f / ext4 discard,errors=remount-ro 0 1
  12. /dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
  13. /dev/sdb /mnt ext3 defaults 0 0

通过命令mount -a,实现修改/etc/fstab后不重启且生效。然后执行 df -h 查看分区。如果出现数据盘信息,说明挂载成功,可以使用新分区了。

  1. root@virtio-scsi-nodiscard-ceph-sas:~# mount -a
  2. root@virtio-scsi-nodiscard-ceph-sas:~# df -h
  3. Filesystem Size Used Avail Use% Mounted on
  4. udev 3.9G 0 3.9G 0% /dev
  5. tmpfs 798M 84M 715M 11% /run
  6. /dev/sda1 20G 2.1G 17G 11% /
  7. tmpfs 3.9G 0 3.9G 0% /dev/shm
  8. tmpfs 5.0M 0 5.0M 0% /run/lock
  9. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  10. tmpfs 798M 0 798M 0% /run/user/0
  11. /dev/sdb 98G 61M 93G 1% /mnt

自此云主机挂载云硬盘成功。

6. linux使用dd命令快速生成大文件

dd命令可以轻易实现创建指定大小的文件,如

dd if=/dev/zero of=test bs=1M count=1000

会生成一个1000M的test文件,文件内容为全0(因从/dev/zero中读取,/dev/zero为0源)

  1. root@virtio-scsi-nodiscard-ceph-sas:/mnt# ls
  2. lost+found
  3. root@virtio-scsi-nodiscard-ceph-sas:/mnt# dd if=/dev/zero of=test bs=1M count=1000
  4. 1000+0 records in
  5. 1000+0 records out
  6. 1048576000 bytes (1.0 GB, 1000 MiB) copied, 2.02557 s, 518 MB/s
  7. root@virtio-scsi-nodiscard-ceph-sas:/mnt# ls -ahl
  8. total 1002M
  9. drwxr-xr-x 3 root root 4.0K Jun 15 16:33 .
  10. drwxr-xr-x 22 root root 4.0K May 23 20:14 ..
  11. drwx------ 2 root root 16K Jun 15 16:22 lost+found
  12. -rw-r--r-- 1 root root 1000M Jun 15 16:33 test

但是这样为实际写入硬盘,文件产生速度取决于硬盘读写速度,如果欲产生超大文件,速度很慢。在某种场景下,我们只想让文件系统认为存在一个超大文件在此,但是并不实际写入硬盘则可以

dd if=/dev/zero of=test bs=1M count=0 seek=100000

此时创建的文件在文件系统中的显示大小为100000MB,但是并不实际占用block,因此创建速度与内存速度相当。seek的作用是跳过输出文件中指定大小的部分,这就达到了创建大文件,但是并不实际写入的目的。当然,因为不实际写入硬盘,所以你在容量只有10G的硬盘上创建100G的此类文件都是可以的

参考:挂载云硬盘https://www.backendcloud.cn/2017/06/30/attach-yunyingpan/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/217415
推荐阅读
相关标签
  

闽ICP备14008679号