正文
一、准备 debian 镜像
将下载好的镜像放入 /data
目录(没有的话自行创建)
二、安装虚拟化需要的组件
yum -y install qemu-kvm qemu-img kvm qemu-kvm-tools libvirt bridge-utils virt-*
三、启动libvirtd
systemctl enable libvirtd systemctl start libvirtd echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf sysctl -p
四、创建 qcow2 镜像文件
[root@qijing0 data]# qemu-img create -f qcow2 /data/debian-11.4.0.qcow2 100G Formatting '/data/debian-11.4.0.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=107374182400 lazy_refcounts=off refcount_bits=16 [root@qijing0 data]# ls debian-11.4.0-amd64-netinst.iso debian-11.4.0.qcow2
五、创建虚机
[root@qijing0 data]# virt-install --name debian_qijing --ram 4096 --vcpus 2 --os-type linux --os-variant rhel7 --arch=x86_64 --network network=default,model=virtio --disk path=/data/debian-11.4.0.qcow2,format=qcow2 --location /data/debian-11.4.0-amd64-netinst.iso --console pty,target_type=serial --graphics vnc,listen=0.0.0.0,port=6666 WARNING Graphics requested but DISPLAY is not set. Not running virt-viewer. WARNING No console to launch for the guest, defaulting to --wait -1 Starting install... Retrieving file .treeinfo... | 0 B 00:00:00 Retrieving file content... | 0 B 00:00:00 Retrieving file info... | 74 B 00:00:00 Retrieving file vmlinuz... | 6.5 MB 00:00:00 Retrieving file initrd.gz... | 17 MB 00:00:00 Domain installation still in progress. Waiting for installation to complete.
六、使用 VNCView 连接
七、装系统
八、关机
shutdown now
九、清理机器配置信息
[root@qijing0 ~]# virt-sysprep -d debian_qijing [ 0.0] Examining the guest ... [ 28.8] Performing "abrt-data" ... [ 28.8] Performing "backup-files" ... [ 29.0] Performing "bash-history" ... [ 29.0] Performing "blkid-tab" ... [ 29.0] Performing "crash-data" ... [ 29.0] Performing "cron-spool" ... [ 29.1] Performing "dhcp-client-state" ... [ 29.1] Performing "dhcp-server-state" ... [ 29.1] Performing "dovecot-data" ... [ 29.1] Performing "logfiles" ... [ 29.1] Performing "machine-id" ... [ 29.1] Performing "mail-spool" ... [ 29.1] Performing "net-hostname" ... [ 29.1] Performing "net-hwaddr" ... [ 29.1] Performing "pacct-log" ... [ 29.1] Performing "package-manager-cache" ... [ 29.2] Performing "pam-data" ... [ 29.2] Performing "passwd-backups" ... [ 29.2] Performing "puppet-data-log" ... [ 29.2] Performing "rh-subscription-manager" ... [ 29.2] Performing "rhn-systemid" ... [ 29.2] Performing "rpm-db" ... [ 29.2] Performing "samba-db-log" ... [ 29.2] Performing "script" ... [ 29.2] Performing "smolt-uuid" ... [ 29.2] Performing "ssh-hostkeys" ... [ 29.2] Performing "ssh-userdir" ... [ 29.2] Performing "sssd-db-log" ... [ 29.2] Performing "tmp-files" ... [ 29.2] Performing "udev-persistent-net" ... [ 29.2] Performing "utmp" ... [ 29.2] Performing "yum-uuid" ... [ 29.2] Performing "customize" ... [ 29.2] Setting a random seed [ 29.3] Setting the machine ID in /etc/machine-id [ 29.3] Performing "lvm-uuids" ...
十、压缩镜像
[root@qemu-again data]# virt-sparsify --compress /data/debian-11.4.0.qcow2 /data/debian-11.4.0-bare.qcow2 [ 0.1] Create overlay file in /tmp to protect source disk [ 0.2] Examine source disk [ 3.9] Fill free space in /dev/sda1 with zero 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00 [1124.9] Clearing Linux swap on /dev/sda5 [1137.2] Copy to destination and make sparse [2196.9] Sparsify operation completed with no errors. virt-sparsify: Before deleting the old disk, carefully check that the target disk boots and works correctly. [root@qemu-again data]# [root@qemu-again data]# [root@qemu-again data]# [root@qemu-again data]# date Thu Aug 25 08:25:26 UTC 2022
可以看到,压缩之后的 qcow2 镜像已经生成了。(我这里的镜像压缩完之后还是比较大,因为里面装了 git,还有一个比较大的数据库软件)
十一【附加】、上传镜像到 openstack(使用kolla-ansible 部署的)
source /etc/kolla/admin-openrc.sh
root@OpenStack:~/workspace_openstack# openstack image create --disk-format qcow2 --file debian-11.4.0-bare.qcow2 --progress debian-11.4.0-bare --public /usr/lib/python3/dist-packages/secretstorage/dhcrypto.py:15: CryptographyDeprecationWarning: int_from_bytes is deprecated, use int.from_bytes instead from cryptography.utils import int_from_bytes /usr/lib/python3/dist-packages/secretstorage/util.py:19: CryptographyDeprecationWarning: int_from_bytes is deprecated, use int.from_bytes instead from cryptography.utils import int_from_bytes [=============================>] 100% +------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+ | Field | Value | +------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+ | container_format | bare | | created_at | 2022-08-25T08:31:28Z | | disk_format | qcow2 | | file | /v2/images/38ab0af9-6592-435f-99e1-bfa91e8ceae6/file | | id | 38ab0af9-6592-435f-99e1-bfa91e8ceae6 | | min_disk | 0 | | min_ram | 0 | | name | debian-11.4.0-bare | | owner | eee0422e34e248959f2333b1bd600935 | | properties | os_hidden='False', owner_specified.openstack.md5='', owner_specified.openstack.object='images/debian-11.4.0-bare', owner_specified.openstack.sha256='' | | protected | False | | schema | /v2/schemas/image | | status | queued | | tags | | | updated_at | 2022-08-25T08:31:28Z | | visibility | public | +------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------+ root@OpenStack:~/workspace_openstack#