1. 程式人生 > >kvm的支持openstack的linux系統鏡像制作

kvm的支持openstack的linux系統鏡像制作

back cos wpar can 掛載 main strong cti iso鏡像

1.1 創建虛擬機
qemu-img create -f qcow2 /home/rhel7.0.qcow2 20G //創建20G動態磁盤
//root權限執行
virt-install --os-variant list
//rhel7 : Red Hat Enterprise Linux 7
virt-install --connect=qemu:///system --name rhel7.0 --ram 4096 --vcpus 4 --network network=default --disk path=/home/rhel7.0.qcow2,format=qcow2,device=disk,bus=virtio,cache=writeback --cdrom /home/liutuan/iso/xxx.iso --vnc --os-type linux --os-variant rhel7
// Domain installation still in progress. You can reconnect to
the console to complete the installation process. //通過Virtual Machine Manager去連接操作系統安裝界面

1.2 操作系統安裝過程
圖形界面安裝、新增test登錄用戶,
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片
技術分享圖片

1.3 操作系統配置
#selinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
#iptables //關閉防火墻

systemctl stop iptables.service
systemctl disable iptables.service

1.4 qemu-guest-agent安裝和配置
#需要外部安裝源 @updates
yum install qemu-guest-agent

echo "NOZEROCONF=yes" >> /etc/sysconfig/network

1.5 配置虛擬機163 yum源
1.5.1 給虛擬機配置互聯網IP

宿主機和虛擬機對應的網卡配置:
技術分享圖片
技術分享圖片
1.5.2 配置yum源
vim /etc/sysconfig/network-scripts/ifcfg-eth0

修改eth0配置文件:留四行,刪除各種uuid
#網絡IP
[root@localhost network-scripts]# cat ifcfg-eth0
NAME=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp
vim /etc/resolv.conf
#MAC
Nameserver 114.114.114.114
重啟網絡
ping baidu.com
rm -f /etc/yum.repos.d/*
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
vim /etc/yum.repos.d/CentOS7-Base-163.repo
:%s/$releasever/7/g
:x
yum clean all
yum makecache
yum list

1.6 cloud-init安裝和配置
#調通互聯網,最後別忘了刪除網絡配置。
//針對RHEL-7
yum install cloud-init
yum install cloud-utils-growpart

vim /etc/cloud/cloud.cfg
users:

  • default

disable_root: 0
ssh_pwauth: 0

locale_configfile: /etc/sysconfig/i18n
mount_default_fields: [~, ~, ‘auto‘, ‘defaults,nofail‘, ‘0‘, ‘2‘]
resize_rootfs_tmp: /dev
ssh_deletekeys: 0
ssh_genkeytypes: ~
syslog_fix_perms: ~

cloud_init_modules:

  • migrator
  • bootcmd
  • write-files
  • growpart
  • resizefs
  • set_hostname
  • update_hostname
  • update_etc_hosts
  • rsyslog
  • users-groups
  • ssh

cloud_config_modules:

  • mounts
  • locale
  • set-passwords
  • yum-add-repo
  • package-update-upgrade-install
  • timezone
  • puppet
  • chef
  • salt-minion
  • mcollective
  • disable-ec2-metadata
  • runcmd

cloud_final_modules:

  • rightscale_userdata
  • scripts-per-once
  • scripts-per-boot
  • scripts-per-instance
  • scripts-user
  • ssh-authkey-fingerprints
  • keys-to-console
  • phone-home
  • final-message

system_info:
default_user:
name: centos
lock_passwd: true
gecos: Cloud User
groups: [wheel, adm]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
distro: rhel
paths:
cloud_dir: /var/lib/cloud
templates_dir: /etc/cloud/templates
ssh_svcname: sshd
datasource_list: [‘OpenStack‘,‘ConfigDrive‘]
_# vim:syntax=yaml
//If there is a defaults.cfg in /etc/cloud/cloud.cfg.d/, delete it.

#修改cloud-init配置文件:
Vim /etc/default/grub
#刪除GRUB_CMDLINE_LINUX=……這一行中的rhgb quiet配置項,添加console=tty0 console=ttyS0,115200n8
#使修改後的grub配置文件生效
Grub2-mkconfig –o /boot/grub2/grub.cfg
3.7 個性化配置和清理多余設置
個性化配置:略
清理多余的設置:
比如:DNS配置,磁盤分割,修改密碼等。
vim /etc/resolv.conf
#MAC
Nameserver 114.114.114.114

#root密碼
Passwd root
password

#管理開關虛擬機服務(可選)
yum install acpid
yum enable acpid

#配置硬盤重新分區(可選)
yum install cloud-utils-growpart

#配置本地YUM源,註意是否需要保留本地ISO源
在宿主機環境下掛載本地iso鏡像
virsh destroy rhel7.0
virsh start rhel7.0
virsh attach-disk --type cdrom --mode readonly rhel7.0 "/home/liutuan/iso/xx7.0.iso" hdc

virsh dumpxml rhel7.0 > rhel7.0.xml //方便virsh create *.xml
#進入虛擬機操作系統
#掛載本地iso

mkdir -p /mnt/cdrom /mnt/iso
mount /dev/cdrom /mnt/iso //如果出錯,則需要先attach-disk
cp –frv /mnt/iso /mnt/cdrom
rm –rf /mnt/iso
ls /mnt/cdrom

#配置本地yum源
cd /etc/yum.repos.d/
mv CentOS7-Base-163.repo CentOS7-Base-163.repo.bak
cat > /etc/yum.repos.d/local.repo <<EOF
[local]
name=local
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOF

Kill –s 9 xx(進程號)
yum clean all
yum makecache
yum repolist

#關機
shutdown

1.8 解除綁定
#安裝工具軟件
yum install -y libguestfs-tools-c
//ubuntu主機sudo apt-get install libguestfs-tools
//使用VM名稱
virt-sysprep -d rhel7.0
virsh undefine rhel7.0
#鏡像制作完畢
md5sum rhel7.0.qcow2 > Rrhel7.0.qcow2.md5sum
cat rhel7.0.qcow2.md5sum
781ecc51dde344ffba0e04a7574e6190 rhel7.0.qcow2

1.9 鏡像上傳

  1. scp rhel7.0.qcow2 controller:/home/ //也可以通過ftp工具上傳到控制節點
  2. ssh controller 上傳鏡像img到控制節點鏡像壓縮
    qemu-img convert -f qcow2 –O raw rhel7.0.qcow2 rhel7.0.raw
    1.9.1 上傳到glance

source openrc
glance image-create --property os_type=linux --name " rhel7.0" --is-public true --disk-format raw --container-format bare --file /home/rhel7.0.raw

kvm的支持openstack的linux系統鏡像制作