install kvm hosts use kickstart in CentOS 6 in text mode

2 minute read

背景

和前面一篇文章类似, 但是本文主要讲的是用kickstart脚本或text模式安装KVM虚拟机操作系统的过程。

http://blog.163.com/digoal@126/blog/static/16387704020153142253971/

本文讲述使用kickstart文件安装kvm虚拟机, 因此不需要连接图形界面.

安装过程

在服务器安装必要的包, 不再需要图形相关的包.

# yum install -y qemu-img qemu-kvm virt-manager libvirt libvirt-python python-virtinst libvirt-client libvirt libvirt-client virt-what libdaemon avahi  

创建一个虚拟磁盘目录

# mkdir /data03/kvmdisk  

创建虚拟磁盘, 用于虚拟机的系统盘

# qemu-img create -f qcow2 -o encryption=off,cluster_size=2M,preallocation=full /data03/kvmdisk/disk01.img 32G  

下载安装镜像

# mkdir /data03/iso  
# cd iso  
# wget http://mirrors.aliyun.com/centos/6.6/isos/x86_64/CentOS-6.6-x86_64-bin-DVD1.iso  

启动libvirtd

# service libvirtd start  
# chkconfig libvirtd on  
# chkconfig libvirt-guests off  

查看当前启动的网桥

# brctl show  
bridge name     bridge id               STP enabled     interfaces  
virbr0          8000.5254001263b0       yes             virbr0-nic  
  
# ifconfig  
em1       Link encap:Ethernet  HWaddr 00:22:19:60:77:8F    
          inet addr:172.16.3.150  Bcast:172.16.3.255  Mask:255.255.255.0  
          inet6 addr: fe80::222:19ff:fe60:778f/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:5469716 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:2830916 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000   
          RX bytes:5147311077 (4.7 GiB)  TX bytes:198552462 (189.3 MiB)  
  
lo        Link encap:Local Loopback    
          inet addr:127.0.0.1  Mask:255.0.0.0  
          inet6 addr: ::1/128 Scope:Host  
          UP LOOPBACK RUNNING  MTU:65536  Metric:1  
          RX packets:79073 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:79073 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:24506711 (23.3 MiB)  TX bytes:24506711 (23.3 MiB)  
  
virbr0    Link encap:Ethernet  HWaddr 52:54:00:12:63:B0    
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  

网桥地址配置

# grep -r 192.168.122 /etc/libvirt  
/etc/libvirt/qemu/networks/default.xml:  <ip address="192.168.122.1" netmask="255.255.255.0">  
/etc/libvirt/qemu/networks/default.xml:      <range start="192.168.122.2" end="192.168.122.254" />  
/etc/libvirt/qemu/networks/autostart/default.xml:  <ip address="192.168.122.1" netmask="255.255.255.0">  
/etc/libvirt/qemu/networks/autostart/default.xml:      <range start="192.168.122.2" end="192.168.122.254" />  

生成密码加密字符串, 即虚拟机的root用户密码 :

# grub-crypt --sha-512  
Password:   
Retype password:   
$6$3V1UXXvrJ1qqWgAO$KaT7KOjWwsVERUZOVE5DB97Og1YIqCQeL54BtHZPasPSYHOnDzUDSS60AJ.3wLUCwVWmIS/HhVnhvo6Y4PWjM1  

选择合适的IP地址 , 例如192.168.122.199. 对应的kickstart配置项

network --onboot yes --device eth0 --bootproto static --ip 192.168.122.199 --netmask 255.255.255.0 --gateway=192.168.122.1 --nameserver=202.101.172.35,202.101.172.47 --hostname digoal.sky-mobi.com --noipv6  

创建kickstart配置文件

# vi /root/ ks.cfg   
# install or upgrade?  
install  
# password  
rootpw  --iscrypted $6$yz7YWSt2MmouUuTu$cjz1eZf9lhMke2Ply8P5Jngrwxh5lb8zwmC124JlXdOnSvrKZaD2/IWdVgahFfNIykbYCBnzCqbbVwjZH59YA.  
authconfig --enableshadow --passalgo=sha512  
firewall --service=ssh  
text  
firstboot --disable  
keyboard us  
lang en_US.UTF-8  
selinux --disabled  
timezone Aisa/Shanghai  
# 配置网络  
network --onboot yes --device eth0 --bootproto static --ip 192.168.122.199 --netmask 255.255.255.0 --gateway=192.168.122.1 --nameserver=202.101.172.35,202.101.172.47 --hostname digoal.sky-mobi.com --noipv6  
# 指定引导分区  
zerombr  
bootloader --location=mbr --driveorder=vda  
# 清除硬盘分区  
clearpart --drives=vda --all --initlabel  
part / --fstype="ext4" --asprimary --size=29000  
part swap --size=2048  
reboot  
%packages  
@base  

