From 511ef2f2f7200b4fe92373682433747d095b6dc3 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Thu, 22 Jan 2026 08:24:34 -0500 Subject: [PATCH] refactor for much more simplicity. --- .../files/simple/usr/local/bin/create-vm | 144 ++++++++++-------- 1 file changed, 80 insertions(+), 64 deletions(-) diff --git a/roles/kvmhost/files/simple/usr/local/bin/create-vm b/roles/kvmhost/files/simple/usr/local/bin/create-vm index c4b72e2..5811f12 100755 --- a/roles/kvmhost/files/simple/usr/local/bin/create-vm +++ b/roles/kvmhost/files/simple/usr/local/bin/create-vm @@ -195,7 +195,7 @@ cli-arg-check() { ## from here, checks for when we are actually creating a vm # note: alter this when we upgrade a kvmhost - if [[ $release == aramo && $HOSTNAME == kvmhost[234] && \ + if [[ ( $release == aramo || $release == ecne ) && $HOSTNAME == kvmhost[234] && \ ! -d /srv/debootstrap-cache/$release/etc ]] ; then err "error: this os is too old to debootstrap $release. rsync it first to /srv/debootstrap-cache/$release @@ -449,6 +449,10 @@ EOF } chroot-add-ceph-disk() { + m mount-vm --first-disk $host + push-cleanup "umount-vm $host" + /mnt/$host/boot/keyscript.sh >$keyfile + if [[ ! -e /mnt/$host/dev/sd$letter ]]; then m mknod /mnt/$host/dev/sd$letter b 8 $disk_num fi @@ -502,39 +506,24 @@ ceph-disk() { m rbd create $dname --size $new_disk_mb -p $pool --image-format=2 --image-feature exclusive-lock,object-map,fast-diff,layering push-cleanup "rbd rm $dname" - if $add_disk && vm-on; then return 0; fi + if $add_disk; then return 0; fi - if $add_disk; then - m mount-vm --first-disk $host - push-cleanup "umount-vm $host" - /mnt/$host/boot/keyscript.sh >$keyfile - else - mk-decrypting-grub-image - fi + mk-decrypting-grub-image - mkfs-and-mount-ceph-disk + if $import; then return 0; fi - if $add_disk; then - chroot-add-ceph-disk - else - chroot-add-ceph-root-disk - fi + + # umount $target + # pop-cleanup # todo iank } -close-disk() { - if mountpoint -q $target; then - m umount $target - pop-cleanup - fi - if [[ -e $luks_dev ]]; then - m cryptsetup luksClose $luks_dev - pop-cleanup - fi - if [[ -e $nbd_dev ]]; then - m rbd-nbd unmap $nbd_dev - pop-cleanup - fi + +umount-ceph-disk() { + m umount $target + pop-cleanup + m rbd-nbd unmap $nbd_dev + pop-cleanup } install-dependencies() { @@ -625,6 +614,18 @@ attach-disk() { letter=${disk_letters[disk_num+i]} attach-local-disk $disk $host done + + if $inside_vm_cmds; then + cat <$keyfile + for dir in /root/crypt-keys-backup/*; do + if mountpoint -q $dir; then + keyfile_copies+=("$dir/$host") + fi + done + e writing generated password to ${keyfile_copies[*]} + pwgen 128 -s -1 | tr -d '\n' | tee ${keyfile_copies[@]} >/dev/null fi - push-cleanup "rm -f $keyfile" + push-cleanup "rm -f ${keyfile_copies[*]}" # directory is already 700, just being thorough - m chmod 600 $keyfile + m chmod 600 ${keyfile_copies[*]} mountdir=/mnt/$host @@ -882,9 +889,7 @@ EOF yes YES | m cryptsetup luksFormat $mddev $keyfile || [[ $? == 141 ]] m cryptdisks_start $luks_name push-cleanup "cryptsetup luksClose $luks_dev" - m mkfs.ext4 $luks_dev fi - m mount $luks_dev $target else fs_type=btrfs @@ -914,6 +919,16 @@ EOF push-cleanup "cryptsetup luksClose /dev/mapper/$luks_name" done + fi +} # End nonceph-disk + + +mkfs-and-mount-nonceph-disk() { + if $mdraid; then + m mkfs.ext4 $luks_dev + m mount $luks_dev $target + push-cleanup "umount $target" + else m mkfs.btrfs -f -m raid1c3 -d raid1c3 ${vm_disks[@]} m mount ${vm_disks[0]} $mountdir push-cleanup "umount $mountdir" @@ -921,15 +936,13 @@ EOF m umount $mountdir pop-cleanup m mount -o subvol=root ${vm_disks[0]} $target + push-cleanup "umount $target" fi - # Note: If $add_disk is true, we did not need to mount to $target, but as the - # script is currently written, it would require several conditionals to avoid - # it, so just do it anyways for the sake of making the script simpler. - push-cleanup "umount $target" -} # End nonceph-disk +} + -nonceph-maybe-mount-extra-disk() { - if ! $add_disk; then +chroot-add-nonceph-disk() { + if ! $add_disk || $doceph; then return fi @@ -1490,40 +1503,43 @@ if $doceph; then ceph-disk else nonceph-disk - nonceph-maybe-mount-extra-disk -fi - -if ! $import && ! $add_disk; then - os-bootstrap - os-prep fi -close-disk +#### From here there are 3 separate paths to successful exit. +## 1. Add disk to VM +## 2. VM with filesystem to be imported later. +## 3. Regular fresh VM. if $add_disk; then + if $doceph; then + mkfs-and-mount-ceph-disk + chroot-add-ceph-disk + umount-ceph-disk + else + mkfs-and-mount-nonceph-disk + chroot-add-nonceph-disk + m umount $target; pop-cleanup + fi attach-disk -else - create-vm + exit 0 fi - -if $inside_vm_cmds; then - cat </dev/null; then - if ! backup-crypt-luks-keys; then - echo "$0: this was the last command of create-vm, so just sort out any error and hopefully continue to use the vm" - fi +if $doceph; then + mkfs-and-mount-ceph-disk + chroot-add-ceph-root-disk +else + mkfs-and-mount-nonceph-disk fi +os-bootstrap +os-prep +m umount $target; pop-cleanup +create-vm exit 0 ###### dev notes ##### -- 2.25.1