在KVM中如何创建虚拟机
时间:2019-11-20 08:54:15 来源:igfitidea点击:
如何使用qcow2镜像文件创建虚拟机?
在KVM中如何创建虚拟机VM?
qcow是QEMU使用的磁盘文件格式。qcow是QEMU Copy On Write的首字母缩写。
该文件格式有两种版本:.qcow和.qcow2。
如何在Linux KVM中使用qcow2创建VM
在Linux上创建虚拟机的步骤:
- 下载映像 rhel-8.0-beta-1-x86_64-kvm.qcow2
- 创建元数据和用户数据文件
- 创建磁盘映像
- 导入qcow2
- 创建虚拟机
- 使用ssh命令登录到虚拟机
设置所需的目录和配置文件
# mkdir -vp /var/lib/libvirt/images/theitroad-rhel8
KVM VM的元数据文件
创建元数据文件,用于设置实例主机名和ID:
# cd /var/lib/libvirt/images/theitroad-rhel8 # vi meta-data
内容如下:
instance-id: theitroad-rhel8 local-hostname: theitroad-rhel8
KVM VM的用户数据文件
接下来,您需要根据设置修改预构建的云映像。例如,使用Cloud-init添加用户,ssh密钥,设置时区等。让我们看看如何创建具有2048MB内存,60GB磁盘空间,2个vCPU,用户名为Hyman和ssh键的RHEL 8(测试版)虚拟机。
创建一个新文件:
# cd /var/lib/libvirt/images/theitroad-rhel8 # vi user-data
#cloud-config # Customize as per your need. At least change username (Hyman) and ssh-ed22519 # key with your actual public key # Hostname management preserve_hostname: False hostname: theitroad-rhel8 fqdn: theitroad-rhel8.theitroad.com # Setup Users with ssh keys so that I can log in into new machine users: - default - name: Hyman groups: ['wheel'] shell: /bin/bash sudo: ALL=(ALL) NOPASSWD:ALL ssh-authorized-keys: - ssh-ed25519 AAAAD3NzaC1lZDI1NTE5AAAAIG+zTQJ5FBsD2dTPlPVvQUbMq3jt19HflOtAy1EPBmKw KVM VM Lab SSH Login key for user # Configure where output will go output: all: ">> /var/log/cloud-init.log" # configure interaction with ssh server ssh_genkeytypes: ['ed25519', 'rsa'] # Install your public ssh key to the first user-defined user configured # in cloud.cfg in the template (optional since I created Hyman) ssh_authorized_keys: - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG+zTQJ5FBsD2dTPlPVvQUbMq3jt19HflOtAy1EPBmKw KVM VM Lab SSH Login key for user # set timezone for VM timezone: Asia/Shanghai # Remove cloud-init runcmd: - systemctl stop network && systemctl start network - yum -y remove cloud-init
复制映像
# cp -v /var/lib/libvirt/boot/rhel-8.0-beta-1-x86_64-kvm.qcow2 /var/lib/libvirt/images/theitroad-rhel8/theitroad-rhel8.qcow2
创建60GB磁盘镜像文件
创建镜像文件用户保存虚拟机:
# cd /var/lib/libvirt/images/theitroad-rhel8 # export LIBGUESTFS_BACKEND=direct # qemu-img create -f qcow2 -o preallocation=metadata theitroad-rhel8.new.image 60G # ls -l
使用virt-resize命令来调整虚拟机磁盘的大小:
# virt-resize --quiet --expand /dev/sda1 theitroad-rhel8.qcow2 theitroad-rhel8.new.image # ls -l
使用新的镜像文件:
# mv -v theitroad-rhel8.new.image theitroad-rhel8.qcow2 # ls -l
创建一个云初始化ISO和存储池
使用我们的用户数据和元数据创建ISO9660文件:
# cd /var/lib/libvirt/images/theitroad-rhel8 # mkisofs -o theitroad-rhel8-cidata.iso -V cidata -J -r user-data meta-data
创建一个存储池:
# virsh pool-create-as --name theitroad-rhel8 --type dir --target /var/lib/libvirt/images/theitroad-rhel8 # ls -l
通过qcow2镜像安装RHEL 8
如何使用qcow2镜像安装RHEL 8?
# cd /var/lib/libvirt/images/theitroad-rhel8 # virt-install --import --name theitroad-rhel8 \ --memory 2048 --vcpus 2 --cpu host \ --disk theitroad-rhel8.qcow2,format=qcow2,bus=virtio \ --disk theitroad-rhel8-cidata.iso,device=cdrom \ --network bridge=virbr0,model=virtio \ --os-type=linux \ --os-variant=rhel7.5 \ --graphics spice \ --noautoconsole
删除不需要的文件:
# cd /var/lib/libvirt/images/theitroad-rhel8 # virsh change-media theitroad-rhel8 hda --eject --config # rm -vi meta-data user-data *.iso
如何查看KVM虚拟机通过DHCP获取的IP地址
# virsh net-dhcp-leases default # virsh net-dhcp-leases default | grep theitroad-rhel8 | awk '{ print }'