上云无忧 > 文档中心 > 天翼云iStack云原生一体机实战教程 - centos虚拟机镜像制作
iStack云原生一体机
天翼云iStack云原生一体机实战教程 - centos虚拟机镜像制作

文档简介:
本文将介绍如何制作centos镜像并上传至镜像中心,同时使用该镜像创建虚拟机。 操作步骤: 用于制作镜像的宿主机环境,需安装 kvm 套件: yum install qemu-kvm qemu-img python-virtinst virt-install libvirt libvirt-python virt-manager libvirt-client bridge-utils -y yum install guestfish libguestfs-bash-completion libguestfs-winsupport libguestfs-tools -y #vncos yum group install "GNOME Desktop" -y
*产品来源:中国电信天翼云。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

本文将介绍如何制作centos镜像并上传至镜像中心,同时使用该镜像创建虚拟机。

操作步骤

用于制作镜像的宿主机环境,需安装 kvm 套件

yum install qemu-kvm qemu-img python-virtinst virt-install libvirt 
libvirt-python virt-manager libvirt-client bridge-utils -y
yum install guestfish libguestfs-bash-completion libguestfs-winsupport 
libguestfs-tools -y
#vncos
yum group install "GNOME Desktop" -y复制

使用云镜像文件制作的方法,如下步骤打开云镜像文件。

如果使用 iso 文件安装,可以按下文安装 iso。

例如获取的 centos 7.9 的云镜像文件为:CentOS-7-x86_64-GenericCloud-2009.qcow2

# /root/ /usr/local/src/ 
cp CentOS-7-x86_64-GenericCloud-2009.qcow2 /usr/local/src/centos79.qcow2
cd /usr/local/src/
#root
guestfish --rw -a ./centos79.qcow2
run
list-filesystems
mount /dev/sda1 /
vi /etc/shadow
#shellrootopenssl passwd -1 xxxpass
# xxxxxxxxxxxxshellshadowroot
#centos 6:
vi /etc/grub.conf #GRUB_CMDLINE_LINUX
console=tty0
vi /etc/shadow #root
vi /etc/cloud/cloud.cfg #disable_root0
quit #guestfish
chown qemu:qemu centos79.qcow2复制

创建虚机 xml 配置文件 vi centos79.xml

由于制作过程中,虚机需要能出外网 yum 下载包,因此可以配置使用 kvm 默认的 nat 网络或桥接网络。如果使用 nat 网络,先通过 virsh net-list ;virsh net-dumpxml xxx 查询确认下默认的 nat 虚拟网络名,然后虚机 xml 中的 source network='xxx' 配置该网络名。

<domain type='kvm'>
 <name>centos79</name>
 <memory unit='GiB'>2</memory>
 <vcpu placement='static'>1</vcpu>
 <os>
 <type arch='x86_64' machine='pc'>hvm</type>
 <boot dev='hd'/>
 </os>
 <devices>
 <emulator>/usr/libexec/qemu-kvm</emulator>
 <disk type='file' device='disk'>
 <driver name='qemu' type='qcow2'/>
 <source file='/usr/local/src/centos79.qcow2'/>
 <target dev='vda' bus='virtio'/>
 </disk>
 <interface type='network'>
 <source network='default'/>
 <model type='virtio'/>
 </interface>
 <channel type='unix'>
 <target type='virtio' name='org.qemu.guest_agent.0'/>
 </channel>
 <graphics type='vnc' port='25911' autoport='no' 
listen='0.0.0.0'> 
 <listen type='address' address='0.0.0.0'/> 
 </graphics>
 <console type='pty'>
 <source path='/dev/pts/7'/>
 <target type='virtio' port='1'/>
 </console>
 <serial type='pty'>
 <source path='/dev/pts/6'/>
 <target type='isa-serial' port='0'/>
 </serial>
 </devices>
</domain>复制

启动虚机

chown qemu:qemu cento79.xml
virsh define centos79.xml
virsh start centos79复制

通过 vnc 客户端比如 VNC Viewer,连接宿主机 ip 的 25911 端口,登录虚机。

centos7,8 使用前面更改的root密码登录;centos 6 直接免密登录,但登录后首先执行 passwd root 修改root密码。

如果使用 iso,需如下安装系统。

例如 iso 文件存放在 /root 下,创建系统盘:

注意系统盘尽量 8g 以下,避免 virtual size 太大导致创建虚机过程耗时久。

qemu-img create -f qcow2 /usr/local/src/centos79.qcow2 8G
chown qemu:qemu /usr/local/src/centos79.qcow2
chown qemu:qemu /usr/local/src/CentOSxxxxxx.iso复制

创建虚机 xml 配置文件 vi centos79.xml

