欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

云主机挂载云硬盘的操作姿势

程序员文章站 2022-05-11 16:25:53
...

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

~$ nova list
+--------------------------------------+---------------------------------+--------+------------+-------------+----------------------------------+
| ID                                   | Name                            | Status | Task State | Power State | Networks                         |
+--------------------------------------+---------------------------------+--------+------------+-------------+----------------------------------+

| 72aefa0b-f199-4ddb-b721-9e94f233ced8 | vm-demo  | ACTIVE | -          | Running     | cloud-net-check-vpc=192.168.96.2 |

+--------------------------------------+---------------------------------+--------+------------+-------------+----------------------------------+

2. 创建100G云硬盘

~$  cinder create  --availability_zone yiqiao1 --volume_type  ceph_sas --display_name volume-test 100
+---------------------+--------------------------------------+
|       Property      |                Value                 |
+---------------------+--------------------------------------+
|     attachments     |                  []                  |
|  availability_zone  |               yiqiao1                |
|       bootable      |                false                 |
|      created_at     |      2019-06-15T08:08:09.164567      |
| display_description |                 None                 |
|     display_name    |             volume-test              |
|          id         | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 |
|       metadata      |                  {}                  |
|         size        |                 100                  |
|     snapshot_id     |                 None                 |
|     source_volid    |                 None                 |
|        status       |               creating               |
|     volume_type     |               ceph_sas               |
+---------------------+--------------------------------------+

# 云硬盘为available状态时代表可挂载
(my_new_env) [email protected]:~$ nova volume-list 
+--------------------------------------+-----------+---------------------------+------+-------------+--------------------------------------+
| ID                                   | Status    | Display Name              | Size | Volume Type | Attached to                          |
+--------------------------------------+-----------+---------------------------+------+-------------+--------------------------------------+
| c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 | available | volume-test               | 100  | ceph_sas    |                                      |
+--------------------------------------+-----------+---------------------------+------+-------------+--------------------------------------+

3.挂载云硬盘到云主机

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

[email protected]:~# fdisk -l 
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x68f54af3

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 41943006 41940959  20G 83 Linux


Disk /dev/sdc: 64 MiB, 67108864 bytes, 131072 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
Disklabel type: dos
Disk identifier: 0x00000000
[email protected]:~# 

3.2 挂载云硬盘到云主机

(my_new_env) [email protected]:~$ nova volume-attach 72aefa0b-f199-4ddb-b721-9e94f233ced8 c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 
+----------+--------------------------------------+
| Property | Value                                |
+----------+--------------------------------------+
| device   | /dev/sdb                             |
| id       | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 |
| serverId | 72aefa0b-f199-4ddb-b721-9e94f233ced8 |
| volumeId | c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7 |
+----------+--------------------------------------+

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

[email protected]:~# fdisk -l 
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x68f54af3

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 41943006 41940959  20G 83 Linux


Disk /dev/sdc: 64 MiB, 67108864 bytes, 131072 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
Disklabel type: dos
Disk identifier: 0x00000000


Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 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

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

