OS
Summary: Author: 张亚飞 | Read Time: 5 minute read | Published: 2016-05-17
Filed under
—
Categories:
Linux
—
Tags:
Note,
制作 CentOS-8
系统镜像
CentOS7安装KVM虚拟机详解 Howto use centos cloud images with cloud-init on KVM/libvirtd KVM: Testing cloud-init locally using KVM for a CentOS cloud image Linux 系统安装 cloud-init
ssh-keygen -t rsa
sudo dnf -y install genisoimage virt-install libguestfs-tools-c qemu-img libvirt-client wget libosinfo
sudo dnf -y install libvirt-nss
qemu-img create -f qcow2 centos-6.9.qcow2 100G
virt-install --virt-type hvm --name centos-6.9 --ram 1024 --disk centos-6.9.qcow2,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --location=CentOS-6.9-x86_64-netinstall.iso
- 自动安装包
git clone https://github.com/giovtorres/kvm-install-vm.git
cd kvm-install-vm
./kvm-install-vm create -v foo
- 检查系统硬件是否支持虚拟化
# grep -E '(vmx|svm)' /proc/cpuinfo
# dmesg |grep kvm
[ 0.000000] kvm-clock: cpu 0, msr 2:3ff88001, primary cpu clock
[ 0.000000] kvm-clock: Using msrs 4b564d01 and 4b564d00
[ 0.000000] kvm-clock: using sched offset of 2170906062 cycles
[ 0.000000] kvm-stealtime: cpu 0, msr 23fc1c040
[ 0.219340] kvm-clock: cpu 1, msr 2:3ff88041, secondary cpu clock
[ 0.220665] kvm-stealtime: cpu 1, msr 23fd1c040
[ 0.314982] Switched to clocksource kvm-clock
[ 0.728584] systemd[1]: Detected virtualization kvm.
安装依赖
sudo yum install -y qemu-kvm
sudo yum install -y qemu-kvm-tools
sudo yum install -y virt-manager
sudo yum install -y genisoimage
sudo yum install -y libvirt
sudo yum install -y virt-install
启动libvirtd
系统服务
systemctl start libvirtd
systemctl status libvirtd
systemctl enable libvirtd
libvirt
在安装完成后,默认已经创建了一个NAT
类型的网络,并且已经设置好了NAT
的一系列规则,DHCP
等,这个网络会给宿主机创建一张virbr0
网卡,用于宿主机与虚拟机互通。 启动libvirt
,此时ifcofnig
,查看多了一块virbr0
的网卡,ip
为192.168.122.1
,virsh
命令可以用
# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:c9:39:03:f1 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.200.0.15 netmask 255.255.240.0 broadcast 10.200.15.255
inet6 fe80::5054:ff:fe88:65b1 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:88:65:b1 txqueuelen 1000 (Ethernet)
RX packets 178198 bytes 52034768 (49.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 450892 bytes 4692459992 (4.3 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:59:bf:89 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# virsh net-list
名称 状态 自动开始 持久
----------------------------------------------------------
default 活动 是 是
/var/lib/libvirt/boot
# wget http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
# qemu-img info CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
image: CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
mkdir -p /var/lib/libvirt/images/tst
cp -v /var/lib/libvirt/boot/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2 /var/lib/libvirt/images/tst/tst.qcow2
/var/lib/libvirt/images/tst
# qemu-img resize tst.qcow2 18G
# qemu-img info tst.qcow2
image: tst.qcow2
file format: qcow2
virtual size: 18G (19327352832 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
- Install cloud-utilsPermalink
yum install -y cloud-utils
- 创建密码加密
hash
python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))'
/var/lib/libvirt/images/tst/config.yaml
#cloud-config
package_upgrade: true
users:
- name: coam
groups: wheel
lock_passwd: false
passwd: $6$T8K0cqEJB4Zrwsiy$qExXuAXxdCnhq11gabMKst/v9ph6Fo4rNbh50TDv55L6wSBpXBpAecSWcibsxUz7T.6HY1a0d8QdSTifhdg00/
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJ+Vt+W41nZsziKkwH550HLACST+osX4kWEy0+UdGwBaSJMkKbiFZZwff12E0ceRWMp30MvZw4SDPak7lw7ZvY/VWHNZHfMpHtgciZ8h+KFusgu2IJw/h16f+4V1q5u8Lv7GX7tCrgSu5SpWwf4rQYUVtjqPLBvSEvCO6AEUUHC3fvXmTp4I3dGUmcyUBGVDSvGqW5IE88u8Ioll9Izd0kMjc6u125wQjQQHXHzBAO6r53h2K7cFOS/z1McdniugmK/IUawROSv76hh2zccI323myui5SamSOEb6Luv7zOx7LUvS5GEb7/9PXy7Q0c+zi5L/DNTsE5apXx/wx7ei3Dcoam@MacPro.local
- 编译
config.iso
配置
# cloud-localds config.iso config.yaml
wrote config.iso with filesystem=iso9660 and diskformat=raw
- 使用
osinfo-query
查询系统版本
# osinfo-query os | grep -i centos8
centos8 | CentOS 8 | 8 | http://centos.org/centos/8
- 启动虚拟机
virt-install \
--memory 4096 \
--vcpus 2 \
--name tst \
--disk /var/lib/libvirt/images/tst/tst.qcow2,device=disk \
--disk /var/lib/libvirt/images/coam/config.iso,device=cdrom \
--os-type Linux \
--os-variant centos8 \
--virt-type kvm \
--graphics none \
--network default \
--import
如果报错: ERROR Host does not support domain type kvm for virtualization type 'hvm' arch 'x86_64'
则去掉参数 --virt-type kvm
重新编译
virt-install \
--memory 4096 \
--vcpus 2 \
--name tst \
--disk /var/lib/libvirt/images/tst/tst.qcow2,device=disk \
--disk /var/lib/libvirt/images/coam/config.iso,device=cdrom \
--os-type Linux \
--os-variant centos8 \
--graphics none \
--network default \
--import
查看虚拟机
# virsh list --all
Id 名称 状态
----------------------------------------------------
1 tst running
使用 CentOS-8 安装后 cockpit
无法登录
使用终端安装系统
wget http://mirrors.aliyun.com/centos/7.8.2003/isos/x86_64/CentOS-7-x86_64-Minimal-2003.iso
virt-install \
--memory 2048 \
--vcpus 2 \
--name coam \
--location=/root/CentOS-7-x86_64-Minimal-2003.iso \
--disk /var/lib/libvirt/images/coam/coam.qcow2,device=disk \
--disk /var/lib/libvirt/images/coam/config.iso,device=cdrom \
--os-type Linux \
--os-variant centos7.0 \
--graphics none \
--console=pty,target_type=serial \
--extra-args="console=tty0 console=ttyS0" \
--network default
virt-install \
--name=tos --ram 1024 --vcpus=1 \
--disk path=/root/vms/tos/tos.qcow2,size=10,format=qcow2,bus=virtio \
--location=/root/CentOS-7-x86_64-Minimal-2003.iso --network bridge=virbr0,model=virtio \
--graphics=none \
--console=pty,target_type=serial \
--extra-args="console=tty0 console=ttyS0"
如果提示错误:
ERROR Cannot access storage file '/root/coam/vms/CentOS-8-GenericCloud-8.2.2004-20200611.2.x86_64.qcow2' (as uid:107, gid:107): Permission denied
则修改配置
/etc/libvirt/qemu.conf
user = "root"
group = "root"
查看正在运行的虚拟机
# virsh list
Id Name State
----------------------------------------------------
1 tst running
查看所有虚拟机
# virsh list --all
Id Name State
----------------------------------------------------
2 tos running
- coam shut off
- nocs shut off
- sarah shut off
- tst shut off
启动 coam
虚拟机
virsh start coam
设置 coam
自启动
virsh autostart coam
其它操作
virsh autostart --disable coam #关闭自启动
virsh shutdown coam #关闭虚拟机
virsh shutdown name #正常关闭KVM虚拟机
virsh destroy name #强制关闭KVM虚拟机(类似于直接断电)
virsh suspend name #挂起KVM虚拟机
virsh resume name #恢复挂起的KVM虚拟机
virsh dominfo coam #显示虚拟机的基本信息
virsh dumpxml name #查看KVM虚拟机配置文件,可以把输出的内容定义到xml里,用来克隆迁移用。
virsh edit name #编辑KVM虚拟机的xml配置文件
virsh define /etc/libvirt/qemu/name.xml #定义注册虚拟机,需要先查看xml文件对应的镜像,img等路径是否存在或修改指定路径
virsh undefine name #彻底删除KVM虚拟机,不可逆,如果想找回来,需要备份/etc/libvirt/qemu的xml文件
KVM–Host does not support any virtualization… Host does not support domain type kvm for virtualization type ‘hvm’ arch ‘x86…
手动创建镜像需要确保libvirt
运行有default
网络,这个网络可以给虚拟机提供上网服务。
查看当前是否启用default
网络
[root@compute ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
注: 如果没有启用,使用以下命令启用default
virsh net-start default
安装 cloud-init
服务
安装依赖包
yum update
yum install -y python-pip
- 在
CentOS-8
上
sudo dnf install python3
下载源码包
wget https://launchpad.net/cloud-init/trunk/17.1/+download/cloud-init-17.1.tar.gz
tar -zxvf cloud-init-17.1.tar.gz
安装项目依赖
cloud-init-17.1
pip3 install -r requirements.txt
安装依赖包
yum install cloud-utils-growpart -y
执行命令安装
python3 setup.py build
python3 setup.py install --init-system systemd
常见错误
- 将插槽连接到
/var/run/libvirt/libvirt-sock
失败: 没有那个文件或目录
systemctl start libvirtd
- cannot access storage file (as uid:107, gid:107)permission denied
Changing
/etc/libvirt/qemu.conf
make working things. Uncomment user/group to work as root.
user = "root"
group = "root"
[](https://blog.csdn.net/hobertony_7/article/details/49948807)
参考
CentOS 7 virt-install 命令行方式(非图形界面)安装KVM虚拟机 CentOS 7 使用 virt-install + vnc 图形界面 创建虚拟机 深入理解OpenStack-手动制作qcow2镜像
Comments