由于制作过程中,虚机需要能出外网 yum 下载包,因此可以配置使用 kvm 默认的 nat 网络或桥接网络。如果使用 nat 网络,先通过 virsh net-list ;virsh net-dumpxml xxx 查询确认下默认的 nat 虚拟网络名,然后虚机 xml 中的 source network='xxx' 配置该网络名。

<domain type='kvm'>
 <name>centos79</name>
 <memory unit='GiB'>2</memory>
 <vcpu>1</vcpu>
 <os>
 <type arch='x86_64'>hvm</type>
 <boot dev='cdrom'/>
 <boot dev='hd'/>
 </os>
 <clock sync="localtime"/>
 <devices>
 <emulator>/usr/libexec/qemu-kvm</emulator>
 <disk type='file' device='disk'>
 <driver name='qemu' type='qcow2' cache='none'/>
 <source file='/usr/local/src/centos79.qcow2'/>
 <target dev='hda' bus='ide'/>
 </disk>
 <disk type='file' device='cdrom'>
 <source file='/usr/local/src/CentOSxxxxxx.iso'/>
 <target dev='hdb' bus='ide'/>
 <readonly/>
 </disk>
 <interface type='network'>
 <source network='default'/>
 </interface>
 <channel type='unix'>
 <source mode='bind' path='/var/lib/libvirt/qemu/org.qemu.
guest_agent.0'/>
 <target type='virtio' name='org.qemu.guest_agent.0'/>
 <address type='virtio-serial' controller='0' bus='0' port='1'/>
 </channel>
 <graphics type='vnc' port='25911' autoport='no' 
listen='0.0.0.0'> 
 <listen type='address' address='0.0.0.0'/> 
 </graphics>
 </devices>
</domain>复制

启动虚机

chown qemu:qemu centos79.xml
virsh define centos79.xml
virsh start centos79复制

通过 vnc 客户端比如 VNC Viewer,连接宿主机 ip 的 25911 端口,打开进行安装,安装注意事项如下:

特别注意:安全需要,过程不能创建普通用户,root密码必须根据部门安全要求设置复杂密码。

  • 系统语言需选择英文,否则会导致 cloudinit-utils-growpart 初始化时无法自动扩系统盘根分区。
  • 时区需要选择国内,比如Asia/shanghai;kdump 需要开启。
  • 勾选 dhcp 获取 ip,后续安装完成后,可优先通过 ssh 连到虚机,方便复制后续操作命令。
  • 软件包选项中,使用 minimal 最小化安装,将“Development Tools”开发包组,以及“Compatibility Libraries”兼容包组,勾选上。
  • 主机名选项中,按默认 localhost.localdomain。
  • 建立分区选项中,使用“standard partition”标准分区,不使用 lvm,并删除 swap 分区,保证根分区在整个系统盘的末尾分区,这样后续才能使用 cloud-init growparts 去自动扩容根分区。
  • 格式化,高版本的系统比如 7.9 等,可以使用 xfs,低版本比如 centos 7.1,建议用 ext4,否则 cloudinit 扩盘可能不生效。(或者所有版本都统一用 ext4)

安装完成后,首先确认弹出 cdrom 的 iso 镜像,点界面的重启看能否自动弹出(一般自动弹出),若不行,可以如下手动弹出磁盘,再重启vm:

virsh detach-disk --type cdrom --mode readonly centos79 hdb # virsh 
edit centos79iso
virsh reboot centos79 # virsh 
destroy centos79 virsh start centos79复制

检查内核是否支持 Virtio 网卡、磁盘驱动

若缺少,需要重新编译内核。一般 linux 发行版大都支持。

grep -i virtio /boot/config-$(uname -r)
lsinitrd /boot/initramfs-$(uname -r).img | grep virtio复制

删除弱密码用户,调整时区、检查主机名,调整软件源,启用 networkmanageruserdel -r user #cat /etc

userdel -r user #cat /etc
/passwd |grep bash root
# utf8
locale
echo $LANG
#utf8 localectl set-locale LANG=en_US.utf8 
timedatectl set-timezone Asia/Shanghai #centos78 
cp -i /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #centos6 
hostnamectl status #centos78 localhost.localdomain
cat /etc/sysconfig/network #centos6 localhost.localdomain
# centos6localdomainlocalhost.localdomain.localdomainreboot
#centos7 epel
yum install wget -y
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-
7.repo
#centos6 minorver6.10
minorver=6.10
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
 -e "s|^#baseurl=http://mirror.centos.
org/centos/\$releasever|baseurl=https://mirrors.aliyun.com/centos-vault
/$minorver|g" \
 -i.bak \
 /etc/yum.repos.d/CentOS-*.repo
