@@ -22,21 +22,21 @@ VM_RAM=2048
2222SHARED_FOLDER=" /var/lib/libvirt/shared/${VM_NAME} "
2323
2424function log() {
25- echo
26- echo
27- echo " [$( date --iso=seconds) ] $@ "
25+ echo
26+ echo
27+ echo " [$( date --iso=seconds) ] $@ "
2828}
2929
3030function vm_create() {
31- log " Installing system packages"
32- install_libs
33- config_permissions
34- qemu_connect
35- create_shared_folder
36- download_image
37-
38- log " Creating cloud-init YAML files"
39- cat << EOF >"$CLOUDINIT_USER_YAML "
31+ log " Installing system packages"
32+ install_libs
33+ config_permissions
34+ qemu_connect
35+ create_shared_folder
36+ download_image
37+
38+ log " Creating cloud-init YAML files"
39+ cat << EOF > "$CLOUDINIT_USER_YAML "
4040#cloud-config
4141
4242locale: en_US
@@ -67,154 +67,154 @@ runcmd:
6767 - echo 'LANG=en_US.UTF-8' > /etc/default/locale
6868 - locale-gen
6969EOF
70- cat << EOF >"$CLOUDINIT_META_YAML "
70+ cat << EOF > "$CLOUDINIT_META_YAML "
7171instance-id: $VM_NAME
7272local-hostname: $VM_NAME
7373EOF
7474
75- log " Creating overlay qcow2 image"
76- qemu-img create -f qcow2 -F qcow2 -b " $DEBIAN_QCOW2 " " $OVERLAY_QCOW2 " " $OVERLAY_DISK_SIZE "
77-
78- log " Creating virtual machine"
79-
80- if is_gentoo; then
81- mkdir /var/lib/libvirt/shared/debian12-pydokku
82- mkdir /var/lib/libvirt/boot
83- fi
84-
85- virt-install \
86- --connect " qemu:///system" \
87- --name " $VM_NAME " \
88- --memory $VM_RAM \
89- --vcpus $VM_VCPUS \
90- --os-variant debian12 \
91- --disk " path=$OVERLAY_QCOW2 ,format=qcow2,bus=virtio" \
92- --filesystem " source.dir=${SHARED_FOLDER} ,target.dir=host_shared,driver.type=virtiofs" \
93- --memorybacking " source.type=memfd,access.mode=shared" \
94- --cloud-init " meta-data=$CLOUDINIT_META_YAML ,disable=on" \
95- --cloud-init " user-data=$CLOUDINIT_USER_YAML ,disable=on" \
96- --network " network=default,model=virtio" \
97- --graphics " none" \
98- --console " pty,target_type=serial" \
99- --boot hd \
100- --noautoconsole
101-
102- log " Waiting for the VM network to be up..."
103- ip=$( vm_wait_for_ip)
104- echo " VM IP address: $ip "
105- echo " Use: ssh $DEFAULT_USERNAME @$ip (password: $DEFAULT_PASSWORD )"
75+ log " Creating overlay qcow2 image"
76+ qemu-img create -f qcow2 -F qcow2 -b " $DEBIAN_QCOW2 " " $OVERLAY_QCOW2 " " $OVERLAY_DISK_SIZE "
77+
78+ log " Creating virtual machine"
79+
80+ if is_gentoo; then
81+ mkdir /var/lib/libvirt/shared/debian12-pydokku
82+ mkdir /var/lib/libvirt/boot
83+ fi
84+
85+ virt-install \
86+ --connect " qemu:///system" \
87+ --name " $VM_NAME " \
88+ --memory $VM_RAM \
89+ --vcpus $VM_VCPUS \
90+ --os-variant debian12 \
91+ --disk " path=$OVERLAY_QCOW2 ,format=qcow2,bus=virtio" \
92+ --filesystem " source.dir=${SHARED_FOLDER} ,target.dir=host_shared,driver.type=virtiofs" \
93+ --memorybacking " source.type=memfd,access.mode=shared" \
94+ --cloud-init " meta-data=$CLOUDINIT_META_YAML ,disable=on" \
95+ --cloud-init " user-data=$CLOUDINIT_USER_YAML ,disable=on" \
96+ --network " network=default,model=virtio" \
97+ --graphics " none" \
98+ --console " pty,target_type=serial" \
99+ --boot hd \
100+ --noautoconsole
101+
102+ log " Waiting for the VM network to be up..."
103+ ip=$( vm_wait_for_ip)
104+ echo " VM IP address: $ip "
105+ echo " Use: ssh $DEFAULT_USERNAME @$ip (password: $DEFAULT_PASSWORD )"
106106}
107107
108108function is_gentoo() {
109- if uname -r | grep -iq " gentoo" ; then
110- true
111- else
112- false
113- fi
109+ if uname -r | grep -iq " gentoo" ; then
110+ true
111+ else
112+ false
113+ fi
114114}
115115
116116function install_libs() {
117- if is_gentoo; then
118- echo " Gentoo system detected. Running emerge --sync..."
119- emerge --sync
120- emerge --deep app-emulation/libvirt app-emulation/qemu virtiofsd app-emulation/virt-manager whois
121- systemctl start libvirtd
122- else
123- echo " Non-Gentoo system detected. Running apt update..."
124- apt update
125- apt install -y libvirt-daemon-system virtiofsd
126- fi
117+ if is_gentoo; then
118+ echo " Gentoo system detected. Running emerge --sync..."
119+ emerge --sync
120+ emerge --deep app-emulation/libvirt app-emulation/qemu virtiofsd app-emulation/virt-manager whois
121+ systemctl start libvirtd
122+ else
123+ echo " Non-Gentoo system detected. Running apt update..."
124+ apt update
125+ apt install -y libvirt-daemon-system virtiofsd
126+ fi
127127}
128128
129129function config_permissions() {
130- log " Configuring libvirt user permission and network"
131- if ! groups $USER | grep -q " \blibvirt\b" ; then
132- usermod -a -G libvirt $USER
133- fi
130+ log " Configuring libvirt user permission and network"
131+ if ! groups $USER | grep -q " \blibvirt\b" ; then
132+ usermod -a -G libvirt $USER
133+ fi
134134}
135135
136136function qemu_connect() {
137- if ! virsh --connect " qemu:///system" net-info default | grep -q " Active:.*yes" ; then
138- virsh --connect " qemu:///system" net-start default
139- fi
140- if ! virsh --connect " qemu:///system" net-info default | grep -q " Autostart:.*yes" ; then
141- virsh --connect " qemu:///system" net-autostart default
142- fi
137+ if ! virsh --connect " qemu:///system" net-info default | grep -q " Active:.*yes" ; then
138+ virsh --connect " qemu:///system" net-start default
139+ fi
140+ if ! virsh --connect " qemu:///system" net-info default | grep -q " Autostart:.*yes" ; then
141+ virsh --connect " qemu:///system" net-autostart default
142+ fi
143143}
144144
145145function create_shared_folder() {
146- log " Creating shared folder"
147- mkdir -p " $SHARED_FOLDER "
148- chown -R ${SUDO_USER:- $USER } :libvirt " $SHARED_FOLDER "
146+ log " Creating shared folder"
147+ mkdir -p " $SHARED_FOLDER "
148+ chown -R ${SUDO_USER:- $USER } :libvirt " $SHARED_FOLDER "
149149}
150150
151151function download_image() {
152- log " Downloading Debian cloud-ready image"
152+ log " Downloading Debian cloud-ready image"
153153
154- if is_gentoo; then
155- mkdir -p /var/lib/libvirt/images
156- fi
157- wget -c -t 0 -O " $DEBIAN_QCOW2 " " $DEBIAN_QCOW2_URL "
154+ if is_gentoo; then
155+ mkdir -p /var/lib/libvirt/images
156+ fi
157+ wget -c -t 0 -O " $DEBIAN_QCOW2 " " $DEBIAN_QCOW2_URL "
158158}
159159
160160function vm_start() {
161- virsh --connect " qemu:///system" start " $VM_NAME "
162- while [[ $( virsh --connect " qemu:///system" -q domstate " $VM_NAME " ) != " running" ]]; do
163- sleep 0.1
164- done
161+ virsh --connect " qemu:///system" start " $VM_NAME "
162+ while [[ $( virsh --connect " qemu:///system" -q domstate " $VM_NAME " ) != " running" ]]; do
163+ sleep 0.1
164+ done
165165}
166166
167167function vm_stop() {
168- virsh --connect " qemu:///system" shutdown " $VM_NAME "
169- while [[ $( virsh --connect " qemu:///system" -q domstate " $VM_NAME " ) != " shut off" ]]; do
170- sleep 0.1
171- done
168+ virsh --connect " qemu:///system" shutdown " $VM_NAME "
169+ while [[ $( virsh --connect " qemu:///system" -q domstate " $VM_NAME " ) != " shut off" ]]; do
170+ sleep 0.1
171+ done
172172}
173173
174174function vm_delete() {
175- virsh --connect " qemu:///system" destroy " $VM_NAME " 2> /dev/null || true
176- virsh --connect " qemu:///system" undefine " $VM_NAME " --remove-all-storage 2> /dev/null || true
177- rm -f " $OVERLAY_QCOW2 " " $CLOUDINIT_USER_YAML " " $CLOUDINIT_META_YAML "
178- rm -rf " $SHARED_FOLDER "
175+ virsh --connect " qemu:///system" destroy " $VM_NAME " 2> /dev/null || true
176+ virsh --connect " qemu:///system" undefine " $VM_NAME " --remove-all-storage 2> /dev/null || true
177+ rm -f " $OVERLAY_QCOW2 " " $CLOUDINIT_USER_YAML " " $CLOUDINIT_META_YAML "
178+ rm -rf " $SHARED_FOLDER "
179179}
180180
181181function vm_wait_for_ip() {
182- local VM_IP=" "
183- while [[ -z " $VM_IP " ]]; do
184- VM_IP=$( virsh --connect " qemu:///system" -q domifaddr " $VM_NAME " | grep --color=no ipv4 | sed ' s/.*ipv4\s\+//; s/\/.*//' )
185- sleep 1
186- done
187- echo " $VM_IP "
182+ local VM_IP=" "
183+ while [[ -z " $VM_IP " ]]; do
184+ VM_IP=$( virsh --connect " qemu:///system" -q domifaddr " $VM_NAME " | grep --color=no ipv4 | sed ' s/.*ipv4\s\+//; s/\/.*//' )
185+ sleep 1
186+ done
187+ echo " $VM_IP "
188188}
189189
190190function vm_ssh() {
191- ip=$( vm_wait_for_ip)
192- echo " Connecting to ${ip} . Use the password: $DEFAULT_PASSWORD "
193- ssh $DEFAULT_USERNAME @$ip
191+ ip=$( vm_wait_for_ip)
192+ echo " Connecting to ${ip} . Use the password: $DEFAULT_PASSWORD "
193+ ssh $DEFAULT_USERNAME @$ip
194194}
195195
196196subcommand=" ${1:- } "
197197case " $subcommand " in
198- create)
199- vm_create
200- ;;
201- start)
202- vm_start
203- ;;
204- stop)
205- vm_stop
206- ;;
207- ip)
208- vm_wait_for_ip
209- ;;
210- ssh)
211- vm_ssh
212- ;;
213- delete)
214- vm_delete
215- ;;
216- * )
217- echo " Usage: $0 {create|start|stop|ip|delete}"
218- exit 1
219- ;;
198+ create)
199+ vm_create
200+ ;;
201+ start)
202+ vm_start
203+ ;;
204+ stop)
205+ vm_stop
206+ ;;
207+ ip)
208+ vm_wait_for_ip
209+ ;;
210+ ssh)
211+ vm_ssh
212+ ;;
213+ delete)
214+ vm_delete
215+ ;;
216+ * )
217+ echo " Usage: $0 {create|start|stop|ip|delete}"
218+ exit 1
219+ ;;
220220esac
0 commit comments