Tải bản đầy đủ (.pdf) (37 trang)

Tài liệu Ảo hóa với KVM trên Ubuntu 11.10 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (632.06 KB, 37 trang )

Ảo hóa với KVM trên Ubuntu 11.10
Công nghệ ảo hóa trong Linux với KVM (Kernel-based Virtual Machine) hẳn
không còn xa lạ với các nhà quản trị mạng cũng như nhiều người dùng. Tuy
nhiên sự phát triển của những hệ điều hành có nhân Linux hết sức nhanh
chóng, kéo theo sự thay đổi trong cách sử dụng KVM khiến không ít người e
ngại nâng cấp cho hệ thống của mình. Chuyên mục Linux trên QuanTriMang sẽ
cố gắng cập nhật đầy đủ các hướng dẫn tối ưu nhất để bạn đọc có thể dễ dàng thao
tác, kịp thời, nhanh chóng. Bài hướng dẫn sau đây sẽ trình bày cách sử dụng KVM
trên hệ thống Ubuntu 11.10.

1. Một số lưu ý
Trước hết bạn cần đảm bảo rằng phần cứng của mình hỗ trợ công nghệ ảo hóa, tức
là CPU thuộc dòng Intel VT hoặc AMD-V.
Phần minh họa sau chúng tôi sử dụng hostname server1.example.com, địa chỉ IP
của KVM host là 192.168.0.100.
Toàn bộ các lệnh dưới đây cần được chạy dưới quyền root, vì vậy bạn hãy trở
thành root trước khi bắt đầu thực hiện:
Sudo su
2. Cài đặt KVM và vmbuilder
Trước khi tiến hành, bạn cần xác định xem CPU của mình có hỗ trợ ảo hóa phần
cứng hay không:
egrep '(vmx|svm)' color=always /proc/cpuinfo
Nếu màn hình hiển thị thông tin dạng như sau:
root@server1:~# egrep '(vmx|svm)'
color=always /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 ap
ic sep mtrr pge mca cmov pat pse36 clflush

mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp
lm 3dnowext 3dnow rep_good nopl extd_apicid


pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy 3dno
wprefetch lbrv
flags : fpu vme de pse tsc msr pae mce cx8 ap
ic sep mtrr pge mca cmov pat pse36 clflush

mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp
lm 3dnowext 3dnow rep_good nopl extd_apicid

pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy 3dno
wprefetch lbrv
root@server1:~#
Như vậy là CPU của bạn có hỗ trợ, ngược lại nếu không hiển thị gì thì bạn hãy
dừng lại ở đây.
Để cài đặt KVM và vmbuilder (một script để khởi tạo hệ thống máy ảo Ubuntu cơ
bản) chúng ta chạy:
apt-get install ubuntu-virt-server python-vm-builder
kvm-pxe
Sau đó cần thêm vào user như là đăng nhập hiện tại (root) cho nhóm libvirtd:
adduser `id -un` libvirtd
adduser `id -un` kvm
Bạn cần đăng xuất và đăng nhập lại để các thành viên trong nhóm mới có hiệu lực:
Kiểm tra xem KVM đã cài đặt thành công hay chưa, chạy lệnh:
virsh -c qemu:///system list
Nếu màn hình hiển thị các thông tin như dưới đây chứng tỏ bạn đã thành công:
root@server1:~# virsh -c qemu:///system list
Id Name State

root@server1:~#
Ngược lại, nếu có lỗi nào đó hãy thực hiện thêm lần nữa.
Tiếp theo chúng ta cần thiết lập một cầu nối cho mạng trên máy chủ (network

bridge) để có thể truy cập máy ảo từ xa bằng các host khác nhau như hệ thống vật
lý trong mạng. Để làm điều này, chỉ cần cài đặt gói bridge-utils
apt-get install bridge-utils
…và cấu hình một bridge. Mở /etc/network/interfaces:
vi /etc/network/interfaces
Trước khi sửa đổi tập tin sẽ trông như sau:
# This file describes the network interfaces available
on your system
# and how to activate them. For more information, see
interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.100
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
Bạn tiến hành sửa lại như dưới đây:
# This file describes the network interfaces available
on your system
# and how to activate them. For more information, see
interfaces(5).

# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet manual


