虚拟机的安装

本文详细介绍了使用图形界面和脚本两种方式安装虚拟机的步骤。通过virt-manager创建虚拟机,选择ISO镜像,配置内存、CPU和硬盘大小。安装过程中设置时区、语言、软件选择和分区。同时,提供了一键安装脚本,通过自动化命令行操作,包括挂载ISO,配置yum源,安装必要软件,设置自动应答脚本,配置DHCP和TFTP服务,实现无人值守安装。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

######安装虚拟机########

前面介绍,使用图形化界面一步一步来进行虚拟机安装

后面还有使用脚本进行一键安装


virt-manger  开启图形化管理界面


打开图形管理界面点击左上角亮灯的按钮


选择默认第一个选项 并点击 forward 下一步


点击browse 选择所要进行安装的iso镜像




点击browse loacl 在本地文件中进行寻找


找到后双击 进行确定 进入下一步


选择内存的大小和cpu的核数,点击forward


选择硬盘大小 点击forward


编写虚拟机名称,点击finish 完成创建

如下图,弹出安装虚拟机的界面,选择第一个选项 install Red Hat Enterprise Linux 7.2


选择在安装过程中所用的语言(注意是安装过程中,而不是虚拟机语言)


选择后点击Continue 进入下一步


此界面需要进行一系列操作来配置虚拟机


点击第一个DATE&TIME,选择时区为上海


点击LANGUAGE SUPPORT选择语言添加中文(想添加什么看你开心咯)


点击SOFTWEARE SELECTION 选择你要安装的虚拟机

左侧第一个为最简装机,没有图形

左侧最后一个是带图形装机


点击MANUAL  PARTITIONONG选择虚拟机的分区

如图选择Standard Partition 此为标准分区


点击左下方的+号 出现此界面 用以选择分区以及分配大小


如图,给/boot分配200M、swap分配500M(swap为内存在硬盘上的缓存空间,最好为内存的两倍,此为节省空间遂选择500)剩下的全部分给 / (根目录)


分区完成后选择Dnoe 出现下图界面,点击Accept Changes确认分区


所有配置完成后,点击Begin installnation进行下一步


如图,点击ROOT PASSWORD 设置超极用户密码


在下图中,填写超极用户的密码,点击两次Dnoe保存密码


然后进行漫长的等待,等待下载好所有的软件后,点击Reboot重启


完成上面一系列操作后,虚拟机就安装完成了



下面使用脚本进行一键安装,执行下面命令,你就只需要去喝茶了,等一会虚拟机就安装好了

#!/bin/bash
##要求:必须在mnt目录下有rhel-server-7.2-x86_64-dvd.iso文件

