Example: CentOS Stream image¶
This example shows you how to install a CentOS Stream image and focuses mainly on CentOS Stream 9. Because the CentOS installation process might differ across versions, the installation steps might differ if you use a different version of CentOS.
Catatan
This is just an example, adjust paths and commands according to your environment
Download sebuah file ISO install CentOS¶
Navigate to the CentOS mirrors page.
Choose one of the mirrors and navigate to
9-stream/BaseOS/x86_64/iso
. Download a ISO, chooseboot
to download packages during install, otherwise choosedvd
Memulai proses instalasi¶
Start the installation process using either the virt-manager or the virt-install command as described in the Tools: libvirt and virsh/virt-manager page.
virt-install¶
If you use the virt-install command, do not forget to connect your VNC client to the virtual machine.
The command should look something like this:
$ sudo virt-install --virt-type kvm --name my-centos --ram 2048 \
--network network=default \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--os-type=linux --os-variant=centos-stream9 \
--location=~/Downloads/CentOS-Stream-9-20240819.0-x86_64-dvd1.iso
virt-manager¶
Catatan
See here for libvirt wiki about new VM creation in virt-manager
When creating a new VM from the downloaded ISO, virt-manager should automatically detect the OS. If it fails, manually select CentOS Stream as OS. Default settings should be fine.
Langkah keseluruhan instalasi¶
Catatan
This guide focuses on the steps specific to create a OpenStack image, for a general overview of installation process see CentOS Documentation
In Installation Summary
follow the instructions below.
DHCP and hostname¶
In Network & Host Name
ensure that Ethernet
is on and in
Configure.../IPv4 Settings
the Method
is set to Automatic (DHCP)
.
The same page allows for host name selection - leave it to default as the
cloud-init
package will be installed later.
Pilih opsi instalasi¶
In Software Selection
choose what to install, the default is Server with
GUI
, the smallest choice is Minimal Install
.
Create a working user¶
Configure a root password in Root Password
, as this will be needed later to
finalize the installation. By default it will be then blocked by cloud-init. It
is also possible to create an adiministrator user, as it will be later deleted
by virt-sysprep.
Lepaskan CD-ROM dan reboot¶
virt-install
Untuk mengeluarkan disk dengan menggunakan perintah virsh, libvirt mengharuskan Anda memasang disk kosong pada target yang sama dengan CD-ROM yang sebelumnya terpasang, yang mungkin adalah hda
. Anda bisa mengkonfirmasi target yang sesuai dengan perintah virsh dumpxml vm-image.
$ sudo virsh dumpxml my-centos
<domain type='kvm' id='19'>
<name>centos</name>
...
<disk type='block' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hda' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
...
</domain>
Run the following commands from the host to eject the disk
and reboot using virsh
, as root.
$ sudo virsh attach-disk --type cdrom --mode readonly my-centos "" hda
$ sudo virsh reboot my-centos
virt-manager
If you are using virt-manager
, the commands above will work, but you can
also use the GUI to detach and reboot.
Finalize installation¶
Menginstal layanan ACPI¶
Untuk mengaktifkan hypervisor untuk reboot atau shutdown suatu instance, Anda harus menginstal dan menjalankan layanan acpid
pada sistem guest.
Log in to the CentOS guest and run the following commands to install the ACPI service and configure it to start when the system boots:
# dnf install acpid
# systemctl enable acpid
Konfigurasi untuk mengambil metadata¶
Catatan
Check cloud-init documentation for more information
An instance must interact with the metadata service to perform
several tasks on start up. For example, the instance must get
the ssh public key and run the user data script. To ensure that
the instance performs these tasks, install the cloud-init
utility.
Paket cloud-init
secara otomatis mengambil kunci publik dari server metadata dan menempatkan kunci di akun. Install cloud-init
di dalam CentOS guest dengan menjalankan:
# dnf install cloud-init
The account varies by distribution. On CentOS Stream virtual machines,
the account is called cloud-user
.
Anda dapat mengubah nama akun yang digunakan oleh cloud-init
dengan mengedit file /etc/cloud/cloud.cfg
dan menambahkan sebuah baris dengan pengguna yang berbeda. Misalnya, untuk mengkonfigurasi cloud-init
untuk meletakkan kunci di akun bernama admin
, gunakan sintaks berikut di file konfigurasi:
users:
- name: admin
(...)
Cloud-init alternatives¶
Peringatan
This method is not recommended as only gets the ssh public key from the metadata server. It does not get user data, which is optional data that can be passed by the user when requesting a new instance. User data is often used to run a custom script when an instance boots.
Jika Anda tidak dapat menginstal paket cloud-init
dalam image Anda, mengambil ssh kunci publik dan menambahkannya ke akun root, edit file /etc/rc.d/rc.local
dan tambahkan baris berikut sebelum baris touch /var/lock/subsys/local
:
if [ ! -d /root/.ssh ]; then
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi
# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key \
> /tmp/metadata-key 2>/dev/null
if [ \$? -eq 0 ]; then
cat /tmp/metadata-key >> /root/.ssh/authorized_keys
chmod 0600 /root/.ssh/authorized_keys
restorecon /root/.ssh/authorized_keys
rm -f /tmp/metadata-key
echo "Successfully retrieved public key from instance metadata"
echo "*****************"
echo "AUTHORIZED KEYS"
echo "*****************"
cat /root/.ssh/authorized_keys
echo "*****************"
fi
done
Catatan
Beberapa klien VNC menggantikan colon (:
) dengan semicolon (;
) dan underscore (_
) dengan hyphen (-
). Pastikan untuk menentukan http:
dan bukan http;
. Pastikan untuk menentukan authorized_keys
dan bukan authorized-keys
.
Catatan
With this method, previous CentOS versions needed to disable the zeroconf route, this is not needed as of EL8.
Allow partitions to resize¶
Agar partisi root dapat disesuaikan dengan benar, pasang paket cloud-utils-growpart
, yang berisi alat yang tepat agar disk dapat diubah ukurannya menggunakan cloud-init.
# dnf install cloud-utils-growpart
Mengkonfigurasi konsol¶
Peringatan
Actually this does not work even following https://access.redhat.com/solutions/3443551
For the openstack console log command to work properly, instance must be configured to sent output to a serial console.
Edit the
/etc/default/grub
file and appendconsole=tty0 console=ttyS0,115200n8
toGRUB_CMDLINE_LINUX
.Sebagai contoh:
... GRUB_CMDLINE_LINUX="resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap console=tty0 console=ttyS0,115200n8"
Then regenerate GRUB configuration:
# grub2-mkconfig -o /boot/grub2/grub.cfg --update-bls-cmdline
Customize¶
If you are building an image manually, you are probably interested in customizing it, now it's time to apply your edits.
Matikan instance¶
Shutdown the VM:
# shutdown now
Clean up¶
The image needs to be cleaned up of details such as the MAC address - the tool used is virt-sysprep, part of libguestfs
sudo virt-sysprep -d my-centos
Resize the image¶
Resize and compress the image to remove unused space (the image will then use available space after initialization) using virt-sparsify, part of libguestfs
sudo virt-sparsify --compress /var/lib/libvirt/images/my-centos.qcow2 ~/upload.qcow2
Image lengkap¶
The resized image is now ready to be uploaded using openstack image create. For more information, see the python-openstackclient command list.
Tak ada penetapan domain libvirt¶
Now that you can upload the image to the Image service, you no longer need to have this virtual machine image managed by libvirt.
$ sudo virsh undefine my-centos
Or if you used virt-manager, delete it through the GUI.