Tag - qcow2

? qcow2 ? ? 分区 ?    2020-08-13 15:04:24    1110    1    0

官方参考文档

diskimage-builder code
https://opendev.org/openstack/diskimage-builder

官方分区设置

https://docs.openstack.org/diskimage-builder/latest/user_guide/building_an_image.html

If you wish to customise the top-level block-device-default.yaml file
from one of the block-device-* elements, set the environment variable
DIB_BLOCK_DEVICE_CONFIG. This variable must hold YAML structured
configuration data or be a file:// URL reference to a on-disk
configuration file.


在自己自定义的element 的根目录下,创建一个文件block-device-default.yaml,将分区方案按yaml 文件格式配置其中,如果element 下方有一个block-device-default.yaml,则会优先使用用户定义的分区配置,如果用户无定制,则使用默认配置:
https://opendev.org/openstack/diskimage-builder/src/branch/master/diskimage_builder/elements/block-device-mbr/block-device-default.yaml

或使用变量DIB_BLOCK_DEVICE_CONFIG定义分区配置,将block-device-default.yaml的文本赋值给DIB_BLOCK_DEVICE_CONFIG

https://github.com/GuaKing/qcow2build.git

官方的一个默认配置:

磁盘全部容量分配r

? qcow2 ? ? 分区 ?    2020-08-13 13:39:52    1050    1    0

ubuntu 扩展磁盘后如何扩展分区

背景: 使用kvm 安装 qcow2 文件 ,启用一个ubuntu 虚拟机。

直接扩展qcow2 镜像文件 的磁盘大小,并指定扩展在分区N

  1. qemu-img info a.qcow2
  2. qemu-img create -f qcow2 -o preallocation=metadata b.qcow2 60G
  3. virt-resize --expand /dev/sda1 a.qcow2 b.qcow2
  4. #第一个命令查看a.qcow2 镜像磁盘有多大(a 包含我们要安装系统镜像)
  5. #第二个命令创建一块 60G 的镜像磁盘文件(即最终磁盘容量,b须比a大)
  6. #第三个命令,将a 镜像内容拷到b镜像中,并将空闲空间(b'size-a'size)扩展到/dev/sda1 分区上。
  7. #即,如果a.qcow2 原磁盘大小是50G ,执行完命令后b.qcow2 除了是a.qcow2 的完整拷贝外,b.qcow2中的/deb/sda磁盘大小为60G,其中/dev/sda1 分区 比a.qcow2 中/dev/sda1 多了10G.

扩展磁盘,不分配到指定分区,进入系统后再分配

  1. qemu-img resize a.qcow2 +5G
  2. virsh destroy vm
  3. virsh start vm
  4. ## enter vm
  5. fdisk -l | grep sda (/dev/sda 磁盘大小增加了5g, 但是分区 /dev/sda1 未拿到这些空间)
  6. parted /dev/sda (将磁盘的未分配空间,分配给分区sda1
  7. (parted) p
  8. (parted) resizepart 1
  9. (parted) quit
  10. ## 执行log 如下:
  11. root@CASA-MOBILE:~# parted /dev/sda
  12. (parted) p <== remember space of all disk /dev/sda ,and remember the Number of sda1
? qcow2 ?    2020-03-11 09:43:31    680    0    0

https://cloudinit.readthedocs.io/en/latest/topics/modules.htm

https://cloudinit.readthedocs.io/en/latest/topics/examples.html

 

安装工具:

apt-get install cloud-init​

 

使用cloud-localds命令生成一个seed.img , 然后在virt-install qcow2 image 的时候同时指定这个seed.img

 

即 把初始化镜像时的自定义修改放到seed.img中, 然后将它作为一个参数传给kvm 。

 

cloud-localds seed.img user-data meta-data​

 

这个命令是指user-data meta-data 中的配置加载到seed.img ,并生成seed.img 。 所以首先我们要编辑user-data meta-data 。格式与yaml相同。用法参见https://cloudinit.readthedocs.io/en/latest/topics/examples.html

 

注意,write file 是生成新文件,会覆盖。如要修改已有文件的个别部分,应该修改后的全文贴入对应部分

生成seed.img 之后,virt-install 时加上--disk path=/vob/cloud-init/seed.img,bus=virtio,format=raw

qemu-img resize xxxx.qcow2 +30G
virt-install -d --connect qemu:///system --name casa-mobile-a -r 61440 --vcpus 4 --cpu host  \
--disk path=/vob/cloud-init/xxxxxxx.qcow2,format=qcow2,bus=virtio --import   \
--disk path=/vob/cloud-init/seed.img,bus=virtio,format=raw    \
--autostart --noautoconsole​

 

user-data 的一个示例,必须是以#cloud-config 开头

还可用于修改root 密码之类的变更,用passwd 。