随笔 - 120  文章 - 0  评论 - 35  阅读 - 85万

centos7安装kvm环境采用网桥模式并创建虚拟机制作openstack需要的镜像

初始环境的安装:centos7 mini iso镜像进行安装的系统

采用的环境是vm该软件,联网方式NAT模式下配置的静态ip(如何在NAT模式下配置静态ip参考之前的文章)

1、由于要安装kvm环境,首先确认是否开启了虚拟机

将图中的地方打上勾

2、安装kvm环境这里采用使用vnc连接的方式进行创建虚拟机,所以这里采用安装桌面环境,并安装vnc

1
2
# yum groupinstall "Desktop" "GNOME Desktop"   安装桌面环境
# yum install tigervnc

 3、centos7默认启动级别是3,所以这里需要进行修改到5

1
2
3
4
设置centos7启动级别:5
systemctl set-default graphical.target
 
# reboot  查看是否完成桌面环境

 中间图形安装这里省略,结果为:

4、将网络配置为网桥模式:

1
2
# cd /etc/sysconfig/network-scripts
# cp ifcfg-ens33 ifcfg-br0

 两个配置文件的设置如下:

1
2
3
4
5
6
7
8
9
[root@kvm network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=none
UUID=3a9cfe3f-505f-4d4c-8fc0-fc86f2b218f3
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.101.12
GATEWAY=192.168.101.2
NETMASK=255.255.255.0

 ens33这个配置文件如下:

1
2
3
4
5
6
7
8
[root@kvm network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
UUID=3a9cfe3f-505f-4d4c-8fc0-fc86f2b218f3
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0

 配置完成后,重启网络服务:

1
# systemctl restart network

 

将网桥名为br0开启:

1
2
3
4
5
[root@kvm network-scripts]# brctl stp br0 on
[root@kvm network-scripts]# brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.000c290ed6cf   yes     ens33
virbr0      8000.5254009c1553   yes     virbr0-nic

 确保网络没有问题:

1
2
[root@kvm network-scripts]# cat /etc/resolv.conf
nameserver 192.168.101.2

接下来开启配置kvm需要的环境:

1
# yum install qemu-kvm libvirt virt-install bridge-utils virt-manager libguestfs-tools-c qemu-img

 1、创建磁盘镜像文件:

1
# qemu-img create -f qcow2 /tmp/centos.qcow2 10G

 然后将需要的镜像文件上传到tmp目录下(CentOS-7-x86_64-Minimal-1611.iso)

1
2
3
4
5
[root@kvm ~]# ll /tmp/
total 697452
-rw-r--r--  1 root    root         1148 Oct 23 15:52 anaconda.log
-rw-r--r--  1 root    root    713031680 Jul  3 22:27 CentOS-7-x86_64-Minimal-1611.iso
-rw-r--r--  1 root    root       197120 Oct 25 13:46 centos.qcow2

2、使用命令创建虚拟机:

1
# virt-install --virt-type kvm --name centos --ram 1024 --disk /tmp/centos.qcow2,format=qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-Minimal-1611.iso

 这里--network bridge=br0表示这里采用刚刚创建的网桥

1
2
3
4
5
6
7
8
[root@kvm ~]# virt-install --virt-type kvm --name centos --ram 1024 --disk /tmp/centos.qcow2,format=qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-Minimal-1611.iso
 
Starting install...
Retrieving file .treeinfo...                                                                                      |  366 B  00:00:00    
Retrieving file vmlinuz...                                                                                        | 5.1 MB  00:00:00    
Retrieving file initrd.img...                                                                                     |  41 MB  00:00:00    
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

 然后移动到桌面环境进行安装

首先查看vnc的5900端口是否开启:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@kvm ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      24681/qemu-kvm     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd          
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2189/dnsmasq       
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1337/sshd          
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1000/cupsd         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd          
tcp6       0      0 :::22                   :::*                    LISTEN      1337/sshd          
tcp6       0      0 ::1:631                 :::*                    LISTEN      1000/cupsd         
udp        0      0 192.168.122.1:53        0.0.0.0:*                           2189/dnsmasq       
udp        0      0 0.0.0.0:67              0.0.0.0:*                           2189/dnsmasq       
udp        0      0 127.0.0.1:323           0.0.0.0:*                           705/chronyd        
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           671/avahi-daemon: r
udp        0      0 0.0.0.0:42824           0.0.0.0:*                           671/avahi-daemon: r
udp6       0      0 ::1:323                 :::*                                705/chronyd        

 可以看见5900端口已经开启了,于是使用vnc进行连接到5900端口,进行安装:

1
# vncviewer :5900

 执行该命令后会弹出图形安装界面:

然后进行一步一步安装就行

查看虚拟机状态:

1
2
3
4
[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos                         shut off

 将虚拟机centos开启:

1
2
3
4
5
6
7
[root@kvm ~]# virsh start centos
Domain centos started
 
[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos                         running

 使用vncview :5900进行连接到虚拟机:

然后进行登录到虚拟机,设置虚拟机的网络:

重启网络:

1
# systemctl restart network

 

然后使用ssh连接工具连接到虚拟机192.168.101.13

至此虚拟机安装完成,现在需要将此虚拟机做成镜像

kvm主机执行:

1
[root@kvm ~]# virsh dumpxml centos

 找出使用的磁盘名称

弹出虚拟cd,关机并重启
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@kvm ~]# virsh attach-disk --type cdrom --mode readonly centos "" hda         
Disk attached successfully
 
[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos                         running
 
[root@kvm ~]# virsh destroy centos
Domain centos destroyed
 
[root@kvm ~]# virsh list --all   
 Id    Name                           State
----------------------------------------------------
 -     centos                         shut off
 
[root@kvm ~]# virsh start centos
Domain centos started
 
[root@kvm ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 3     centos                         running

 在面的操作在虚拟机中操作:

1、To enable the hypervisor to reboot or shutdown an instance, you must install and run the acpid service on the guest system.

1
2
# yum install acpid
# systemctl enable acpid

 2、配置去获取matadata:

1
# yum install cloud-init

 For example, to configure cloud-init to put the key in an account named admin, use the following syntax in the configuration file:

修改/etc/cloud/cloud.cfg
配置文件最后添加:

users:
- name: admin

3、安装cloud-utils-growpart以允许分区调整大小

1
# yum install cloud-utils-growpart

 4、Disable the zeroconf route

1
# echo "NOZEROCONF=yes" >> /etc/sysconfig/network

 

5、Configure console

修改配置文件/etc/default/grub,将GRUB_CMDLINE_LINUX的值改为如下:

1
2
3
4
5
6
7
8
[root@localhost ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap console=tty0 console=ttyS0,115200n8"
GRUB_DISABLE_RECOVERY="true"

 Run the following command to save the changes:

1
# grub2-mkconfig -o /boot/grub2/grub.cfg

 

Shut down the instance

1
# poweroff

 

至此在虚拟机上面的操作就已经完成了,接下来还有几个步骤在kvm主机上执行

Clean up (remove MAC address details)

1
# virt-sysprep -d centos

 

清除mac地址的这一步骤总是报错,不知啥原因,虚拟机状态为:

1
2
3
4
[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos                         shut off

 将mac地址如果成功清除后,执行最后一步将虚拟机取消定义:

1
# virsh undefine centos

 最后/tmp/centos.qcow2这个镜像文件就可以作为镜像传给openstack使用了

参考文档:https://docs.openstack.org/image-guide/centos-image.html

posted on   wadeson  阅读(1126)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示