开始安装, 使用text console.

# virt-install \  
   --name=centos6_6_x64 \  
   --disk path=/data03/kvmdisk/disk01.img,device=disk,bus=virtio,perms=rw,cache=writethrough \  
   --graphics none \  
   --vcpus=4 --ram=4096 \  
   --location=/data03/iso/CentOS-6.6-x86_64-bin-DVD1.iso \  
   --network bridge=virbr0 \  
   --os-type=linux \  
   --os-variant=rhel6 \  
   --initrd-inject=/root/ks.cfg \  
   --extra-args="ks=file:/ks.cfg console=tty0 console=ttyS0,115200n8"  

等待安装完, 这个虚拟机会自动关闭, 需要人工启动这个虚拟机

# virsh list --all  
 Id    Name                           State  
----------------------------------------------------  
 -     centos6_6_x64                  shut off  

 
# virsh start centos6_6_x64  

开机自动启动配置 :

# vi /etc/rc.local  
/usr/bin/virsh start centos6_6_x64  

text 交互式安装

1. 如果不想使用kickstart, 同时又要使用text安装的话, 只需要略微变动virt-install的参数, 如下 :

这样就完美了, 在没有图形环境时, 可以放心使用了.

virt-install \  
   --name=centos6_6_x64 \  
   --disk path=/data03/kvmdisk/disk01.img,device=disk,bus=virtio,perms=rw,cache=writethrough \  
   --graphics none \  
   --vcpus=4 --ram=4096 \  
   --location=/data03/iso/CentOS-6.6-x86_64-bin-DVD1.iso \  
   --network bridge=virbr0 \  
   --os-type=linux \  
   --os-variant=rhel6 \  
   --extra-args="console=tty0 console=ttyS0,115200n8"  

连接console的方法 :

# virsh  
> console $domainID  
> 退出console 按下 ctrl+]  

例如 :

[root@db-172-16-3-150 ~]# virsh  
Welcome to virsh, the virtualization interactive terminal.  
  
Type:  'help' for help with commands  
       'quit' to quit  
  
virsh # list  
 Id    Name                           State  
----------------------------------------------------  
 2     centos6_6_x64                  running  
  
virsh # console 1  
error: failed to get domain '1'  
error: Domain not found: no domain with matching name '1'  
  
virsh # console 2  
Connected to domain centos6_6_x64  
Escape character is ^]  
  
CentOS release 6.6 (Final)  
Kernel 2.6.32-504.el6.x86_64 on an x86_64  
  
digoal.sky-mobi.com login: root  
Password:   
Last login: Thu Apr  2 00:12:27 on ttyS0  
[root@digoal ~]#   
[root@digoal ~]#   
[root@digoal ~]# exit  
logout  
  
CentOS release 6.6 (Final)  
Kernel 2.6.32-504.el6.x86_64 on an x86_64  
  
digoal.sky-mobi.com login: 这里按下ctrl+]返回本地控制台  
virsh #   
virsh #   

问题

1. 当你启动libvirtd 服务失败的时候。你用status查看这个服务的状态的时候,会发现他提示的是“libvirtd dead but subsys locked” 这边你可能需要将其他几个关联服务给一同启动起来

/etc/init.d/messagebus start

/etc/init.d/avahi-daemon start

/etc/init.d/libvirtd start

当时想使用KVM管理工具virsh时候提示/var/run/libvirt/libvirt-sock 找不到文件夹。

在你安装libvirt包的情况下还出现这个问题

你可能有几个服务没有安装。用yum安装

libdaemon avahi

参考

1. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Getting_Started_Guide/ch05.html#idp8381488
2. man virt-install
3. man virsh
4. man qemu-kvm
5. man qemu-img
6. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/index.html

Flag Counter

digoal’s 大量PostgreSQL文章入口