在CentOS下面多部分命令都是有包libguestfs-tools-c
提供,所以,首先需要安装它
virt-ls
virt-ls可以列出虚拟机中目录下的文件或目录,用法如下
1 2 |
|
如
1
|
|
可以像使用ls
一样加一些参数,如-l
等,具体请看virt-ls --help
virt-what
virt-what可以用来检测当前系统是不是一个虚拟机,如果不是虚拟机,执行virt-what将不会有任何输出,如果是虚拟机,它会打印一系列关于虚拟机的’facts’(如kvm)
virt-what命令由同名包提供,要命令需要先安装(yum -y install virt-what)
virt-host-validate
这个命令可以用来检测本机是否正确配置以运行虚拟化,如果没有加参数,它会检查它所知道的所有的虚拟化驱动,可选的可以加qemu
或lxc
做限制
1
|
|
输出类似这样
1 2 3 4 5 |
|
virt-top
virt-top命令由同名软件包提供,和top命令相似,只是进程换成了虚拟机
1 2 |
|
输出
1 2 3 4 5 6 7 8 9 |
|
virt-cat
virt-cat可以虚拟机中文件的内容,用法如下
1 2 |
|
如
1
|
|
domname可以通过virsh list
得到
也可以对虚拟机的磁盘文件操作
1
|
|
virt-edit
这个命令可以修改
1 2 |
|
例如
1
|
|
在我的系统中它会用vim
打开文件,编辑完保存即可修改虚拟机内的文件内容
也可以直接对虚拟机的磁盘文件进行操作
1
|
|
Note: 如果虚拟机正在运行,使用第一种文件修改它的文件会有下面的报错
1 2 3 4 5 |
|
但直接对虚拟机磁盘镜像文件操作不会有这个提示,并且可以修改成功,会不会出问题我就不知道了
virt-copy-out
virt-copy-out这个命令可以把虚拟机里的文件复制出来, 用法如下
1 2 |
|
例子
1
|
|
可以是多个文件或目录
1 2 |
|
也可以直接对虚拟机磁盘文件操作,只需要将-d domname
换成-a path_of_disk_file
virt-copy-in
virt-copy-in是将文件复制到虚拟机里面,用法和virt-copy-out基本相同,这里只举一个例子
1
|
|
不出你的所料,如果虚拟机正在运行,上面的命令也会报错
更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-08/105140p2.htm
virt-df
这个命令是比较简单了,就是将在虚拟机中执行df
命令的结果输出
1 2 |
|
可以加-h
参数以human-readable
显示
virt-alignment-scan
旧的操作系统安装时会使用不对齐的分区,这会引起一些不必要的I/O,这个命令的作用是检查是否正在不对齐的问题,如果存在,只是警告(Warns)你,当前这个工具不会帮你解决这个问题
1
|
|
输出类似这样
1
|
|
virt-inspector2
这个命令可以显示虚拟机的操作系统版本和其它一些信息,包含的信息非常多,用法非常简单
1
|
|
输出类似这样
1 2 3 4 5 6 7 8 9 |
|
virt-resize
- virt-resize可以调整虚拟机磁盘的大小,调整或删除任何分区
- virt-resize不可以就地调整磁盘,不应该对正在运行的虚拟机进行磁盘调整,为了确保一致性,调整先需要关闭虚拟机
- virt-resize调整的过程非常慢,从35G的磁盘进行扩展需要差不多10分钟
- virt-resize调整所花的时候只和开始磁盘的大小有关,从35G扩展到40G和扩展到135G所花的时间差不多
- 如果你使用qcow2磁盘格式,个人建议先转成raw,调整完后再转回去,因为直接对qcow2做调整,比较35G的qcow2磁盘镜像文件可能只有1G大小(ls查看),通过virt-resize调整后就会变成35G大小了(ls查看)(也可能是我的方法不对),先转成raw调整完大小后再转回去可以避免这个问题
概要
1 2 3 |
|
- 示例1.给一个分区增加5G
首先关闭虚拟机
1
|
|
查看分区情况
1
|
|
1 2 3 4 |
|
把qcow2格式的磁盘镜像转成raw
1 2 3 |
|
利用truncate创建一个新的文件,大小比centos2.raw大5G
1 2 |
|
开始调整
1
|
|
你应该看到类似这样的信息
1 2 3 4 5 6 7 8 9 10 11 |
|
然后是持续好久的刷屏信息,好在有倒计时
最后应该看到类似下面的信息
1 2 3 4 |
|
调整完后转回qcow2格式
1
|
|
虚拟机里面不用再做操作,现在使用新的磁盘镜像文件启动虚拟机应该可以看到/dev/sda1已经从35G变为40G了
分区的缩减我们一般用不到,没有做测试,lvm的调整可以参考这里
virt-install
virt-install命令由python-virtinst
提供,需要安装python-virtinst才可以使用virt-install
1
|
|
安装例子
virt-install --name kvm-test-centos-6.2-x64 --ram 1024 --vcpus 4 --cdrom /opt/isos/CentOS-6.2-x86_64-bin-DVD1.iso --network bridge:virbr0 --vnc --vncport=5910 --vnclisten=localhost --disk /opt/images/kvm-test-centos-6.2-64bit.img,size=20
如果磁盘文件不存在,会自动创建(需要size参数),默认创建是raw磁盘镜像,可以用qemu-img手动创建磁盘文件
qemu-img create -f raw kvm-test-centos-6.2-64bit.img 20G
这里磁盘格式常见的有两种,raw和qcow2(还有qed正在开发中,据说性能更好)raw的读写性能要比qcow2好,但如果你需要快照等高级特性,可以选择qcow2如果使用qcow2,加上preallocation性能会有所提升
qemu-img create -f qcow2 -o preallocation=metadata kvm-test-centos-6.2-64bit.img 20G
不同的bus类型,cache类型和aio选择都会有性能有影响,所以你可希望把这些也加进去,格式类似这样
...
--disk path=/opt/images/kvm-test-centos-6.2-64bit.img,size=20,bus=virtio,cache=none,aio=threads,format=qcow2 ...
这里可供选择的参数有
bus: virtio, ide
cache: unsafe, none, writeback, writethrough, directsync
aio: threads, native
不同的网卡驱动类型会影响到虚拟机的网络性能,可以这样指定
...
--network bridge:virbr0,model=e1000 ...
可供选择的参数有
# 可以通过qemu-system-x86_64 -net nic,model=?查到支持的参数
model: ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio
virbr0是系统自动创建的桥,可以手动创建桥接设备然后指定虚拟机使用 如果你的虚拟机是windows并且想使用virtio做为硬盘驱动和网卡驱动,你需要下载两个驱动文件 这两个文件可以从这里下载
网卡驱动
virt-install --name kvm-test-win2003 --ram 1024 --vcpus 1 --cdrom /opt/isos/cn_win_srv_2003_r2_enterprise_x64_with_sp2_vl_cd1_X13-47314.iso --network bridge:virbr0,model=virtio --vnc --disk path=/opt/images/kvm-test-win2003.img,bus=virtio,cache=none,format=qcow2,size=20 --disk path=/opt/drivers/virtio-win-1.1.16.vfd,device=floppy --disk path=/opt/drivers/virtio-win-0.1-12.iso,device=cdrom,perms=ro
如果不出问题,应该可以看到安装窗口了(需要安装virt-viewer)如果没有安装virt-viewer或者你在没有安装图形的服务器上操作 你仍然可以通过以下方法访问到安装窗口
vncviewer vnclisten:vncport
前面的–vnclisten就不能写localhost了
第二种方法是:
virt-viewer --connect qemu+ssh://user@ip_address:port/system name_of_instance
kickstart安装也可以用在virt-install中
yum -y install httpd
mkdir /var/www/centos
mount -o loop /opt/isos/CentOS-6.2-x86_64-bin-DVD1.iso /var/www/centos
cp /root/ks.cfg /var/www/centos
/etc/init.d/httpd restart
virt-install --name kvm-test-centos-6.2-x64 --ram 1024 --vcpus 4 --location http://192.168.122.1/centos/
--network bridge:virbr0 --vnc --vncport=5910 --vnclisten=localhost --disk /opt/images/kvm-test-centos-6.2-64bit.img,size=20
--extra-args "ks=http://192.168.122.1/ks.cfg ip=192.168.122.10 ip=192.168.122.10 netmask=255.255.255.0 gateway=192.168.122.1 dns=8.8.8.8"
安装好之后,就可以使用virsh对虚拟做一些操作了virt-install的更多参数可以参考这里 http://www.linuxidc.com/Linux/2014-08/105140p3.htm
virt-install 参数
In the previous chapter we explored the creation and management of KVM guest operating systems using the virt-manager graphical tool. In this chapter we will turn our attention to the creation of KVM guest operating system using the virt-install command-line tool.
The virt-install tool is supplied to allow new virtual machines to be created by providing a list of command-line options. Whilst most users will probably stay with the graphical virt-mamager tool, virt-install has the advantage that virtual machines can be created when access to a graphical desktop is not available, or when creation needs to be automated in a script.
This chapter assumes that the necessary KVM tools are installed and that the system was rebooted after these were installed. For details on these requirements read Installing and Configuring Fedora KVM Virtualization.
|
||
Preparing the System for virt-install
virt-install provides the option of supporting graphics for the guest operating system installation. This is achieved through use of QEMU. If graphics support is disabled (the default is to enable it) during the virt-install session, the standard text based installer will be used.
Running virt-install to Build the KVM Guest System
virt-install must be run as root and accepts a wide range of command-line arguments that are used to provide configuration information related to the virtual machine being created. Some of these command-line options are mandatory (specifically name, ram and disk storage must be provided) while others are optional. A summary of these arguments is outlined in the following table:
Argument |
Description |
---|---|
-h, –help | Show the help message and exit |
–connect=CONNECT | Connect to a non-default hypervisor. |
-n NAME, –name=NAME | Name of the new guest virtual machine instance. This must be unique amongst all guests known to the hypervisor on the connection, including those not currently active. To re-define an existing guest, use the virsh(1) tool to shut it down (’virsh shutdown’) & delete (’virsh undefine’) it prior to running “virt-install”. |
-r MEMORY, –ram=MEMORY | Memory to allocate for guest instance in megabytes. If the hypervisor does not have enough free memory, it is usual for it to automatically take memory away from the host operating system to satisfy this allocation. |
–arch=ARCH | Request a non-native CPU architecture for the guest virtual machine. The option is only currently available with QEMU guests, and will not enable use of acceleration. If omitted, the host CPU architecture will be used in the guest. |
-u UUID, –uuid=UUID | UUID for the guest; if none is given a random UUID will be generated. If you specify UUID, you should use a 32-digit hexadecimal number. UUID are intended to be unique across the entire data center, and indeed world. Bear this in mind if manually specifying a UUID |
–vcpus=VCPUS | Number of virtual cpus to configure for the guest. Not all hypervisors support SMP guests, in which case this argument will be silently ignored |
–check-cpu | Check that the number virtual cpus requested does not exceed physical CPUs and warn if they do. |
–cpuset=CPUSET | Set which physical cpus the guest can use. “CPUSET” is a comma separated list of numbers, which can also be specified in ranges. If the value ’auto’ is passed, virt-install attempts to automatically determine an optimal cpu pinning using NUMA data, if available. |
–os-type=OS_TYPE | Optimize the guest configuration for a type of operating system (ex. ’linux’, ’windows’). This will attempt to pick the most suitable ACPI & APIC settings, optimally supported mouse drivers, virtio, and generally accommodate other operating system quirks. See “–os-variant” for valid options. For a full list of valid options refer to the man page (man virt-install). |
–os-variant=OS_VARIANT | Further optimize the guest configuration for a specific operating system variant (ex. ’fedora8’, ’winxp’). This parameter is optional, and does not require an “–os-type” to be specified. For a full list of valid options refer to the man page (man virt-install). |
–host-device=HOSTDEV | Attach a physical host device to the guest. HOSTDEV is a node device name as used by libvirt (as shown by ’virsh nodedev-list’). |
–sound | Attach a virtual audio device to the guest. (Full virtualization only). |
–noacpi | Override the OS type / variant to disables the ACPI setting for fully virtualized guest. (Full virtualization only). |
-v, –hvm | Request the use of full virtualization, if both para & full virtualization are available on the host. This parameter may not be available if connecting to a Xen hypervisor on a machine without hardware virtualization support. This parameter is implied if connecting to a QEMU based hypervisor. |
-p, –paravirt | This guest should be a paravirtualized guest. If the host supports both para & full virtualization, and neither this parameter nor the “–hvm” are specified, this will be assumed. |
–accelerate | When installing a QEMU guest, make use of the KVM or KQEMU kernel acceleration capabilities if available. Use of this option is recommended unless a guest OS is known to be incompatible with the accelerators. The KVM accelerator is preferred over KQEMU if both are available. |
-c CDROM, –cdrom=CDROM | File or device use as a virtual CD-ROM device for fully virtualized guests. It can be path to an ISO image, or to a CDROM device. It can also be a URL from which to fetch/access a minimal boot ISO image. The URLs take the same format as described for the “–location” argument. If a cdrom has been specified via the “–disk” option, and neither “–cdrom” nor any other install option is specified, the “–disk” cdrom is used as the install media. |
-l LOCATION, –location=LOCATION | Installation source for guest virtual machine kernel+initrd pair. The “LOCATION” can take one of the following forms:
|
–pxe | Use the PXE boot protocol to load the initial ramdisk and kernel for starting the guest installation process. |
–import | Skip the OS installation process, and build a guest around an existing disk image. The device used for booting is the first device specified via “–disk” or “–file”. |
–livecd | Specify that the installation media is a live CD and thus the guest needs to be configured to boot off the CDROM device permanently. It may be desirable to also use the “–nodisks” flag in combination. |
-x EXTRA, –extra-args=EXTRA | Additional kernel command line arguments to pass to the installer when performing a guest install from “–location”. |
–disk=DISKOPTS | Specifies media to use as storage for the guest, with various options. |
–disk opt1=val1,opt2=val2,… | To specify media, one of the following options is required:
|
-f DISKFILE, –file=DISKFILE | Path to the file, disk partition, or logical volume to use as the backing store for the guest’s virtual disk. This option is deprecated in favor of “–disk”. |
-s DISKSIZE, –file-size=DISKSIZE | Size of the file to create for the guest virtual disk. This is deprecated in favor of “–disk”. |
–nonsparse | Fully allocate the storage when creating. This is deprecated in favort of “–disk” |
–nodisks | Request a virtual machine without any local disk storage, typically used for running ’Live CD’ images or installing to network storage (iSCSI or NFS root). |
-w NETWORK, –network=NETWORK | Connect the guest to the host network. The value for “NETWORK” can take one of 3 formats:
|
-b BRIDGE, –bridge=BRIDGE | Bridge device to connect the guest NIC to. This parameter is deprecated in favour of the “–network” parameter. |
-m MAC, –mac=MAC | Fixed MAC address for the guest; If this parameter is omitted, or the value “RANDOM” is specified a suitable address will be randomly generated. For Xen virtual machines it is required that the first 3 pairs in the MAC address be the sequence ’00:16:3e’, while for QEMU or KVM virtual machines it must be ’54:52:00’. |
–nonetworks | Request a virtual machine without any network interfaces. |
–vnc | Setup a virtual console in the guest and export it as a VNC server in the host. Unless the “–vncport” parameter is also provided, the VNC server will run on the first free port number at 5900 or above. The actual VNC display allocated can be obtained using the “vncdisplay” command to “virsh” (or virt-viewer(1) can be used which handles this detail for the use). |
–vncport=VNCPORT | Request a permanent, statically assigned port number for the guest VNC console. Use of this option is discouraged as other guests may automatically choose to run on this port causing a clash. |
–sdl | Setup a virtual console in the guest and display an SDL window in the host to render the output. If the SDL window is closed the guest may be unconditionally terminated. |
–nographics | No graphical console will be allocated for the guest. Fully virtualized guests (Xen FV or QEmu/KVM) will need to have a text console configured on the first serial port in the guest (this can be done via the –extra-args option). Xen PV will set this up automatically. The command ’virsh console NAME’ can be used to connect to the serial device. |
–noautoconsole | Don’t automatically try to connect to the guest console. The default behaviour is to launch a VNC client to display the graphical console, or to run the “virsh” “console” command to display the text console. Use of this parameter will disable this behaviour. |
-k KEYMAP, –keymap=KEYMAP | Request that the virtual VNC console be configured to run with a non- English keyboard layout. |
-d, –debug | Print debugging information to the terminal when running the install process. The debugging information is also stored in “$HOME/.virtinst/virt-install.log” even if this parameter is omitted. |
–noreboot | Prevent the domain from automatically rebooting after the install has completed. |
–wait=WAIT | Amount of time to wait (in minutes) for a VM to complete its install. Without this option, virt-install will wait for the console to close (not neccessarily indicating the guest has shutdown), or in the case of –noautoconsole, simply kick off the install and exit. Any negative value will make virt-install wait indefinitely, a value of 0 triggers the same results as noautoconsole. If the time limit is succeeded, virt-install simply exits, leaving the virtual machine in its current state. |
–force | Prevent interactive prompts. If the intended prompt was a yes/no prompt, always say yes. For any other prompts, the application will exit. |
–prompt | Specifically enable prompting. Default prompting is off (as of virtinst 0.400.0) |
Example virt-install Command
With reference to the above command-line argument list, we can now look at an example command-line construct using the virt-install tool.
The following command creates a new KVM virtual machine configured to run Windows XP. It creates a new, 6GB disk image, assigns 512MB of RAM to the virtual machine, configures a CD device for the installation media and uses VNC to display the console:
virt-install --name myWinXP --ram 512 --disk path=/tmp/winxp.img,size=6 --network network:default --vnc --os-variant winxp --cdrom /dev/sr0
Once the guest system has been created, the virt-viewer screen will appear containing the operating system installer loaded from the specified installation media:
Follow the standard installation procedure for the guest operating system.
Summary
In the chapter we have looked at the steps necessary to create a KVM Virtual System using the virt-install command line tool.
Once the installation is completed the next step is to learn how to administer KVM virtual systems system. This can be achieved the graphical virt-manager tool. (see Managing and Monitoring Fedora based KVM Guest Systems).