Home automatisch eine VM mit virt-install erstellen
Post
Cancel

automatisch eine VM mit virt-install erstellen

Dieser Beitrag wurde vor mehr als 3 Jahren veröffentlicht!
Höchstwahrscheinlich ist dieser inzwischen veraltet.

Ab und an benötige ich eine Test VM um ein paar Dinge auszuprobieren.

Dank KVM kann ich das ganze auch ziemlich einfach auf meiner Monsterentwicklungsmaschine lokal machen ohne gleich den Computer anderer Leute (aka Cloud) zu nutzen. Für bestimmte Sachen möchte ich aber so wenig wie möglich in einem automatischen Prozess eingreifen. In dem Falle … der Erstinstallation mit dem abfragen aller Parameter.

Dafür kann man fai (für debain) oder kickstart (für RedHat / CentOS) nutzen.

Automatisiert möchte ich einfach nur ein Script aufrufen, welches mir eine VM erstellt, die ich anschließend weiter nutzen kann.

Vorarbeiten

Als erstes benötigen wir ein Installationsmedium!
Da ich in meinem speziellen Fall ein CentOS benötige, habe ich mir bei einem der mirrors ein Image heruntergeladen.
Wichtig ist hier, das man ein minimal ISO nimmt, das NetInstall ISO hat mit der folgenden Kickstart Datei nicht funktioniert.

1
2
3
curl -L \
-o /var/lib/libvirt/isos/centos7.iso \
http://mirror1.hs-esslingen.de/pub/Mirrors/centos/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso

Eine VM kann ohne eine existierende virtuelle Platte nicht erzeugt werden. Daher nutzen wir die qemu Tools, um uns eine qcow2 Datei zu erstellen:

1
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/testing.qcow2 20G

Letzendlich benötigen wir auch noch die Kickstart Datei

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# System authorization information
auth --enableshadow --passalgo=sha512

# Use CDROM installation media
cdrom

# Use text or graphical install mode
text
#graphical

# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda

# Keyboard layouts
keyboard --vckeymap=de --xlayouts='de'

# System language
lang de_DE.UTF-8

# Network information
network  --bootproto=dhcp --device=eth0 --activate
network  --hostname=localhost.localdomain

# Set repo to mirror.centos.orgrepo --name="CentOS" --baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ --cost=100
repo --name="Updates" --baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/ --cost=100

# Root password
rootpw xxx
#rootpw --iscrypted <ENCRYPTED_PASSWORD>

# System services
services --enabled="chronyd"

# System timezone
timezone Europe/Berlin --isUtc

# user --groups=wheel --name=stack --password=<ENCRYPTED_PASSWORD> --iscrypted --gecos="stack"

# System bootloader configuration
bootloader --append=" crashkernel=auto tsc=reliable divider=10 plymouth.enable=0 console=ttyS0 " --location=mbr --boot-drive=vda

# partition plan
zerombr
clearpart --all --drives=vda --initlabel
part /boot --fstype ext4 --size=1024
part swap --size=1024
part pv.01      --size=1000     --grow  --ondisk=vda
volgroup s pv.01
logvol /        --vgname=s --fstype=xfs --size 2048 --grow --name=root
logvol /var/log --vgname=s --fstype=xfs --size=1024 --name=var_log
logvol /tmp     --vgname=s --fstype=xfs --size=512  --name=tmp

selinux --permissive

# Installation logging level
logging --level=debug

reboot
%packages
@^minimal
@core  --nodefaults
chrony
nano
kexec-tools
# Exclude packages that are not needed in a VM image
-aic94xx-firmware
-alsa-*
-btrfs-progs*
-centos-logos
-dracut-config-rescue
-dracut-network
-microcode_ctl
-NetworkManager*
-ivtv-firmware
-iwl*
firmware-plymouth*
%end

%post --log=/var/log/ks.post02.log
#!/bin/bash
#set -x
# MOTD symlinks
ln -sf /etc/motd /etc/issue
ln -sf /etc/motd /etc/issue.net

# Make sure we have the latest security updates
/usr/bin/yum clean all
/usr/bin/yum update -y

# Clean up all yum caches
/usr/bin/yum clean all

# Tune Linux vm.dirty_background_bytes (IMAGE-439)
# The following tuning causes dirty data to begin to be background flushed at
# 100 Mbytes, so that it writes earlier and more often to avoid a large build
# up and improving overall throughput.
echo "vm.dirty_background_bytes=100000000" >> /etc/sysctl.conf

# Disable kdump
systemctl disable kdump.service
%end

Diese speichert man sich als ks.cfg an einem gewünschten Ort ab.

los gehts …

Zum Schluß müssen wir alles nur noch miteinander kombinieren.

1
2
3
4
5
6
7
8
9
10
sudo virt-install \
--name test \
--memory 1024 \
--vcpus 2 \
--boot hd,cdrom,menu=on \
--disk /var/lib/libvirt/images/testing.qcow2,size=20,bus=virtio \
--location /var/lib/libvirt/isos/centos7.iso \
--network network=default \
--initrd-inject ~/automation/centos/ks.cfg \
--extra-args "inst.ks=file:/ks.cfg"

Wer möchte kann über --metadata description="My nice long description" der VM noch eine passende Beschreibung mitgeben.

This post is licensed under CC BY 4.0 by the author.