CentOS 7 virt-install 命令行方式(非图形界面)安装KVM虚拟机

环境及网卡配置请参考

https://blog.youkuaiyun.com/mshxuyi/article/details/98305715

创建镜像目录

mkdir -p /home/vms

 virt-install 配置文件

virt-install \
--name=kvm107 --ram 1024 --vcpus=1 \
--disk path=/home/vms/kvm107.qcow2,size=10,format=qcow2,bus=virtio \
--location=/mnt/CentOS-7-x86_64-Minimal-1810.iso --network bridge=br0,model=virtio \
--graphics=none --console=pty,target_type=serial \
--extra-args="console=tty0 console=ttyS0"
  1. --graphics 指定是否开启图形

  2. --console 定义终端的属性,target_type 则是定义终端的类型

  3. --extra-args 定义终端额外的参数

开始安装

Starting installer, one moment...
anaconda 21.48.22.93-1 for CentOS Linux 7 started.
 * installation log files are stored in /tmp during the installation
 * shell is available on TTY2
 * when reporting a bug add logs from /tmp as separate text/plain attachments
09:40:52 Not asking for VNC because we don't have a network
================================================================================
================================================================================
Installation

 1) [x] Language settings                 2) [!] Time settings
        (English (United States))                (Timezone is not set.)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 输入 1 然后回车

[anaconda] 1:main* 2:shell  3:log  4:storage-lo> Switch tab: Alt+Tab | Help: F1 

这里选择 1 进入语言设置界面 

Available languages
 1)  Afrikaans             25)  Hindi                 48)  Oriya
 2)  Amharic               26)  Croatian              49)  Punjabi
 3)  Arabic                27)  Hungarian             50)  Polish
 4)  Assamese              28)  Interlingua           51)  Portuguese
 5)  Asturian              29)  Indonesian            52)  Romanian
 6)  Belarusian            30)  Icelandic             53)  Russian
 7)  Bulgarian             31)  Italian               54)  Sinhala
 8)  Bengali               32)  Japanese              55)  Slovak
 9)  Bosnian               33)  Georgian              56)  Slovenian
10)  Catalan               34)  Kazakh                57)  Albanian
11)  Czech                 35)  Kannada               58)  Serbian
12)  Welsh                 36)  Korean                59)  Swedish
13)  Danish                37)  Lithuanian            60)  Tamil
14)  German                38)  Latvian               61)  Telugu
15)  Greek                 39)  Maithili              62)  Tajik
16)  English               40)  Macedonian            63)  Thai
17)  Spanish               41)  Malayalam             64)  Turkish
18)  Estonian              42)  Marathi               65)  Ukrainian
19)  Basque                43)  Malay                 66)  Urdu
20)  Persian               44)  Norwegian Bokmål      67)  Vietnamese
21)  Finnish               45)  Nepali                68)  Chinese
22)  French                46)  Dutch                 69)  Zulu
Press ENTER to continue

这里回车后 选择 68 选择 Chinese 中文

Available locales
 1)  Simplified Chinese     3)  Traditional Chinese    4)  Simplified Chinese
     (China)                    (Hong Kong)                (Singapore)
 2)  Traditional Chinese
     (Republic of China)
Please select language support to install.

这里输入 1 选择 Simplified Chinese (china)简体中文 然后回车

Installation

 1) [x] Language settings                 2) [!] Time settings
        (Simplified Chinese (China))             (Timezone is not set.)
 3) [x] Installation source               4) [!] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]:

回车后重新进入主界面,接下来选择 2 进入 Time settings 时区设置

Time settings

Timezone: not set

NTP servers:not configured

 1)  Set timezone
 2)  Configure NTP servers
  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 

选择 1 设置时区

Available regions
 1)  Africa                 6)  Atlantic              10)  Pacific
 2)  America                7)  Australia             11)  US
 3)  Antarctica             8)  Europe                12)  Etc
 4)  Arctic                 9)  Indian
 5)  Asia
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]: 5

选择 5 Asia 亚洲

Available timezones in region Asia
 1)  Aden                  28)  Hovd                  54)  Pontianak
 2)  Almaty                29)  Irkutsk               55)  Pyongyang
 3)  Amman                 30)  Jakarta               56)  Qatar
 4)  Anadyr                31)  Jayapura              57)  Qyzylorda
 5)  Aqtau                 32)  Jerusalem             58)  Riyadh
 6)  Aqtobe                33)  Kabul                 59)  Sakhalin
 7)  Ashgabat              34)  Kamchatka             60)  Samarkand
 8)  Baghdad               35)  Karachi               61)  Seoul
 9)  Bahrain               36)  Kathmandu             62)  Shanghai
10)  Baku                  37)  Khandyga              63)  Singapore
11)  Bangkok               38)  Kolkata               64)  Srednekolymsk
12)  Barnaul               39)  Krasnoyarsk           65)  Taipei
13)  Beirut                40)  Kuala_Lumpur          66)  Tashkent
14)  Bishkek               41)  Kuching               67)  Tbilisi
15)  Brunei                42)  Kuwait                68)  Tehran
16)  Chita                 43)  Macau                 69)  Thimphu
17)  Choibalsan            44)  Magadan               70)  Tokyo
18)  Colombo               45)  Makassar              71)  Tomsk
19)  Damascus              46)  Manila                72)  Ulaanbaatar
20)  Dhaka                 47)  Muscat                73)  Urumqi
21)  Dili                  48)  Nicosia               74)  Ust-Nera
22)  Dubai                 49)  Novokuznetsk          75)  Vientiane
Press ENTER to continue

