kvm安装虚拟机命令详解

简介: kvm安装虚拟机命令详解

kvm的命令是非常多的,但,主要使用的关于创建虚拟机,启动虚拟机的命令必定是 virt-install。那么,我们想要学习一个命令,当然最快速简捷的方式就是通过帮助文档啦。

下面就是virt-install  的帮助文档。可以看到,该命令是有六个部分组成:

(1)基本选项General Options,

(2)Installation Method Options安装方法选项,

(3)Device Options设备选项

(4)虚拟机配置选项Guest Configuration Options

(5)Virtualization Platform Options虚拟模式选项

(6)其它辅助选项Miscellaneous Options

这里需要注意啦,该命令是比较详细的给每个选项都给出了示例,

[root@centos1 ~]# virt-install --help
usage: virt-install --name NAME --memory MB STORAGE INSTALL [options]
Create a new virtual machine from specified install media.
optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --connect URI         Connect to hypervisor with libvirt URI
General Options:
  -n NAME, --name NAME  Name of the guest instance
  --memory MEMORY       Configure guest memory allocation. Ex:
                        --memory 1024 (in MiB)
                        --memory 512,maxmemory=1024
                        --memory 512,maxmemory=1024,hotplugmemorymax=2048,hotplugmemoryslots=2
  --vcpus VCPUS         Number of vcpus to configure for your guest. Ex:
                        --vcpus 5
                        --vcpus 5,maxvcpus=10,cpuset=1-4,6,8
                        --vcpus sockets=2,cores=4,threads=2
  --cpu CPU             CPU model and features. Ex:
                        --cpu coreduo,+x2apic
                        --cpu host-passthrough
                        --cpu host
  --metadata METADATA   Configure guest metadata. Ex:
                        --metadata name=foo,title="My pretty title",uuid=...
                        --metadata description="My nice long description"
