1. Create a centos image

Preparations:

Qemu-common qemu-img and qemu-KVM qemu-system must be installed first

yum install qemu-common qemu-img qemu-kvm qemu-system -y
Copy the code

1, dd an image file:

qemu-img create -f raw centos63_server.img 30G
Copy the code

2. Install Centos

Open VNC access with the -vnc parameter, so you can log in to this page from another machine to install the system:

/usr/libexec/qemu-kvm -m 16024 -cdrom centos6.3_x86_64.iso -drive file=centos63_server.img,index=0 -boot d -net nic -net user -nographic -vnc  :2  
Copy the code

3. VNC connection installation

/usr/libexec/qemu-kvm -m 16024 -drive file=centos63_server.img,index=0,boot=on -boot c -net nic -net user -nographic -vnc :3
# VNC connection:Vncveiwers 10.28.1.96:3If you do not restart, use the following command to start the newly installed vm image centos.img
Failed to find romfile pxe-rtf8139.bin 错 错 failed to find romfile pxe-rtf8139.bin 错 错
yum install kvm-pxe
Copy the code

4. Convert the image and install the package

Mount the image file, install required software, and generate an ext4 file system:

losetup -f centos63_server.img
losetup -a
fdisk -cul /dev/loop0
2048×512 = 1048576
losetup -d /dev/loop0
mkdir hh && mount -o loop,offset=1048576  centos63_server.img  hh
# or
losetup -f -o 1048576 centos63_server.img


Convert it to ext4 format
losetup -a # Check which /dev/loop is mounted: Do the following
dd  if=/dev/loop0 of=cenos63_x86.img
losetup -d /dev/loop0
mount -o loop centos63_x86.img hh
Copy the code

5. Install the software and make necessary configurations

    1. Software installation:
cd hh. && chroot .
yum install gcc gcc-c   gcc-c++ make automake autoconfig libgcc gcc* glibc* rpmbuild unzip bunzip2 qpid openssh-server cloud-init rsync tree kde
Copy the code
    1. Delete the network rule file. Otherwise, the centos VM cannot create virtual NIC eth0

/etc/udev/rules.d /etc/udev/rules.d /etc/udev/rules.d

-rw-r--r--. 1 root root 316 Dec  8 04:56 60-raw.rules
-rw-r--r--. 1 root root  53 Dec  8 07:38 91-drm-modeset.rules
Copy the code

All the others are deleted

Check whether the root node of /etc/fstab is mounted with a UUID. If yes, change the value to LABEL=/

    1. Configuration ifcfg – eth0

Enable DHCP by specifying or deleting the line #HWADDR= in network interface configuration:

# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
Copy the code
    1. Inject an SSH key so that no password is required for login (you can skip this step)

SSH -i mykey.priv root@host to log in to the vm instance without a password. Add the following to /etc/rc.local:

# vi /etc/rc.local
mkdir -p /root/.ssh
echo> > / root /. SSH/known_hosts curl - m 10 - s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key | grep'ssh-rsa'> > / root /. SSH/known_hosts curl - m 10 - s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key | grep SSH - rsa  >> /root/.ssh/known_hosts chmod 700 ~/.ssh chmod 600 ~/.ssh/known_hostsecho "AUTHORIZED_KEYS:"
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"
cat /root/.ssh/authorized_keys
echo "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"
Copy the code
    1. Open selinux and iptables

SELINUX needs to be enabled, otherwise it will not work even if the above SSH Settings allow root to log in without a password:

# vi /etc/selinux/config
SELINUX=enabled
SELINUXTYPE=targeted
Copy the code

Run the chkconfig iptables on command to open the iptables service

    1. Make a startup file:

Inject virtio’s packages into the image to improve the performance of instances

mkinitrd --with virtio_pci --with virtio_ring --with virtio_blk --with virtio_net --with virtio_balloon --with virtio -f  /boot/initrd-$(uname -r).img $(uname -r)Copy the code

Unmount and push out the chroot

exitImg /opt/centos_img/ centos6.7_img/cp boot/vmlinuz-2.6.32-504.el6.x86_64 / opt/centos_img centos6.7 _img /cd ..
umount  hh 或者losetup -d /dev/loop0
Copy the code
    1. Check whether the mirror is qualified

Verify that the root Label of the image file is consistent

tune2fs  -L  cenos67_x86.img
Copy the code
    1. Upload the image and start the VM

Upload the image to Glance and start the VM. Go to /opt/centos_img/ centos6.7_img /

[root @ node54 centos6.7 _img]# glance image-create --name centos6_7_ramdisk --disk-format ari --container-format ari --image-type VM --is-public True < initrd - 2.6.32-504. The el6. X86_64. Img
Added new image with ID: 7

[root@node54 centos6.7_img]# glance image-create --name centos6_7_kernel --disk-format aki --container-format aki --image-type VM --is-public True < called - 2.6.32-504. The el6. X86_64
Added new image with ID: 8

[root@node54 centos6.7_img]# glance image-create --name Centos6_7 --disk-format ami --container-format ami --is-public True --property ramdisk-id=7  --property kernel-id=8 < cenos67_x86.img

Copy the code

2: Windows Image:

Windows2008

Create a 10GB image file using qemu-img (qemu-img) :

qemu-img create -f qcow2 win2008.img 10G
Copy the code

Since Windows does not have a default virtio driver, download it first:

Wget HTTP: / / http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/bin/virtio-win-0.1-22.isoCopy the code

Use VNC to access the installation. During the installation, you need to select the hard disk driver. After the installation, stop the VM and restart it with the following command:

qemu-kvm -m 1024 -drive file=/home/win7/win2008_server.qcow2,if= virtio, boot = on - drive file = / home/Windows 7 / virtio - win - 0.1-22. Iso, index = 1, media = cdrom drive File = /home/cn_win7_sp1_x86_33in1_V1.2. iso,index=2,media= cdrom-net nic,model= virtio-net user-boot c-nographic VNC :9Copy the code

Window 2003:

/usr/libexec/qemu-kvm -m 512 -boot d -drive file=/xxx/win2k3.qcow2,cache=writeback,if=virtio,boot= on-drive file=/home/win7/virtio-win-0.1-22.iso,-fda/XXX/virtio-win-1.1.16.vfd-cdrom /xxx/cn_win_srv_2003_r2_standard_with_sp2_cd1_X13-13927.iso --drive file -vnc :1Copy the code

Cache =yes is faster

qemu-kvm -m 4096 -drive file=windows2008.qcow2,if= virtio, cache = none, the boot = on - drive file = / home/virtio - win - 0.1-22. Iso, index = 1, media = cdrom drive file=/home/windows_2008.iso,index=2,media=cdrom -net nic,model=virtio -net user -boot c -nographic -vnc :9Copy the code

After you publish the Windows image and access it using VNC, a message is displayed indicating that the virtio NIC driver has been automatically installed. Use Glance to add the image in qcow2 format

glance add  name="win2008" is_public=true container_format=ovf  disk_format=qcow2 < win2008.img

Copy the code

The last step: After installing Windows, you can perform some necessary customization, such as closing the firewall, opening RDP access, and installing some necessary software.