yum install wget -y
#centos 6 epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epelarchive-6.repo
#centos8 minorver8.2.2004
minorver=8.2.2004
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" \
 -e "s|^#baseurl=http://mirror.centos.
org/\$contentdir/\$releasever|baseurl=https://mirrors.aliyun.com/centosvault/$minorver|g" \
 -i.bak \
 /etc/yum.repos.d/CentOS-*.repo
yum install wget -y
#centos 8 epel
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.
noarch.rpm
sed -i 's|^#baseurl=https://download.
example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
#centos78 networkmanager
yum install NetworkManager -y
systemctl start NetworkManager && systemctl enable NetworkManager && 
systemctl status NetworkManager复制

安装 acpid 电源管理包,禁用 zeroconf 路由,安装 qemu-guest-agent,配置 console 控制台输出

#cloud-initzeroconflinux169.254.0.0
echo "NOZEROCONF=yes" >>/etc/sysconfig/network
#centos 78 acpid
yum install acpid -y
systemctl enable acpid
#centos 6 acpid
yum install acpid -y
/etc/init.d/acpid start
chkconfig acpid on
#centos 78 qemu-ga
yum install qemu-guest-agent -y
systemctl enable qemu-guest-agent
#centos 6 qemu-ga
yum install qemu-guest-agent -y
/etc/init.d/qemu-ga start
chkconfig qemu-ga on
#centos78 console
cat /boot/grub2/grub.cfg #
vi /etc/default/grub 
GRUB_CMDLINE_LINUXrhgb quiet console=tty0 console=ttyS0,115200n8
grub2-mkconfig -o /boot/grub2/grub.cfg
# arm64 efi grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
reboot
cat /proc/cmdline #
#centos 6 console
 console=tty0 console=ttyS0,115200
grub-install /dev/vda #grub
#
yum install unzip zip man parted -y
yum install usbutils gdisk parted ntfs-3g ntfs-3g-devel -y # usb 
yum install numactl numactl-libs numactl-devel -y
yum group install "Compatibility Libraries" -y
yum group install "Development Tools" -y
sed -i 's/=enforcing/=permissive/' /etc/selinux/config
setenforce 0
yum install curl wget -y
yum install net-tools traceroute lsof tcpdump iotop -y
yum install bash-completion -y
yum install pciutils -y # gpu 
update-pciids
systemctl start serial-getty@ttyS0.service
systemctl enable serial-getty@ttyS0.service
systemctl status serial-getty@ttyS0.service复制

修改网络配置

#centos 78 
vi ifcfg-eth0 #
TYPE=Ethernet
BOOTPROTO=dhcp
DEVICE=eth0
NAME=eth0
ONBOOT=yes
USERCTL=yes
PEERDNS=yes
IPV6INIT=yes
PERSISTENT_DHCLIENT="1"
cat /boot/grub2/grub.cfg #net.ifnames=0 biosdevname=0iso 
vi /etc/default/grub #GRUB_CMDLINE_LINUX 
net.ifnames=0 biosdevname=0
grub2-mkconfig -o /boot/grub2/grub.cfg
# arm64 efi grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
reboot
cat /proc/cmdline #
#centos 6 
rm -f /etc/udev/rules.d/70-persistent-net.rules
PRIMARY_INTERFACE=$(ip route list match 0.0.0.0 | awk 'NR==1 {print 
$5}')
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-$PRIMARY_INTERFACE
sed -i '/HWADDR/d' /etc/sysconfig/network-scripts
/ifcfg-$PRIMARY_INTERFACE
/etc/init.d/network restart复制

配置 cloud-init

#centos 78 
yum install cloud-init -y
yum install cloud-utils cloud-utils-growpart -y
sed -i "s/disable_root.*/disable_root\: 0/;s/ssh_pwauth.*/ssh_pwauth\: 0
/" /etc/cloud/cloud.cfg #rootssh
vi /etc/cloud/cloud.cfg #
systemctl enable cloud-init
systemctl list-unit-files |grep -i cloud #cloudinitenable
# centos 6 
yum install cloud-init cloud-utils cloud-utils-growpart -y
yum install dracut-kernel dracut dracut-modules-growroot -y
sed -i "s/disable_root.*/disable_root\:0/;s/ssh_pwauth.*/ssh_pwauth\: 0
/" /etc/cloud/cloud.cfg
vi /etc/cloud/cloud.cfg #
chkconfig --list |grep -i cloud #cloudiniton
dracut -f复制

安全加固

基于安全团队提供的基线,根据实际情况有调整,没有全部使用