(my_new_env) [email protected]:~$ nova show  72aefa0b-f199-4ddb-b721-9e94f233ced8
+----------------------------------------------+-----------------------------------------------------------------------------------------------------------+
| Property                                     | Value                                                                                                     |
+----------------------------------------------+-----------------------------------------------------------------------------------------------------------+
| OS-DCF:diskConfig                            | MANUAL                                                                                                    |
| OS-EXT-AZ:availability_zone                  | yiqiao1.intel_core_vpc1                                                                                   |
| OS-EXT-SRV-ATTR:host                         | pubbeta1-nova90.yq.163.org                                                                                |
| OS-EXT-SRV-ATTR:hypervisor_hostname          | pubbeta1-nova90.yq.163.org                                                                                |
| OS-EXT-SRV-ATTR:instance_name                | instance-00a11c37                                                                                         |
| OS-EXT-STS:power_state                       | 1                                                                                                         |
| OS-EXT-STS:task_state                        | -                                                                                                         |
| OS-EXT-STS:vm_state                          | active                                                                                                    |
| OS-SRV-USG:launched_at                       | 2019-05-23T09:23:07.000000                                                                                |
| OS-SRV-USG:terminated_at                     | -                                                                                                         |
| accessIPv4                                   |                                                                                                           |
| accessIPv6                                   |                                                                                                           |
| availability_zone                            | yiqiao1.intel_core_vpc1                                                                                   |
| business_type                                | normal                                                                                                    |
| ceph_pool                                    | switch02_sas_vms                                                                                          |
| cloud-net-check-vpc network                  | 192.168.96.2                                                                                              |
| config_drive                                 | 1                                                                                                         |
| created                                      | 2019-05-23T09:23:00Z                                                                                      |
| flavor                                       | flavor_54 (54)                                                                                            |
| hostId                                       | 51464028f275ea2bf91cafcb89022951644b8336294a890815b8d83d                                                  |
| hypervisor_type                              | qemu                                                                                                      |
| id                                           | 72aefa0b-f199-4ddb-b721-9e94f233ced8                                                                      |
| image                                        | debian9.1_virtio_scsi_test (75c0df36-a1cd-4b07-a0a5-fec65d3639c2)                                         |
| instance-qos:read_bps                        | 104857600                                                                                                 |
| instance-qos:read_iops                       | 1000                                                                                                      |
| instance-qos:write_bps                       | 104857600                                                                                                 |
| instance-qos:write_iops                      | 1000                                                                                                      |
| key_name                                     | id_rsa                                                                                                    |
| metadata                                     | {"use-vpc": "true"}                                                                                       |
| name                                         | virtio_scsi_nodiscard_ceph_sas                                                                            |
| os-extended-volumes:volumes_attached         | [{"id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7"}]                                                          |
| os-netease-extended-volumes:volumes_attached | [{"delete_on_terminate": false, "id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7", "device_name": "/dev/sdb"}] |
| os-server-status                             | up                                                                                                        |
| os_type                                      | linux                                                                                                     |
| progress                                     | 0                                                                                                         |
| status                                       | ACTIVE                                                                                                    |
| tenant_id                                    | 1951eb911bf444f299e295ef455bafc4                                                                          |
| updated                                      | 2019-05-23T12:14:29Z                                                                                      |
| use_ceph                                     | yes                                                                                                       |
| user_id                                      | 7f339ebbc2bb4688ac753c483d97b644                                                                          |
+----------------------------------------------+-----------------------------------------------------------------------------------------------------------+

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

| os-extended-volumes:volumes_attached         | [{"id": "c2a0f8dc-a1a4-4912-a0a4-990b3916e5a7"}]                                                          |
| 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.代表分区成功。

[email protected]:~# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.29.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.
Created a new DOS disklabel with disk identifier 0xcf3e7bcc.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): 

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): wq
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[email protected]:~# 

4.2 查看新创建的分区

[email protected]:~# fdisk -l 
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x68f54af3

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 41943006 41940959  20G 83 Linux


Disk /dev/sdc: 64 MiB, 67108864 bytes, 131072 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
Disklabel type: dos
Disk identifier: 0x00000000


Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xcf3e7bcc

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 209715199 209713152  100G 83 Linux

5. 格式化磁盘

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

[email protected]:~#  mkfs.ext3 /dev/sdb
mke2fs 1.43.4 (31-Jan-2017)
Found a dos partition table in /dev/sdb
Proceed anyway? (y,N) y
Discarding device blocks: done                            
Creating filesystem with 26214400 4k blocks and 6553600 inodes
Filesystem UUID: eee12366-e32e-4459-b3d0-30bbfed666ae
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done  

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

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

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

[email protected]:~# mount -a
[email protected]:~#  df -h 
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           798M   84M  715M  11% /run
/dev/sda1        20G  2.1G   17G  11% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           798M     0  798M   0% /run/user/0
/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源)

[email protected]:/mnt# ls
lost+found
[email protected]:/mnt# dd if=/dev/zero of=test bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 2.02557 s, 518 MB/s

[email protected]:/mnt# ls -ahl
total 1002M
drwxr-xr-x  3 root root  4.0K Jun 15 16:33 .
drwxr-xr-x 22 root root  4.0K May 23 20:14 ..
drwx------  2 root root   16K Jun 15 16:22 lost+found
-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的此类文件都是可以的