Installation Method Options:
  --cdrom CDROM         CD-ROM installation media
  -l LOCATION, --location LOCATION
                        Installation source (eg, nfs:host:/path,
                        http://host/path, ftp://host/path)
  --pxe                 Boot from the network using the PXE protocol
  --import              Build guest around an existing disk image
  --livecd              Treat the CD-ROM media as a Live CD
  -x EXTRA_ARGS, --extra-args EXTRA_ARGS
                        Additional arguments to pass to the install kernel
                        booted from --location
  --initrd-inject INITRD_INJECT
                        Add given file to root of initrd from --location
  --os-variant DISTRO_VARIANT
                        The OS variant being installed guests, e.g.
                        'fedora18', 'rhel6', 'winxp', etc.
  --boot BOOT           Configure guest boot settings. Ex:
                        --boot hd,cdrom,menu=on
                        --boot init=/sbin/init (for containers)
  --idmap IDMAP         Enable user namespace for LXC container. Ex:
                        --idmap uid_start=0,uid_target=1000,uid_count=10
Device Options:
  --disk DISK           Specify storage with various options. Ex.
                        --disk size=10 (new 10GiB image in default location)
                        --disk /my/existing/disk,cache=none
                        --disk device=cdrom,bus=scsi
                        --disk=?
  -w NETWORK, --network NETWORK
                        Configure a guest network interface. Ex:
                        --network bridge=mybr0
                        --network network=my_libvirt_virtual_net
                        --network network=mynet,model=virtio,mac=00:11...
                        --network none
                        --network help
  --graphics GRAPHICS   Configure guest display settings. Ex:
                        --graphics vnc
                        --graphics spice,port=5901,tlsport=5902
                        --graphics none
                        --graphics vnc,password=foobar,port=5910,keymap=ja
  --controller CONTROLLER
                        Configure a guest controller device. Ex:
                        --controller type=usb,model=ich9-ehci1
  --input INPUT         Configure a guest input device. Ex:
                        --input tablet
                        --input keyboard,bus=usb
  --serial SERIAL       Configure a guest serial device
  --parallel PARALLEL   Configure a guest parallel device
  --channel CHANNEL     Configure a guest communication channel
  --console CONSOLE     Configure a text console connection between the guest
                        and host
  --hostdev HOSTDEV     Configure physical USB/PCI/etc host devices to be
                        shared with the guest
  --filesystem FILESYSTEM
                        Pass host directory to the guest. Ex: 
                        --filesystem /my/source/dir,/dir/in/guest
                        --filesystem template_name,/,type=template
  --sound [SOUND]       Configure guest sound device emulation
  --watchdog WATCHDOG   Configure a guest watchdog device
  --video VIDEO         Configure guest video hardware.
  --smartcard SMARTCARD
                        Configure a guest smartcard device. Ex:
                        --smartcard mode=passthrough
  --redirdev REDIRDEV   Configure a guest redirection device. Ex:
                        --redirdev usb,type=tcp,server=192.168.1.1:4000
  --memballoon MEMBALLOON
                        Configure a guest memballoon device. Ex:
                        --memballoon model=virtio
  --tpm TPM             Configure a guest TPM device. Ex:
                        --tpm /dev/tpm
  --rng RNG             Configure a guest RNG device. Ex:
                        --rng /dev/urandom
  --panic PANIC         Configure a guest panic device. Ex:
                        --panic default
  --memdev MEMDEV       Configure a guest memory device. Ex:
                        --memdev dimm,target_size=1024
Guest Configuration Options:
  --security SECURITY   Set domain security driver configuration.
  --cputune CPUTUNE     Tune CPU parameters for the domain process.
  --numatune NUMATUNE   Tune NUMA policy for the domain process.
  --memtune MEMTUNE     Tune memory policy for the domain process.
  --blkiotune BLKIOTUNE
                        Tune blkio policy for the domain process.
  --memorybacking MEMORYBACKING
                        Set memory backing policy for the domain process. Ex:
                        --memorybacking hugepages=on
  --features FEATURES   Set domain <features> XML. Ex:
                        --features acpi=off
                        --features apic=on,eoi=on
  --clock CLOCK         Set domain <clock> XML. Ex:
                        --clock offset=localtime,rtc_tickpolicy=catchup
  --pm PM               Configure VM power management features
  --events EVENTS       Configure VM lifecycle management policy
  --resource RESOURCE   Configure VM resource partitioning (cgroups)
  --sysinfo SYSINFO     Configure SMBIOS System Information. Ex:
                        --sysinfo emulate
                        --sysinfo host
                        --sysinfo bios_vendor=Vendor_Inc.,bios_version=1.2.3-abc,...
                        --sysinfo system_manufacturer=System_Corp.,system_product=Computer,...
                        --sysinfo baseBoard_manufacturer=Baseboard_Corp.,baseBoard_product=Motherboard,...
  --qemu-commandline QEMU_COMMANDLINE
                        Pass arguments directly to the qemu emulator. Ex:
                        --qemu-commandline='-display gtk,gl=on'
                        --qemu-commandline env=DISPLAY=:0.1
Virtualization Platform Options:
  -v, --hvm             This guest should be a fully virtualized guest
  -p, --paravirt        This guest should be a paravirtualized guest
  --container           This guest should be a container guest
  --virt-type HV_TYPE   Hypervisor name to use (kvm, qemu, xen, ...)
  --arch ARCH           The CPU architecture to simulate
  --machine MACHINE     The machine type to emulate
Miscellaneous Options:
  --autostart           Have domain autostart on host boot up.
  --transient           Create a transient domain.
  --wait WAIT           Minutes to wait for install to complete.
  --noautoconsole       Don't automatically try to connect to the guest
                        console
  --noreboot            Don't boot guest after completing install.
  --print-xml [XMLONLY]
                        Print the generated domain XML rather than create the
                        guest.
  --dry-run             Run through install process, but do not create devices
                        or define the guest.
  --check CHECK         Enable or disable validation checks. Example:
                        --check path_in_use=off
                        --check all=off
  -q, --quiet           Suppress non-error output
  -d, --debug           Print debugging information
Use '--option=?' or '--option help' to see available suboptions
See man page for examples and full option syntax.

那么,相对于kvm镜像来说,有两种情况,第一是镜像文件已经安装了操作系统,也就是已经虚拟化完成了,第二种是仅仅有一个磁盘虚拟文件,在这个文件内并没有安装操作系统,并不能使用,这两种情况需要virt-install 命令进行启动或者安装。

第一、已经安装好了操作系统的磁盘映像文件

例如,OpenStack常用的测试文件cirros-0.3.4-x86_64-disk.img ,假设这个文件存放在了opt目录下,那么,启动这个kvm镜像的命令应该如下:

virt-install --name test01 --virt-type qemu --ram 1024 --accelerate  --disk path=/opt/cirros-0.3.4-x86_64-disk.img --network=default --force --import --autostart --vnc --vnclisten=0.0.0.0 --vncport=5922

以上命令,主要使用了 --name --virt-type --ram --accelerate --disk --import 这些参数,如果将--virt-type 改成kvm,此kvm镜像也可以启动。

那么,一个kvm镜像install的时候必定需要一个name,--ram ,disk,其实基本有这三个也就可以了,剩下的net和vnc仅仅是出于使用这个kvm虚拟机的目的而设置,毕竟kvm镜像启动后我们是需要使用的,对吧。上面的命令将会开启一个端口5922,这个端口并不是默认的5900端口。

--accelerate参数是加速

例如,这个命令也可以启动一个已存在安装过操作系统的镜像,如果有安装virt-viewer命令,将会直接打开一个kvm终端窗口。

virt-install --name test06 --ram 1024 --accelerate  --disk path=/opt/cirros-0.3.4-x86_64-disk.img --import --check all=off

--check all=off 这个参数是表示允许多次启动kvm镜像,以上命令中,name是test06,name应该是唯一的不重复的,仅此一个要求,因为前面我们使用了test1 2 3 4 5

第二、通过qemu-img 命令生成一个全新的磁盘镜像文件,并通过挂载操作系统安装包,开始一个全新安装kvm镜像。

说人话也就是全新的kvm镜像安装需要至少两个文件,一个是qemu-img命令生成的磁盘文件,一个是ISO操作系统安装文件。例如全新安装一个Windows的kvm镜像:

[root@centos1 opt]# virt-install --name win7 --os-variant=win7 --memory 512 --vcpus=1  --cpu host-model --vcpus 2 --disk path=/opt/win7.vmdk,size=30,bus=ide --accelerate --disk device=cdrom,path=/opt/cn_windows_7_ultimate_with_sp1_x64_dvd_618537\(1\).iso --disk device=cdrom,path=/opt/virtio-win-0.1.102.iso --vnc --vncport=5922 --vnclisten=0.0.0.0 --network=default --noautoconsole
Starting install...
Allocating 'win7.vmdk'                                                                                                         |  30 GB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

这里的提示就和启动一个已安装操作系统的kvm镜像不一样了,有提示:to complete the installation process.

安装Windows需要挂载两个cdrom,一个是系统安装ISO文件,一个是驱动文件,如果有要挂载成cdrom的文件,写的形式是:

--disk device=cdrom,path=/opt/virtio-win-0.1.102.iso

而硬盘文件是这样的:

--disk path=/opt/linux.img,size=30,format=qcow2,bus=ide

当然,Windows安装和linux安装是不一样的,因此,bus这里使用了ide,使用ide的原因是为了提高兼容性,并没有其它的意思,如果是安装linux系统的kvm镜像,可以不用指定bus,直接使用默认,默认为virtio,这里的bus是指的使用的驱动类型,而format=qcow2,是因为我们前面建立的kvm磁盘镜像格式是qcow2,如果忘记了也没关系,通过qemu-img info 命令可以查看到到底是哪种格式的磁盘文件:

[root@centos1 opt]# qemu-img info linux.img 
image: linux.img
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

关于kvm的磁盘文件格式,大体情况是这样的:

虚拟机常用镜像格式介绍

目前,虚拟机的主流镜像格式有raw、cow、qcow、qcow2以及vmdk,下面,我就详细介绍一下这些主流的虚拟机镜像格式。

(一)raw格式


raw格式是一种很早的镜像格式,格式比较原始、简单,性能上也还不错。并且raw格式镜像的一个突出好处是它支持转换成其他格式的镜像,或者作为其他格式镜像转换的中间格式。但是,raw格式的一个突出缺点就是不支持快照。 CentOS6的虚拟机KVM和XEN默认使用raw格式。

(二)cow、qcow和qcow2格式


cow、qcow和qcow2是另外的镜像格式,cow格式、qcow格式目前已经逐步被qcow2格式所取代,qcow2格式是目前的一种主流镜像格式,性能上与raw格式相差无几,但是支持虚拟机快照。CentOS7的KVM虚拟机镜像默认使用qcow2格式。

(三)vmdk格式


vmdk是Vmware虚拟的镜像格式,其整体性能非常出色,稳定性和其他方面的能力很好,也支持快照。

因此,--disk path=/opt/win7.img,size=30,format=qcow2,bus=ide这里必须要format=qcow2,如果不指定将会使用raw。

Linus的安装比较简单了,通常的安装命令为:

virt-install --virt-type kvm --name centos --ram 1024 --disk /tmp/centos.qcow2,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=centos7.0 --location=/root/CentOS-7-x86_64-DVD-1708.iso

以上为kvm的virt-install 命令的大概介绍,希望各位网友指出写的不足的地方。

目录
相关文章
|
1月前
|
数据安全/隐私保护 虚拟化 Windows
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
58 0
|
2天前
|
Linux KVM 数据库
虚拟机数据恢复—误删除KVM虚拟机的数据恢复案例
虚拟化数据恢复环境&故障: KVM是Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux2.6.20版本之后集成在Linux的各个主要发行版本中。KVM使用Linux自身的调度器进行管理。 本案例中的服务器操作系统为Linux,文件系统为EXT4。操作系统上的部署的几台KVM虚拟机被删除,每台KVM虚拟机包含一个qcow2格式的磁盘文件和一个raw格式的磁盘文件,用户需要恢复的数据是raw格式的磁盘文件。这几台被误删除的虚拟机存放的是数据库,程序代码等数据。
|
2天前
|
Ubuntu 数据安全/隐私保护
在UBUNTU虚拟机上安装R软件包
在UBUNTU虚拟机上安装R软件包
|
4天前
|
Ubuntu Linux 定位技术
手把手教你优雅的安装虚拟机 Ubuntu —— 图文并茂
手把手教你优雅的安装虚拟机 Ubuntu —— 图文并茂
|
30天前
如何在vm虚拟机里面安装win10操作系统
如何在vm虚拟机里面安装win10操作系统
11 0
|
30天前
|
关系型数据库 MySQL 数据库
虚拟机Linux-Centos系统网络配置常用命令+Docker 的常用命令
虚拟机Linux-Centos系统网络配置常用命令+Docker 的常用命令
34 0
|
1月前
|
Linux 虚拟化 数据安全/隐私保护
使用VMware安装linux虚拟机
使用VMware安装linux虚拟机
使用VMware安装linux虚拟机
|
1月前
|
Ubuntu 搜索推荐 数据挖掘
下载Ubantu镜像文件、创建虚拟机以及ubantu安装详细教程(系统性学习day1)
下载Ubantu镜像文件、创建虚拟机以及ubantu安装详细教程(系统性学习day1)

热门文章

最新文章