#firewalld 
yum install firewalld -y
systemctl enable firewalld
systemctl start firewalld
firewall-cmd --list-all
vi /etc/login.defs #908
PASS_MAX_DAYS 90
PASS_MIN_LEN 8
PASS_WARN_AGE 10
vi /etc/security/pwquality.conf #3
minclass = 3
vi /etc/pam.d/su #wheelsu
auth required pam_wheel.so use_uid
usermod -L nobody #usermod -U nobodycentos 8nobody
usermod -L ftp
echo "export TMOUT=600"> /etc/profile.d/timeout.sh #10
systemctl enable rsyslog
vi /etc/logrotate.conf #10
rotate 10
chkconfig --list |egrep "telnet|sendmail|klogin|kshell|ntalk|tftp"
systemctl list-unit-files | egrep 
"telnet|sendmail|klogin|kshell|ntalk|tftp"
 openjdk11
rpm -qa |grep jdk复制

检查版本,清理系统

cat /etc/centos-release #release
# centos 7 
rm -rf /tmp/*
rm -f /etc/udev/rules.d/70-persistent-net.rules
rm -f /var/log/wtmp /var/log/btmp /var/log/anaconda/*
cd /var/log
for i in cron dmesg dmesg.old lastlog maillog messages pm-powersave.log 
secure Xorg.0.log Xorg.0.log.old Xorg.9.log Xorg.9.log.old
do
true > $i
done
dmesg -c
echo >/root/.bash_history
# centos 6 
rm -rf /tmp/*
rm -f /var/log/wtmp /var/log/btmp /var/log/anaconda/*
cd /var/log
for i in cron dmesg dmesg.old lastlog maillog messages pm-powersave.log 
secure Xorg.0.log Xorg.0.log.old Xorg.9.log Xorg.9.log.old
do
true > $i
done
dmesg -c
echo > /root/.bash_history
rm -f /etc/udev/rules.d/70-persistent-net.rules
PRIMARY_INTERFACE=$(ip route list match 0.0.0.0 | awk 'NR==1 {print 
$5}')
sed -i '/UUID/d' /etc/sysconfig/network-scripts/ifcfg-$PRIMARY_INTERFACE
sed -i '/HWADDR/d' /etc/sysconfig/network-scripts
/ifcfg-$PRIMARY_INTERFACE
# cloudinit
yum clean all
cloud-init clean # cloud-init
rm -rf /var/lib/cloud
history -c #复制

关机,压缩镜像

#
halt -p
#
virt-sysprep -a centos79.qcow2 
# yum install libguestfs-tools -y 
#
qemu-img convert -p -c -O qcow2 centos79.qcow2 centos79-compress.qcow2
#
qemu-img check centos79-compress.qcow2复制

以上完成后,centos79-compress.qcow2 即为可用

上传镜像

路径:登录iStack云管平台->xx工作区->/运营平台

使用说明:

  1. 选择【运维管理】导航栏,点击【云存储】二级菜单,选择三级菜单【集群】,进入具体的集群。
  2. 点击【虚拟机镜像】,选择【私有镜像】,点击【上传镜像】,这时就可以上传以上制作好的镜像了。
  3. 在云产品处开通虚拟机的时候则可以选择对应的镜像进行虚拟机的开通了。

相似文档
  • iStack云原生一体机常用文档下载。 iStack云原生一体机技术白皮书.pdf
  • iStack云原生一体机产品类常见问题。 Q:客户数据中心的iStack云原生一体机物理安全由谁负责? A:客户负责数据中心机房的风、火、水、电等安全。 Q:天翼云如何维护iStack云原生一体机基础设施? A:当iStack云原生一体机连入到公共云,并可在公共云控制台进行资源管控时,天翼云将把它作为公共云的一部分进行监控。如果需要执行物理维护,天翼云将会预约上门时间进行硬件维护。
  • iStack云原生一体机计费类常见问题。 Q:iStack云原生一体机的计费模式是什么? A:iStack云原生一体机提供租赁服务,最短3年租赁期,可增购2年维保服务,共5年维护期;到期后,设备回收处理。 产品按照不同的规格型号进行收费,标准产品出账模式为元/年 元/3年;维保服务出账模式为元/年。
  • iStack云原生一体机购买类常见问题。 Q:iStack云原生一体机如何选择? A:请按照客户需求资源量选择不同的规格,详细选购方式请见这里。 Q:iStack云原生一体机如何下单? A:请在BCP选择对应的型号进行下单,详细下单方式请见这里。
  • iStack云原生一体机操作类常见问题。 Q:如何登录iStack云原生一体机管理平台? A:请获取登录地址和账号后登录,详细操作方式请见这里。 Q:如何进行租户管理? A:请登录iStack云原生一体机管理平台,对租户进行增删改查,详细操作方式请见这里。
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部