install kvm hosts use kickstart in CentOS 6 in text mode-阿里云开发者社区

开发者社区> 云计算> 正文
登录阅读全文

install kvm hosts use kickstart in CentOS 6 in text mode

简介:
和前面一篇文章类似, 
本文讲述使用kickstart文件安装kvm虚拟机, 因此不需要连接图形界面.

在服务器安装必要的包, 不再需要图形相关的包.
# yum install -y qemu-img qemu-kvm virt-manager libvirt libvirt-python python-virtinst libvirt-client libvirt libvirt-client virt-what

创建一个虚拟磁盘目录
# 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

[其他]
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 # lis
error: unknown command: 'lis'
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 # 


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章
最新文章
相关文章