auto br0
iface br0 inet static
address 192.168.0.100
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off
(Hãy chắc chắn rằng các thiết lập chính xác đối với mạng của bạn!)
Khởi động lại mạng:
/etc/init.d/networking restart
Và chạy:
ifconfig
Network bridge (br0) sẽ hiển thị như sau:
root@server1:~# ifconfig
br0 Link encap:Ethernet HWaddr 00:1e:90:f3:f0:02
inet addr:192.168.0.100 Bcast:192.168.0.255
Mask:255.255.255.0

inet6 addr: fe80::21e:90ff:fef3:f002/64 Scope
:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Met
ric:1
RX packets:17 errors:0 dropped:0 overruns:0 f
rame:0
TX packets:17 errors:0 dropped:0 overruns:0 c
arrier:0

collisions:0 txqueuelen:0
RX bytes:1196 (1.1 KB) TX bytes:1966 (1.9 KB
)

eth0 Link encap:Ethernet HWaddr 00:1e:90:f3:f0:02

UP BROADCAST RUNNING MULTICAST MTU:1500 Met
ric:1
RX packets:35100 errors:0 dropped:0 overruns:
0 frame:0
TX packets:18619 errors:0 dropped:0 overruns:
0 carrier:0

collisions:0 txqueuelen:1000
RX bytes:51126318 (51.1 MB) TX bytes:1521772
(1.5 MB)
Interrupt:41 Base address:0x6000


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:16436 Metric:1

RX packets:0 errors:0 dropped:0 overruns:0 fr
ame:0
TX packets:0 errors:0 dropped:0 overruns:0 ca
rrier:0
collisions:0 txqueuelen:0

RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

virbr0 Link encap:Ethernet HWaddr 46:b4:d1:49:75:eb
inet addr:192.168.122.1 Bcast:192.168.122.25
5 Mask:255.255.255.0

UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 fr
ame:0
TX packets:0 errors:0 dropped:0 overruns:0 ca
rrier:0

collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

root@server1:~#
Trước khi chúng ta bắt đầu máy ảo đầu tiên, nên khởi động lại hệ thống:
Reboot
Nếu không có thể sảy ra thông báo lỗi open /dev/kvm: Permission denied trong thư
mục /var/log/libvirt/qemu/.

