CentOS 7 使用 virt-install + vnc 图形界面/非图形界面 创建虚拟机
一. 使用 virt-install + vnc 图形界面 创建虚拟机
环境
操作系统:CentOS 7.0
IP地址:192.168.0.102
1、安装qemu-kvm,libvirt
# qemu-kvm创建虚拟机硬盘,libvirt管理虚拟机
yum install -y qemu-kvm libvirt
2、安装virt-install
# 用来创建虚拟机
yum install -y virt-install
3、启动 librvirt
libvirt在安装完成后,默认已经创建了一个NAT类型的网络,并且已经设置好了NAT的一系列规则,DHCP等,这个网络会给宿主机创建一张virbr0网卡,用于宿主机与虚拟机互通。
# 启动libvirt,此时ifcofnig,查看多了一块virbr0的网卡,ip为 192.168.122.1,virsh命令可以用
systemctl start libvirtd
# 设为开机启动
systemctl enable libvirtd
4、网络配置
# 配置桥接网卡
cd /etc/sysconfig/network-scripts/
vi ifcfg-br0
# 配置内容
DEVICE=br0 # 桥接网卡名称
TYPE=Bridge # 类型桥接
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.0.102 # 此处的IP为本地的物理IP,通过ifconfig查看
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=8.8.8.8
配置物理网卡
vi ifcfg-ens33
# 配置如容
DEVICE=ens33
TYPE=Ethernet
ONBOOT=yes
BRIDGE=br0 # 设置网络桥接的名称
NM_CONTROLLED=yes
重启网络
systemctl restart network
查看网络
[root@centos7 network-scripts]# ifconfig
# 结果
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.102 netmask 255.255.255.0 broadcast 192.168.0.255……
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:47:ec:83 txqueuelen 1000 (Ethernet)……
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>……
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:ab:d0:6c txqueuelen 1000 (Ethernet)……
# 查看网络
brctl show
# 显示如下
bridge name bridge id STP enabled interfaces
br0 8000.000c2947ec83 no ens33
virbr0 8000.525400abd06c yes virbr0-nic
# 到此,先ping下百度,如果通,表示配置正确
报错:
cat /var/log/messages | grep "libssl"
# libssl错误
version libssl.so.10 not defined in file libssl.so.10 with link time reference
# 升级
yum install -y openssl-1.0.2k*
5、安装虚拟机
安装方式:在服务器上只完成虚拟机的配置信息和镜像的引导,系统的安装通过VNC远程登录系统进行
virt-install \
--name=kvm001 --ram 1024 --vcpus=1 \
--disk path=/home/raw/kvm001.raw,size=10,format=raw,bus=virtio \
--cdrom=/mnt/CentOS-7-x86_64-Minimal-1810.iso --network bridge=br0,model=virtio \
--graphics vnc,listen=0.0.0.0 --noautoconsole
--name #虚拟机名称
--ram #分配给虚拟机的内存,单位MB
--vcpus #分配给虚拟机的cpu个数
--cdrom #指定CentOS镜像ISO文件路径
--disk #指定虚拟机raw文件路径
size #虚拟机文件大小,单位GB
bus #虚拟机磁盘使用的总线类型,为了使虚拟机达到好的性能,这里使用virtio
cache #虚拟机磁盘的cache类型
--network bridge #指定桥接网卡
model #网卡模式,这里也是使用性能更好的virtio
--graphics #图形参数
打开 5900 端口
vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5900:5902 -j ACCEPT #打开VNC远程端口
在window下载TightVNC Viewer,进行远程安装
6、启动虚拟机
# 列出所有虚拟机
virsh list --all
Id 名称 状态
----------------------------------------------------
- kvm001 关闭
# 启动kvm001虚拟机
virsh start kvm001
# 设置自启动
virsh autostart kvm001
# 其它操作
----------------------------------------------------
virsh autostart --disable kvm001 # 关闭自启动
virsh shutdown kvm001 # 关闭虚拟机
virsh dominfo kvm001 # 显示虚拟机的基本信息
virsh dumpxml kvm001 # 显示虚拟机的配置文件
7、如果安装时没记住IP,可以通过以下方法找到,然后再配置静态IP
virsh dumpxml kvm002 | grep mac
# 结果
<partition>/machine</partition>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<mac address='52:54:00:89:67:66'/> #记录mac地址
arp -a | grep 52:54:00:89:67:66 #查询IP
# 结果
? (192.168.0.100) at 52:54:00:89:67:66 [ether] on br0 #找到IP
# 找不到的话,执行以下,全局扫描IP段
yum install -y namp
nmap -sP 192.168.0.1/24
8、修改虚拟机名称
# 1、关机
virsh shutdown kvm001
# 2、把名称改为kvm100
virsh domrename kvm001 kvm100
# 3、修改磁盘文件名称
mv /home/raw/kvm001.raw /home/raw/kvm100.raw
# 4、修改配置文件
virsh edit kvm100
# 修改内容
<source file='/home/raw/kvm100.raw'/>
# 5、开机
virsh start kvm100
9、删除虚拟机
# 强制关闭虚拟机
virsh destroy kvm001
# 删除定义虚拟机
virsh undefine kvm001
# 查找虚拟机文件
find / -name kvm002*
/var/log/libvirt/qemu/kvm002.log
/home/raw/kvm002.raw
# 删除
find / -name kvm002* | xargs rm -rf
二. CentOS 7 Virt-Manager 创建虚拟机
1、安装图形桌面环境
yum groups install 'GNOME Desktop' -y
2、Xshell 下需要安装 Xmanager 才能启动 virt-manager
virt-manager
3、Xmanager 关于输入字母重复的解决方法
打开 Xconfig -> Default profile -> 属性,去掉 XKEYBOARD 选项卡
三. CentOS 7 virt-install 命令行方式(非图形界面)安装KVM虚拟机
环境及网卡配置请参考
一. CentOS 7 使用 virt-install + vnc 图形界面 创建虚拟机
创建镜像目录
mkdir -p /home/vms
virt-install 配置文件
virt-install \
--name=kvm107 --ram 1024 --vcpus=1 \
--disk path=/home/vms/kvm107.qcow2,size=10,format=qcow2,bus=virtio \
--location=/mnt/CentOS-7-x86_64-Minimal-1810.iso --network bridge=br0,model=virtio \
--graphics=none --console=pty,target_type=serial \
--extra-args="console=tty0 console=ttyS0"
-
–graphics 指定是否开启图形
-
–console 定义终端的属性,target_type 则是定义终端的类型
-
–extra-args 定义终端额外的参数
开始安装
Starting installer, one moment...
anaconda 21.48.22.93-1 for CentOS Linux 7 started.
* installation log files are stored in /tmp during the installation
* shell is available on TTY2
* when reporting a bug add logs from /tmp as separate text/plain attachments
09:40:52 Not asking for VNC because we don't have a network
================================================================================
================================================================================
Installation
1) [x] Language settings 2) [!] Time settings
(English (United States)) (Timezone is not set.)
3) [!] Installation source 4) [!] Software selection
(Processing...) (Processing...)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: 输入 1 然后回车
[anaconda] 1:main* 2:shell 3:log 4:storage-lo> Switch tab: Alt+Tab | Help: F1
这里选择 1 进入语言设置界面
Available languages
1) Afrikaans 25) Hindi 48) Oriya
2) Amharic 26) Croatian 49) Punjabi
3) Arabic 27) Hungarian 50) Polish
4) Assamese 28) Interlingua 51) Portuguese
5) Asturian 29) Indonesian 52) Romanian
6) Belarusian 30) Icelandic 53) Russian
7) Bulgarian 31) Italian 54) Sinhala
8) Bengali 32) Japanese 55) Slovak
9) Bosnian 33) Georgian 56) Slovenian
10) Catalan 34) Kazakh 57) Albanian
11) Czech 35) Kannada 58) Serbian
12) Welsh 36) Korean 59) Swedish
13) Danish 37) Lithuanian 60) Tamil
14) German 38) Latvian 61) Telugu
15) Greek 39) Maithili 62) Tajik
16) English 40) Macedonian 63) Thai
17) Spanish 41) Malayalam 64) Turkish
18) Estonian 42) Marathi 65) Ukrainian
19) Basque 43) Malay 66) Urdu
20) Persian 44) Norwegian Bokmål 67) Vietnamese
21) Finnish 45) Nepali 68) Chinese
22) French 46) Dutch 69) Zulu
Press ENTER to continue
这里回车后 选择 68 选择 Chinese 中文
Available locales
1) Simplified Chinese 3) Traditional Chinese 4) Simplified Chinese
(China) (Hong Kong) (Singapore)
2) Traditional Chinese
(Republic of China)
Please select language support to install.
这里输入 1 选择 Simplified Chinese (china)简体中文 然后回车
Installation
1) [x] Language settings 2) [!] Time settings
(Simplified Chinese (China)) (Timezone is not set.)
3) [x] Installation source 4) [!] Software selection
(Local media) (Minimal Install)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
回车后重新进入主界面,接下来选择 2 进入 Time settings 时区设置
Time settings
Timezone: not set
NTP servers:not configured
1) Set timezone
2) Configure NTP servers
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]:
选择 1 设置时区
Available regions
1) Africa 6) Atlantic 10) Pacific
2) America 7) Australia 11) US
3) Antarctica 8) Europe 12) Etc
4) Arctic 9) Indian
5) Asia
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 5
选择 5 Asia 亚洲
Available timezones in region Asia
1) Aden 28) Hovd 54) Pontianak
2) Almaty 29) Irkutsk 55) Pyongyang
3) Amman 30) Jakarta 56) Qatar
4) Anadyr 31) Jayapura 57) Qyzylorda
5) Aqtau 32) Jerusalem 58) Riyadh
6) Aqtobe 33) Kabul 59) Sakhalin
7) Ashgabat 34) Kamchatka 60) Samarkand
8) Baghdad 35) Karachi 61) Seoul
9) Bahrain 36) Kathmandu 62) Shanghai
10) Baku 37) Khandyga 63) Singapore
11) Bangkok 38) Kolkata 64) Srednekolymsk
12) Barnaul 39) Krasnoyarsk 65) Taipei
13) Beirut 40) Kuala_Lumpur 66) Tashkent
14) Bishkek 41) Kuching 67) Tbilisi
15) Brunei 42) Kuwait 68) Tehran
16) Chita 43) Macau 69) Thimphu
17) Choibalsan 44) Magadan 70) Tokyo
18) Colombo 45) Makassar 71) Tomsk
19) Damascus 46) Manila 72) Ulaanbaatar
20) Dhaka 47) Muscat 73) Urumqi
21) Dili 48) Nicosia 74) Ust-Nera
22) Dubai 49) Novokuznetsk 75) Vientiane
Press ENTER to continue
回车后 选择 62 Shanghai 上海 然后回车
1) [x] Language settings 2) [x] Time settings
(Simplified Chinese (China)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
重新进入主界面,这时选择 5 进入系统安装位置
Probing storage...
Installation Destination
[x] 1) : 10 GiB (vda)
1 disk selected; 10 GiB capacity; 10 GiB free ...
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]:
这里可以看到 前面创建的虚拟机 10G,输入 C 下一步
Autopartitioning Options
[ ] 1) Replace Existing Linux system(s)
[x] 2) Use All Space
[ ] 3) Use Free Space
Installation requires partitioning of your hard drive. Select what space to use
for the install target.
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]:
这里默认为 2,使用全部空间,输入 C 进入下一步
Partition Scheme Options
[ ] 1) Standard Partition
[ ] 2) Btrfs
[x] 3) LVM
[ ] 4) LVM Thin Provisioning
Select a partition scheme configuration.
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]:
这里默认为 3,使用LVM分区,输入 C 进入下一步
nstallation
1) [x] Language settings 2) [x] Time settings
(Simplified Chinese (China)) (Asia/Shanghai timezone)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [x] Installation Destination 6) [x] Kdump
(Automatic partitioning (Kdump is enabled)
selected) 8) [!] Root password
7) [ ] Network configuration (Password is not set.)
(Not connected)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]:
重新返回主界面 ,输入 8 设置 Root 密码
Please select new root password. You will have to type it twice.
Password:
Password (confirm):
================================================================================
================================================================================
Question
The password you have provided is weak: The password is shorter than 8
characters.
Would you like to use it anyway?
Please respond 'yes' or 'no': yes
输入 yes 确认,然后按 b 进入安装界面
Installing boot loader
.
Performing post-installation setup tasks
.
Configuring installed system
.
Writing network configuration
.
Creating users
.
Configuring addons
.
Generating initramfs
.
Running post-installation scripts
.
Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA
Installation complete. Press return to quit
Installation complete 安装完成,按 回车 退出
接下来可以通过 virsh console 登陆GUEST虚拟机
#查看虚拟机
virsh list
Id Name State
----------------------------------------------------
8 kvm107 running
#通过字符界面登陆虚拟机
virsh console kvm107
输入虚拟机的账号跟密码
Connected to domain kvm107
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64
kvm107 login: root
password:
Last login: Tue Aug 20 21:50:57 from 192.168.0.103
[root@kvm107 ~]#