现象:
创建磁盘大小为40G,但实际磁盘virtual_size只有40G
[root@compute01 /]# cinder list | grep acfc7748-c459-463d-a269-374d4a983483
| acfc7748-c459-463d-a269-374d4a983483 | available | 12345 | 40 | __DEFAULT__ | true | |
[root@compute01 /]# qemu-img info /var/lib/nova/mnt/6ee3397902b681df883306df8b4f29e0/volume-acfc7748-c459-463d-a269-374d4a983483
image: /var/lib/nova/mnt/6ee3397902b681df883306df8b4f29e0/volume-acfc7748-c459-463d-a269-374d4a983483
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 1.4G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
原因:
openstack从镜像创建磁盘,是先创建空白的40G磁盘;然后通过文件读写的方式读写镜像文件写入创建好的空白盘,相当于拷贝了原镜像文件;所以
创建好磁盘后,大小只有原镜像大小
解决方法:
在数据拷贝完后,再对磁盘镜像扩容到目标大小
###修改代码:
原有代码:
def _copy_volume_from_snapshot(self, snapshot, volume, volume_size):
"""Copy data from snapshot to destination volume.
This is done with a qemu-img convert to raw/qcow2 from the snapshot
qcow2.
"""
LOG.debug("Copying snapshot: %(snap)s -> volume: %(vol)s, "
"volume_size: %(size)s GB",
{'snap': snapshot.id,
'vol': volume.id,
'size': volume_size})
info_path = self._local_path_volume_info(snapshot.volume)
snap_info = self._read_info_file(info_path)
vol_path = self._local_volume_dir(snapshot.volume)
forward_file = snap_info[snapshot.id]
forward_file = snap_info[snapshot.id]
forward_path = os.path.join(vol_path, forward_file)
# Find the file which backs this file, which represents the point
# when this snapshot was created.
img_info = self._qemu_img_info(forward_path, snapshot.volume.name)
path_to_snap_img = os.path.join(vol_path, img_info.backing_file)
path_to_new_vol = self._local_path_volume(volume)
LOG.debug("will copy from snapshot at %s", path_to_snap_img)
if self.configuration.nfs_qcow2_volumes:
out_format = 'qcow2'
else:
out_format = 'raw'
image_utils.convert_image(path_to_snap_img,
path_to_new_vol,
out_format,
run_as_root=self._execute_as_root)
self._set_rw_permissions_for_all(path_to_new_vol)
更改后的代码:
def _copy_volume_from_snapshot(self, snapshot, volume, volume_size):
"""Copy data from snapshot to destination volume.
This is done with a qemu-img convert to raw/qcow2 from the snapshot
qcow2.
"""
LOG.debug("Copying snapshot: %(snap)s -> volume: %(vol)s, "
"volume_size: %(size)s GB",
{'snap': snapshot.id,
'vol': volume.id,
'size': volume_size})
info_path = self._local_path_volume_info(snapshot.volume)
snap_info = self._read_info_file(info_path)
vol_path = self._local_volume_dir(snapshot.volume)
forward_file = snap_info[snapshot.id]
forward_file = snap_info[snapshot.id]
forward_path = os.path.join(vol_path, forward_file)
# Find the file which backs this file, which represents the point
# when this snapshot was created.
img_info = self._qemu_img_info(forward_path, snapshot.volume.name)
path_to_snap_img = os.path.join(vol_path, img_info.backing_file)
path_to_new_vol = self._local_path_volume(volume)
LOG.debug("will copy from snapshot at %s", path_to_snap_img)
if self.configuration.nfs_qcow2_volumes:
out_format = 'qcow2'
else:
out_format = 'raw'
image_utils.convert_image(path_to_snap_img,
path_to_new_vol,
out_format,
run_as_root=self._execute_as_root)
##添加判断,如果目标磁盘大于镜像,则进行扩容#####
if volume_size >image_utils.qemu_img_info(path_to_new_vol).virtual_size/units.Gi:
self.extend_volume(volume, volume_size)
self._set_rw_permissions_for_all(path_to_new_vol)