3. Tạo một image-based VM
Bây giờ chúng ta có thể tạo ra máy ảo đầu tiên - image-based VM (nếu bạn muốn
có thật nhiều lượng traffic và các thao tác đọc – ghi cho máy ảo, hãy sử dụng một
LVM-based VM như mục 6 sau đây sẽ giới thiệu. Image-based VM gây nặng cho
đĩa cứng IO).
Ở đây tôi muốn tạo máy ảo của mình trong thư mục /var/lib/libvirt/images/ (không
thể tạo trong /root bởi user libvirt-qemu không có quyền đọc trong thư thư mục
này).
Chúng tôi sẽ tạo một thư mục mới cho mỗi máy ảo, chẳng hạn như
/var/lib/libvirt/images/vm1, /var/lib/libvirt/images/vm2,
/var/lib/libvirt/images/vm3,… bởi vì mỗi máy ảo sẽ có một thư mục con có tên
ubuntu-kvm tương ứng với mỗi thư mục vừa tạo. Nếu cố gắng tạo một thư mục thứ
hai trong /var/lib/libvirt/images/vm1, bạn sẽ nhận được thông báo lỗi ubuntu-kvm
already exists (trừ khi bạn chạy vmbuilder với đối số dest=DESTDIR):
root@server1:/var/lib/libvirt/images/vm1# vmbuilder kvm
ubuntu -c vm2.cfg
2009-05-07 16:32:44,185 INFO Cleaning up
ubuntu-kvm already exists
root@server1:/var/lib/libvirt/images/vm1#
Chúng tôi sử dụng công cụ vmbuilder để tạo các máy ảo (tìm hiểu thêm vmbuilder
tại đây). Vmbuilder sử dụng một template để tạo máy ảo – template này nằm
trong thư mục /etc/vmbuilder/libvirt/. Đầu tiên ta tạo một bản sao:
mkdir -p
/var/lib/libvirt/images/vm1/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/*
/var/lib/libvirt/images/vm1/mytemplates/libvirt/
Tiếp theo là đến phần phân vùng cho VM. Chúng ta tạo một tập tin có tên
vmbuilder.partition
vi /var/lib/libvirt/images/vm1/vmbuilder.partition
… và xác định phân vùng mong muốn như sau:

root 8000
swap 4000

/var 20000
Như vậy phân vùng root (/) được xác định với kích thước 8000MB, một phân
vùng swap 4000MB, và một phân vùng /var với 20000MB. Dòng có tác dụng
làm cho các phân vùng bên dưới nó (ở đây là /var) là một ảnh đĩa riêng biệt. Tức
là tạo ra hai ảnh đĩa, một cho root và swa, một cho /var. Tất nhiên, bạn được tự do
định nghĩa bất cứ phân vùng nào mình thích (miễn là bao gồm cả root và swap).
Tôi muốn cài đặt openssh-server trong VM. Để đảm bảo rằng mỗi VM có một
OpenSSH key duy nhất, chúng ta không thể cài đặt openssh-server khi tạo VM. Vì
vậy giải pháp là tạo một script có tên boot.sh để thực thi khi khởi động VM lần
đầu tiên. Nó sẽ cài đặt openssh-server (với một key duy nhất) và có hiệu lực với
user (ở đây sử dụng username mặc định là administrator với mật khẩu mặc định
quantrimang) để thay đổi mật khẩu khi đăng nhập lần đầu tiên.
vi /var/lib/libvirt/images/vm1/boot.sh
# This script will run the first time the virtual
machine boots
# It is ran as root.

# Expire the user account
passwd -e administrator

# Install openssh-server
apt-get update
apt-get install -qqy force-yes openssh-server
Lưu ý thay đổi username administrator với tên đăng nhập mặc định của bạn. (Xem
thêm tại đây). Hoặc bạn cũng có thể định nghĩa một script "first login" tại đây.
Bây giờ hãy nhìn vào:
vmbuilder kvm ubuntu –help

để tìm hiểu thêm về các tùy chọn có sẵn.
Tiếp theo, tạo VM đầu tiên, vm1, chúng ta vào thư mục VM…
cd /var/lib/libvirt/images/vm1/
… và chạy vmbuilder:
vmbuilder kvm ubuntu suite=oneiric flavour=virtual
arch=amd64
mirror= -o
libvirt=qemu:///system ip=192.168.0.101
gw=192.168.0.1 part=vmbuilder.partition
templates=mytemplates user=administrator
name=Administrator pass=quantrimang addpkg=vim-nox
addpkg=unattended-upgrades addpkg=acpid
firstboot=/var/lib/libvirt/images/vm1/boot.sh mem=256
hostname=vm1 bridge=br0
Hầu hết các tùy chọn là tự nó giải thích. part quy định cụ thể các tập tin với chi
tiết các phân vùng, có liên quan tới thư mục đang hoạt động (đó là lý do tại sao
chúng ta cần đi tới thư mục VM trước khi chạy vmbuilder). templates quy định
cụ thể thư mục lưu giữ tập tin template (liên quan tới thư mục đang hoạt động), và
firstboot quy định script cho lần khởi động đầu tiên. libvirt=qemu:///system
sẽ “nói” cho KVM để thêm VM này vào danh sách máy ảo có sẵn. addpkg cho
phép bạn chỉ định các gói Ubuntu muốn cài đặt trong quá trình khởi tạo VM (đây
là lý do tại sao không nên thêm openssh-server vào danh sách và sử dụng script để
thay thế). bridge thiết lập một bridge network; như bridge br0 mà chúng ta đã
tạo trong mục 2. Bây giờ là lúc xác định bridge.
Trong dòng mirror bạn có thể chỉ định một kho lưu trữ chính thức của Ubuntu,
chẳng hạn Nếu bỏ qua mirror, Ubuntu sẽ
sử dụng kho mặc định (
Quá trình xây dựng có thể mất vài phút. Sau đó bạn sẽ tìm thấy tập tin cấu hình
XML cho VM trong /etc/libvirt/qemu/ (=> /etc/libvirt/qemu/vm1.xml):
s -l /etc/libvirt/qemu/

root@server1:/var/lib/libvirt/images/vm1# ls -l
/etc/libvirt/qemu/
total 8
drwxr-xr-x 3 root root 4096 2011-11-16 11:01 networks
-rw 1 root root 2017 2011-11-16 11:18 vm1.xml
root@server1:/var/lib/libvirt/images/vm1#
Các ảnh đĩa được đặt trong ubuntu-kvm/ - thư mục con của thư mục VM:
s -l /var/lib/libvirt/images/vm1/ubuntu-kvm/
root@server1:/var/lib/libvirt/images/vm1# ls -l
/var/lib/libvirt/images/vm1/ubuntu-kvm/
total 627736
-rw-r r 1 root root 315424768 2011-11-16 11:17
tmpYxLTYF.qcow2
-rw-r r 1 root root 327614464 2011-11-16 11:18
tmpyyxauA.qcow2
root@server1:/var/lib/libvirt/images/vm1
4. Tạo một VM thứ hai
Tương tự như trên, nếu muốn tạo thêm một VM nữa (vm2), dưới đây là một tóm
tắt về các lệnh cần thực hiện:
mkdir -p
/var/lib/libvirt/images/vm2/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/*
/var/lib/libvirt/images/vm2/mytemplates/libvirt/
vi /var/lib/libvirt/images/vm2/vmbuilder.partition
vi /var/lib/libvirt/images/vm2/boot.sh
cd /var/lib/libvirt/images/vm2/
vmbuilder kvm ubuntu suite=oneiric flavour=virtual
arch=amd64
mirror= -o
libvirt=qemu:///system ip=192.168.0.102

gw=192.168.0.1 part=vmbuilder.partition
templates=mytemplates user=administrator
name=Administrator pass=howtoforge addpkg=vim-nox -
-addpkg=unattended-upgrades addpkg=acpid
firstboot=/var/lib/libvirt/images/vm2/boot.sh mem=256
hostname=vm2 bridge=br0
(Lưu ý rằng bạn không thể tạo một thư mục mới cho VM
(/var/lib/libvirt/images/vm2) nếu truyền đối số -d DESTDIR cho lệnh vmbuilder
– nó cho phép bạn tạo một VM mới trong thư mục đã tạo VM khác. Trong trường
hợp đó không cần tạo mới tập tin vmbuilder.partition và boot.sh, cũng không cần
chỉnh sửa template, nhưng có thể dễ dàng sử dụng các tập tin đã tồn tại:
cd /var/lib/libvirt/images/vm1/
vmbuilder kvm ubuntu suite=oneiric flavour=virtual
arch=amd64
mirror= -o
libvirt=qemu:///system ip=192.168.0.102
gw=192.168.0.1 part=vmbuilder.partition
templates=mytemplates user=administrator
name=Administrator pass=howtoforge addpkg=vim-nox -
-addpkg=unattended-upgrades addpkg=acpid
firstboot=/var/lib/libvirt/images/vm1/boot.sh mem=256
hostname=vm2 bridge=br0 -d vm2-kvm
)
5. Quản trị máy ảo
Các máy cảo có thể được quản trị thông qua virsh, là một "virtual shell". Để kết
nối tới virtual shell ta chạy:
virsh connect qemu:///system
virtual shell sẽ trông như sau:
root@server1:~# virsh connect qemu:///system
Welcome to virsh, the virtualization interactive

terminal.
Type: 'help' for help with commands
'quit' to quit
virsh #
Bây giờ có thể nhập vào lệnh sau trên virtual shell để quản trị máy ảo của bạn:
Help
Một danh sách các lệnh có sẵn sẽ được liệt kê:
virsh # help
Grouped commands:

Domain Management (help keyword 'domain'):
attach-
device attach device from an XML file

attach-disk attach disk device
attach-
interface attach network interface
autostart autostart a domain
blkiotune Get or set blkio par
ameters

console connect to the guest
console
cpu-baseline compute baseline CPU
cpu-
compare compare host CPU with a CPU
described by an XML file

create create a domain from
an XML file

define define (but don't st
art) a domain from an XML file

destroy destroy a domain
detach-
device detach device from an XML file
detach-disk detach disk device

detach-
interface detach network interface
domid convert a domain nam
e or UUID to domain id
domjobabort abort active domain
job

domjobinfo domain job informati
on
domname convert a domain id
or UUID to domain name
domuuid convert a domain nam
e or id to domain UUID

domxml-from-
native Convert native config to domain XML
domxml-to-
native Convert domain XML to native confi
g
dump dump the core of a d
omain to a file for analysis


dumpxml domain information i
n XML
edit edit XML configurati
on for a domain
inject-
nmi Inject NMI to the guest

managedsave managed save of a do
main state
managedsave-
remove Remove managed save of a domain
maxvcpus connection vcpu maxi
mum

memtune Get or set memory pa
rameters
migrate migrate domain to an
other host
migrate-
setmaxdowntime set maximum tolerable downtime

reboot reboot a domain
restore restore a domain fro
m a saved state in a file

resume resume a domain
save save a domain state
to a file
schedinfo show/set scheduler p
arameters


screenshot take a screenshot of
a current domain console and store it into a file
setmaxmem change maximum memor
y limit

setmem change memory alloca
tion
setvcpus change number of vir
tual CPUs
shutdown gracefully shutdown
a domain

start start a (previously
defined) inactive domain
suspend suspend a domain
ttyconsole tty console

undefine undefine an inactive
domain
update-
device update device from an XML file
vcpucount domain vcpu counts

vcpuinfo detailed domain vcpu
information
vcpupin control domain vcpu
affinity
version show version
vncdisplay vnc display



Domain Monitoring (help keyword 'monitor'):
domblkinfo domain block device
size information
domblkstat get device block sta
ts for a domain

domifstat get network interfac
e stats for a domain
dominfo domain information
dommemstat get memory statistic
s for a domain

domstate domain state
list list domains

Host and Hypervisor (help keyword 'host'):
capabilities capabilities

connect (re)connect to hyper
visor
freecell NUMA free memory
hostname print the hypervisor
hostname
nodeinfo node information

qemu-monitor-command Qemu Monitor Command
sysinfo print the hypervisor
sysinfo

uri print the hypervisor
canonical URI


Interface (help keyword 'interface'):
iface-
begin create a snapshot of current i
nterfaces settings, which can be later commited (iface-
commit) or restored (iface-rollback)

iface-
commit commit changes made since ifac
e-begin and free restore point
iface-
define define (but don't start) a phy
sical host interface from an XML file

iface-
destroy destroy a physical host interf
ace (disable it / "if-down")
iface-
dumpxml interface information in XML

iface-
edit edit XML configuration for a p
hysical host interface
iface-
list list physical host interfaces
iface-
mac convert an interface name to i

nterface MAC address

iface-
name convert an interface MAC addre
ss to interface name
iface-
rollback rollback to previous saved con
figuration created via iface-begin

iface-
start start a physical host interfac
e (enable it / "if-up")
iface-
undefine undefine a physical host inter
face (remove it from configuration)


Network Filter (help keyword 'filter'):
nwfilter-
define define or update a network filter
from an XML file

nwfilter-
dumpxml network filter information in XML
nwfilter-
edit edit XML configuration for a netw
ork filter
nwfilter-list list network filters

nwfilter-

undefine undefine a network filter

Networking (help keyword 'network'):
net-autostart autostart a network

net-
create create a network from an XML
file
net-
define define (but don't start) a n
etwork from an XML file

net-destroy destroy a network
net-
dumpxml network information in XML
net-
edit edit XML configuration for a
network

net-info network information
net-list list networks
net-
name convert a network UUID to ne
twork name

net-
start start a (previously defined)
inactive network
net-
undefine undefine an inactive network

net-
uuid convert a network name to ne
twork UUID


Node Device (help keyword 'nodedev'):
nodedev-
create create a device defined by an XM
L file on the node

nodedev-
destroy destroy a device on the node
nodedev-
dettach dettach node device from its dev
ice driver

nodedev-
dumpxml node device details in XML
nodedev-
list enumerate devices on this host
nodedev-
reattach reattach node device to its devi
ce driver

nodedev-reset reset node device

Secret (help keyword 'secret'):
secret-
define define or modify a secret from
an XML file


secret-
dumpxml secret attributes in XML
secret-get-
value Output a secret value
secret-list list secrets
secret-set-value set a secret value

secret-undefine undefine a secret

Snapshot (help keyword 'snapshot'):
snapshot-create Create a snapshot
snapshot-
current Get the current snapshot

×