mkdir /iso
mv /mnt/rhel-server-7.2-x86_64-dvd.iso /iso
mkdir /mnt/rhel7.2
mount /iso/rhel-server-7.2-x86_64-dvd.iso /mnt/rhel7.2    ##挂载镜像
rm -rf /etc/yum.repos.d/*
touch /etc/yum.repos.d/yum.repo
cat > /etc/yum.repos.d/yum.repo <<EOF    ##编写配置文件
[rhel7.2]
name=rhel7.2
baseurl=file:///mnt/rhel7.2
gpgcheck=0
EOF
yum clean all 
yum install httpd.x86_64 -y  ##下载httpd
systemctl stop firewalld     ##关闭防火墙
systemctl disable firewalld   ##设置开机不启动防火墙
systemctl start httpd   ##开启httpd服务
systemctl enable httpd   ##设置开启启动
mkdir /var/www/html/rhel7.2
umount /mnt/rhel7.2      ##解除挂载
echo "mount /iso/rhel-server-7.2-x86_64-dvd.iso /var/www/html/rhel7.2" >>/etc/rc.d/rc.local    ##重新挂载到http发布位置
cat > /etc/yum.repos.d/yum.repo <<EOF   ##重新编写配置文件
[rhel7.2]
name=rhel7.2
baseurl=file:///var/www/html/rhel7.2
gpgcheck=0
EOF
yum clean all


createrepo /softwear           
cat >> /etc/yum.repos.d/yum.repo <<EOF    ##编写本地第三方软件yum仓库配置文件




[softwear]
name=softweat from internet
baseurl=file:///softwear
gpgcheck=0
EOF
yum clean all


yum install system-config-kickstart dhcp syslinux tftp-server -y     ##下载自动应答脚本制作工具
system-config-kickstart  ##save file ks.cfg in /var/www/html/       ##使用此工具建立文件保存到html目录下

cat > /etc/dhcp/dhcpd.conf <<EOF    ##编写dhcp配置文件
option domain-name "example.com";
option domain-name-servers 172.25.254.5;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 172.25.254.0 netmask 255.255.255.0 {
  range 172.25.254.50 172.25.254.80;
  option routers 172.25.254.5;
  next-server 172.25.254.5;
  filename "pxelinux.0";
}
EOF


systemctl restart dhcpd   ##重启dhcpd服务
systemctl stop firewalld  ##关闭防火墙


cat > /etc/xinetd.d/tftp  <<EOF    ##编写tftp配置文件
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes 
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
EOF


systemctl restart xinetd    ##重启服务


mkdir /var/lib/tftpboot/pxelinux.cfg
cp /var/www/html/rhel7.2/isolinux/* /var/lib/tftpboot                                        ##将一系列需要的文件复制到tftpboot目录下
cp /var/lib/tftpboot/isolinux.cfg   /var/lib/tftpboot/pxelinux.cfg/default
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/


cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF                   ##配置default文件,更改虚拟机安装过程的配置
default vesamenu.c32
timeout 60 ##等待时间1/10s


display boot.msg


# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title Red Hat Enterprise Linux 7.0 ##大标题
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13


# Border Area
menu color border * #00000000 #00000000 none


# Selected item
menu color sel 0 #ffffffff #00000000 none


# Title bar
menu color title 0 #ff7ba3d0 #00000000 none


# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none


# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none


# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none


# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none


# Help text
menu color help 0 #ffffffff #00000000 none


# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none


# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none


# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none


# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.


menu tabmsg Press Tab for full configuration options on menu items.


menu separator # insert an empty line
menu separator # insert an empty line


label linux
  menu label ^Install Red Hat Enterprise Linux 7.0 ##小标题
   menu default ##默认标题设定
  kernel vmlinuz
  append initrd=initrd.img repo=http://172.25.254.5/rhel7.2 ks=http://172.25.254.5/ks.cfg quiet                    ##安装源和ks


label check
  menu label Test this ^media & install Red Hat Enterprise Linux 7.0
  #menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 rd.live.check quiet


menu separator # insert an empty line


# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting


label vesa
  menu indent count 5
  menu label Install Red Hat Enterprise Linux 7.0 in ^basic graphics mode
  text help
Try this option out if you're having trouble installing
Red Hat Enterprise Linux 7.0.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 xdriver=vesa nomodeset quiet


label rescue
  menu indent count 5
  menu label ^Rescue a Red Hat Enterprise Linux system
  text help
If the system will not boot, this lets you access files
and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 rescue quiet


label memtest
  menu label Run a ^memory test
  text help
If your system is having issues, a problem with your
system's memory may be the cause. Use this utility to
see if the memory is working correctly.
  endtext
  kernel memtest


menu separator # insert an empty line


label local
  menu label Boot from ^local drive
  localboot 0xffff


menu separator # insert an empty line
menu separator # insert an empty line


label returntomain
  menu label Return to ^main menu
  menu exit


menu end
EOF
systemctl restart tftp     ##重启tftp服务


 




virt-install \      ##下载虚拟机
--name $1 \
--ram 1024 \
--file /var/lib/libvirt/images/$1.qcow2 \
--file-size 8 \
--location http://172.25.254.5/rhel7.2 \
--extra-args "ks=http://172.25.254.5/ks.cfg" &

























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值