回车后 选择 62 Shanghai 上海 然后回车

 1) [x] Language settings                 2) [x] Time settings
        (Simplified Chinese (China))             (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

重新进入主界面,这时选择 5 进入系统安装位置

Probing storage...
Installation Destination

[x] 1) : 10 GiB (vda)

1 disk selected; 10 GiB capacity; 10 GiB free ...

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 

这里可以看到 前面创建的虚拟机 10G,输入 C 下一步

Autopartitioning Options

[ ] 1) Replace Existing Linux system(s)

[x] 2) Use All Space

[ ] 3) Use Free Space

Installation requires partitioning of your hard drive. Select what space to use
for the install target.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 

这里默认为 2,使用全部空间,输入 C 进入下一步

Partition Scheme Options

[ ] 1) Standard Partition

[ ] 2) Btrfs

[x] 3) LVM

[ ] 4) LVM Thin Provisioning

Select a partition scheme configuration.

  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 

这里默认为 3,使用LVM分区,输入 C 进入下一步

nstallation

 1) [x] Language settings                 2) [x] Time settings
        (Simplified Chinese (China))             (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [x] Installation Destination          6) [x] Kdump
        (Automatic partitioning                  (Kdump is enabled)
        selected)                         8) [!] Root password
 7) [ ] Network configuration                    (Password is not set.)
        (Not connected)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

重新返回主界面 ,输入 8 设置 Root 密码

Please select new root password. You will have to type it twice.

Password: 
Password (confirm): 
================================================================================
================================================================================
Question

     The password you have provided is weak: The password is shorter than 8
     characters.
     Would you like to use it anyway?

Please respond 'yes' or 'no': yes

输入 yes 确认,然后按 b 进入安装界面

Installing boot loader
.
Performing post-installation setup tasks
.

Configuring installed system
.
Writing network configuration
.
Creating users
.
Configuring addons
.
Generating initramfs
.
Running post-installation scripts
.
        Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA

        Installation complete.  Press return to quit

Installation complete 安装完成,按 回车 退出

接下来可以通过 virsh console 登陆GUEST虚拟机

#查看虚拟机
virsh list

 Id    Name                           State
----------------------------------------------------
 8     kvm107                         running

#通过字符界面登陆虚拟机
virsh console kvm107

输入虚拟机的账号跟密码 

Connected to domain kvm107
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

kvm107 login: root
password:
Last login: Tue Aug 20 21:50:57 from 192.168.0.103
[root@kvm107 ~]# 

 

### 如何使用 `virt-install` 命令创建 KVM 虚拟机 #### 创建虚拟机的基本命令结构 为了创建一个基于 KVM虚拟机,可以使用如下基本形式的 `virt-install` 命令: ```bash virt-install \ --name <vm_name> \ --memory <ram_size_in_MB> \ --vcpus=<number_of_vCPUs>,maxvcpus=<maximum_number_of_vCPUs> \ --disk path=<path_to_disk_image>,size=<disk_size_in_GB> \ --cdrom <path_to_iso_image> \ --network bridge=<bridge_interface> \ --graphics vnc,listen=0.0.0.0 \ --os-type linux \ --os-variant generic \ --console pty,target_type=serial \ --location <url_or_path_to_installation_media> ``` 上述命令中的参数可以根据实际需求调整[^2]。 #### 示例:创建名为 "my_vm" 的 CentOS 7 虚拟机 下面给出的是具体实例,该例子展示了怎样利用 ISO 文件来安装操作系统并设置初始硬件规格: ```bash virt-install \ --name my_vm \ --memory 2048 \ --vcpus 2 \ --disk size=10 \ --cdrom /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-2009.iso \ --network bridge=virbr0 \ --graphics vnc,listen=0.0.0.0 \ --os-type linux \ --os-variant centos7.0 ``` 这条命令将会启动一个新的虚拟机,并尝试连接到所提供的 ISO 映像来进行操作系统的安装过程。注意这里的路径 `/var/lib/libvirt/images/CentOS-7-x86_64-Minimal-2009.iso` 应替换为你自己的 ISO 文件位置。 #### 设置高级选项 对于更复杂的场景,还可以添加更多选项来自定义新 VM 的行为。例如: - **指定 Hypervisor 类型**: 如果需要明确指出所使用的 hypervisor 是哪种(比如 kvm),可以通过 `--virt-type=kvm` 来实现。 - **启用自动启动功能**: 若要使虚拟机随宿主机一起开机自启,可加入 `--autostart` 参数。 - **选择虚拟化模式**: 对于那些既支持全虚拟化又支持半虚拟化的环境来说,可以选择特定的一种方式运行 guest OS (`--hvm` 表示完全虚拟化而 `--paravirt` 则表示半虚拟化)[^3]。 #### 注意事项 在执行以上任何一条指令之前,请确保已经正确设置了网络桥接接口 virbr0 或者其他合适的网卡名称作为桥梁设备的一部分。此外,在某些环境中可能还需要额外配置防火墙规则以便允许 VNC 连接访问新建的虚拟机控制台窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值