- KVM host đã được cài đặt sẵn sàng. Cài đặt Môi trường để đóng images
- Đã có file iso của CentOS7
- Sử dụng
Standard
với định dạngext4
cho phân vùng OS không sử dụng LVM. - Sử dụng công cụ virt-manager hoặc web-virt để kết nối tới console máy ảo
- Phiên bản OpenStack sử dụng là Queens
- Hướng dẫn bao gồm 2 phần chính: thực hiện trên máy ảo cài OS và thực hiện trên KVM Host
- Time zone VietNam
- Đăng nhập ssh vào Node KVM bật
virt-manager
virt-manager
- Trên Virt-manager thực hiện các step sau để cài OS
Click chuột phải vào QEMU/KVM
chọn Details
Click vào tab Storage
create một images mới
Set tên và dung lượng cho images này là với định dạng là .qcow2
Quay lại màn hình chính của Virt-manager chọn create VM mới
Chọn boot từ file ISO
Trỏ đường dẫn đến file ISO
Cấu hình RAM và CPU cho VM
Ở đây chúng ta sẽ chọn images centos6.qcow2
đã được tạo
Đặt tên cho VM, click vào Customize...
để bổ sung các cấu hình khác
Cấu hình cho CPU
Cấu hình boot, chỉnh lại menu boot để boot ISO cài đặt OS
Cấu hình lại file ISO
Cấu hình NIC mode virtio
và bắt đầu quá trình cài đặt.
Chọn Install CentOS7
để tiến hành cài đặt
Cấu hình ngôn ngữ chọn English(English)
Cấu hình timezone về Ho_Chi_Minh
Cấu hình disk để cài đặt
Chọn Standard Partition
cho ổ disk
Cấu hình mount point /
cho toàn bộ disk
Định dạng lại ext4
cho phân vùng
Kết thúc quá trình cấu hình disk
Confirm quá trình chia lại partition cho disk
Cấu hình network
Turn on network cho interface và set hostname
Kết thúc cấu hình, bắt đầu quá trình cài đặt OS
Setup passwd cho root
Reboot lại VM sau khi cài đặt hoàn tất
Tiến hành tắt máy ảo và xử lí một số bước sau trên KVM host:
- Chỉnh sửa file
.xml
của máy ảo, bổ sung chỉnh sửachannel
trong (Thường thì CentOS mặc định đã cấu hình sẵn phần này) mục đích để máy host giao tiếp với máy ảo sử dụng qemu-guest-agent
virsh edit centos7.0
với centos*
là tên máy ảo
...
<devices>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
</devices>
...
-
Bật máy ảo lên
-
Cài đặt epel-release & Update
yum install epel-release -y
yum update -y
- Stop firewalld Disable Selinux
systemctl disable firewalld
systemctl stop firewalld
sudo systemctl disable NetworkManager
sudo systemctl stop NetworkManager
sudo systemctl enable network
sudo systemctl start network
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
- Disable IPv6
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.conf
sysctl -p
- Update file
dhclient-script
yum install wget -y
rm -rf /usr/sbin/dhclient-script
wget https://raw.githubusercontent.com/uncelvel/create-images-openstack/master/scripts_all/dhclient-script -O /usr/sbin/dhclient-script
chmod +x /usr/sbin/dhclient-script
- Option ssh ipv4
sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g' /etc/ssh/sshd_config
systemctl restart sshd
- Cài đặt CMDlog
curl -Lso- https://raw.githubusercontent.com/nhanhoadocs/ghichep-cmdlog/master/cmdlog.sh | bash
- Cài đặt Chronyd
yum install chrony -y
sed -i 's|server 1.centos.pool.ntp.org iburst|server 103.101.161.201 iburst|g' /etc/chrony.conf
systemctl enable --now chronyd
hwclock --systohc
==> SNAPSHOT lại KVM host để lưu trữ và đóng gói lại khi cần thiết
- Shutdown VM
- Tiến hành truy cập tab
Snapshot
để snapshot
==> SNAPSHOT lại KVM host để lưu trữ và đóng gói lại khi cần thiết
- Start lại và ssh vào VM
ssh root@<ip_VM>
- Cài đặt acpid nhằm cho phép hypervisor có thể reboot hoặc shutdown instance.
yum install acpid -y
systemctl enable acpid
- Cài đặt qemu guest agent, kích hoạt và khởi động qemu-guest-agent service
yum install -y qemu-guest-agent
systemctl enable qemu-guest-agent.service
systemctl start qemu-guest-agent.service
- Cài đặt cloud-init và cloud-utils:
yum install -y cloud-init cloud-utils
Lưu ý:
Để sử sụng qemu-agent, phiên bản selinux phải > 3.12
rpm -qa | grep -i selinux-policy
Để có thể thay đổi password máy ảo thì phiên bản qemu-guest-agent phải >= 2.5.0
qemu-ga --version
- Cấu hình console
Để sử dụng nova console-log, bạn cần thay đổi option cho GRUB_CMDLINE_LINUX
và lưu lại
sed -i 's/GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"/GRUB_CMDLINE_LINUX="crashkernel=auto console=tty0 console=ttyS0,115200n8"/g' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
- Để máy ảo trên OpenStack có thể nhận được Cloud-init cần thay đổi cấu hình mặc định bằng cách sửa đổi file
/etc/cloud/cloud.cfg
.
sed -i 's/disable_root: 1/disable_root: 0/g' /etc/cloud/cloud.cfg
sed -i 's/ssh_pwauth: 0/ssh_pwauth: 1/g' /etc/cloud/cloud.cfg
sed -i 's/name: centos/name: root/g' /etc/cloud/cloud.cfg
- Disable Default routing
echo "NOZEROCONF=yes" >> /etc/sysconfig/network
- Xóa thông tin card mạng
rm -f /etc/sysconfig/network-scripts/ifcfg-eth0
- Để sau khi boot máy ảo, có thể nhận đủ các NIC gắn vào:
cat << EOF >> /etc/rc.local
for iface in \$(ip -o link | cut -d: -f2 | tr -d ' ' | grep ^eth)
do
test -f /etc/sysconfig/network-scripts/ifcfg-\$iface
if [ \$? -ne 0 ]
then
touch /etc/sysconfig/network-scripts/ifcfg-\$iface
echo -e "DEVICE=\$iface\nBOOTPROTO=dhcp\nONBOOT=yes" > /etc/sysconfig/network-scripts/ifcfg-\$iface
ifup \$iface
fi
done
EOF
- Thêm quyền thực thi cho file
/etc/rc.local
chmod +x /etc/rc.local
- Xóa file hostname
rm -f /etc/hostname
- Clean all
yum clean all
# Xóa last logged
rm -f /var/log/wtmp /var/log/btmp
# Xóa history
rm -f /root/.bash_history
> /var/log/cmdlog.log
history -c
- Tắt VM
poweroff
# Xóa bỏ MAC address details
virt-sysprep -d OPS_Template_CentOS7
# Giảm kích thước image
virt-sparsify --compress /var/lib/libvirt/images/OPS_Template_CentOS7.qcow2 CentOS7.qcow2
- Copy Images sang Node Controller
scp CentOS7.qcow2 root@<controller_host>:/root/
- Di chuyển image tới máy CTL, sử dụng câu lệnh sau
glance image-create --name CentOS7.qcow2 \
--disk-format qcow2 \
--container-format bare \
--file /root/CentOS7.qcow2 \
--visibility=public \
--property hw_qemu_guest_agent=yes \
--progress
- Image đã sẵn sàng để launch máy ảo.
Link tham khảo
http://openstack-xenserver.readthedocs.io/en/latest/24-create-kvm-centos-7-image.html