From c417a6f3f9a1b707f40abfc0b4f4dc1a5338b385 Mon Sep 17 00:00:00 2001 From: emdee Date: Mon, 1 Jan 2024 01:04:40 +0000 Subject: [PATCH] added --- Makefile | 37 +- README.md | 38 +- hosts.yml | 2 +- library/ansible_gentooimgr.py | 3 +- .../ansible-gentoo_install/tasks/#local.yml# | 183 - roles/ansible-gentoo_install/tasks/copy.yml | 8 +- roles/ansible-gentoo_install/tasks/local.yml | 25 +- roles/ansible-gentoo_install/tasks/main.yml | 30 +- roles/toxcore/files/gentoo.xml | 193 + .../local/bin/toxcore_build_overlay_qcow.bash | 92 + .../usr/local/bin/toxcore_check_modules.bash | 125 + .../Linux/usr/local/bin/toxcore_create-ga.sh | 14385 ++++++++++++++++ .../usr/local/bin/toxcore_create-vm.bash | 420 + .../Linux/usr/local/bin/toxcore_daily.bash | 80 + .../usr/local/bin/toxcore_delete-vm.bash | 61 + .../usr/local/bin/toxcore_get-vm-ip.bash | 55 + .../Linux/usr/local/bin/toxcore_hourly.bash | 56 + .../share/openpgp-keys/gentoo-release.asc | 372 + .../Linux/usr/local/src/c-toxcore.bash | 127 + .../Linux/usr/local/src/negotiator.bash | 86 + .../Linux/usr/local/src/tox_profile.bash | 75 + .../usr/local/src/usr_local_toxcore.bash | 43 + roles/toxcore/tasks/Gentoo.yml | 12 +- roles/toxcore/tasks/libvirt_whonix.yml | 275 + roles/toxcore/tasks/main.yml | 1 + .../bin/ansible_gentooimgr.bash | 75 + .../gentooimgr/#install.py# | 360 - src/ansible_gentooimgr/gentooimgr/__main__.py | 13 +- src/ansible_gentooimgr/gentooimgr/install.py | 1 + 29 files changed, 16625 insertions(+), 608 deletions(-) delete mode 100644 roles/ansible-gentoo_install/tasks/#local.yml# create mode 100644 roles/toxcore/files/gentoo.xml create mode 100755 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_build_overlay_qcow.bash create mode 100755 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_check_modules.bash create mode 100755 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_create-ga.sh create mode 100755 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_create-vm.bash create mode 100644 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_daily.bash create mode 100755 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_delete-vm.bash create mode 100755 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_get-vm-ip.bash create mode 100644 roles/toxcore/overlay/Linux/usr/local/bin/toxcore_hourly.bash create mode 100644 roles/toxcore/overlay/Linux/usr/local/share/openpgp-keys/gentoo-release.asc create mode 100755 roles/toxcore/overlay/Linux/usr/local/src/c-toxcore.bash create mode 100644 roles/toxcore/overlay/Linux/usr/local/src/negotiator.bash create mode 100755 roles/toxcore/overlay/Linux/usr/local/src/tox_profile.bash create mode 100644 roles/toxcore/overlay/Linux/usr/local/src/usr_local_toxcore.bash create mode 100644 roles/toxcore/tasks/libvirt_whonix.yml create mode 100755 src/ansible_gentooimgr/bin/ansible_gentooimgr.bash delete mode 100644 src/ansible_gentooimgr/gentooimgr/#install.py# diff --git a/Makefile b/Makefile index 6582cf2..3ec48d4 100644 --- a/Makefile +++ b/Makefile @@ -28,14 +28,6 @@ VERBOSE=2 all: install lint build check run test -# groddy but works for me -install:: -# ( /usr/local/src ; ansible-galaxy collection install \ -# file:///usr/local/src/community.general ) - [ -e $(ANSIBLE_PLUGINS)/connection/libvirt_qemu.py ] \ - || ln -s ${PWD}/lib/plugins/libvirt_qemu.py \ - $(ANSIBLE_PLUGINS)/connection/q || true - lint:: @sudo xmllint -noout roles/ansible-gentoo_install/templates/etc/libvirt/qemu/gentoo.xml @yamllint -c .yamllint.yml -f standard *.yml roles/*/*s/*yml 2>&1| \ @@ -44,18 +36,29 @@ lint:: grep -B 2 error | tee .yamllint.err || true grep Error .yamllint.out || true -build:: build_base +# groddy but works for me +install:: lint +# ( /usr/local/src ; ansible-galaxy collection install \ +# file:///usr/local/src/community.general ) + [ -e $(ANSIBLE_PLUGINS)/connection/libvirt_qemu.py ] \ + || ln -s ${PWD}/lib/plugins/libvirt_qemu.py \ + $(ANSIBLE_PLUGINS)/connection/q || true + @[ -f ${BOX_NBD_BASE_QCOW} ] || { \ + echo ERROR: not created BOX_NBD_DEV="${BOX_NBD_DEV}" - use ; \ + echo qemu-img create -f qcow2 "${BOX_NBD_BASE_QCOW}" 20G ; \ + exit 2 ; } + @( ps ax | grep -v grep | \ + grep "qemu-nbd.*/dev/nbd.*${BOX_NBD_BASE_QCOW}" ) || { \ + echo ERROR: not mounted BOX_NBD_DEV="${BOX_NBD_DEV}" - use ; \ + echo qemu-nbd -n -f qcow2 -c /dev/nbd1 ${BOX_NBD_BASE_QCOW} ;\ + exit 1 ; } + +build:: + sudo $(MAKE) -$(MAKEFLAGS) build_base sudo $(MAKE) -$(MAKEFLAGS) build_overlay -build_base:: lint +build_base:: install echo INFO: $@ "${BOX_NBD_BASE_QCOW}" - @[ ! -f ${BOX_NBD_BASE_QCOW} ] || { \ - echo WARN looks like theres already a build of \ - "${BOX_NBD_BASE_QCOW}" ; exit 2 ; } - @( ! ps ax | grep -v grep | \ - grep "qemu-nbd.*/dev/nbd.*${BOX_NBD_BASE_QCOW}" ) ||{ \ - echo WARN looks like theres an active nbd mount of \ - "${BOX_NBD_BASE_QCOW}" && exit 1 ; } echo INFO running the toxcore role will build ${BOX_NBD_BASE_QCOW} sudo sh ansible_local.bash --diff -i ${PWD}/hosts.yml \ -l ${LOCALHOST} -c local --verbose ${VERBOSE} \ diff --git a/README.md b/README.md index 0c3e481..43ae18b 100644 --- a/README.md +++ b/README.md @@ -77,26 +77,36 @@ the hosts.yml file from the host called gentoo1 in the linux_libvirt_group. There are 3 ansible roles: 1. base : The base role sets up the basics and is required to be run. - + It sets up the essential parameters to run roles on the host or client. + Check the settings in roles/base/defaults/main.yml before running the role. + 2. proxy : The proxy role sets up the networking with proxies, and is required to be run, even if you don't use a proxy. + It sets proxying and installs basic packages on the host or client. + Check the settings in roles/proxy/defaults/main.yml before running the role. 3. toxcore : + This role sets up the software to run libvirt on the host. + Check the settings in roles/toxcore/defaults/main.yml before running the role. + + In addition, toxcore calls an included role ansible-gentoo_install. + This is an updated version of the abandonned + https://github.com/agaffney/ansible-gentoo_install/ This role, + when run on the host, builds the Gentoo base qcow image. As a safety + feature, you must create the qcow2 image and activate it with: + + The host creates the base qcow2 image and then creates the overlay + image. When both are created, it install Tox software on the host and + client. -In addition, toxcore calls an included role ansible-gentoo_install. -This is an updated version of the abandonned -https://github.com/agaffney/ansible-gentoo_install/ This role, -when run on the host, builds the Gentoo base qcow image. As a safety -feature, you must create the qcow2 image and activate it with: + modprobe nbd + qemu-img $BOX_NBD_BASE_QCOW 20G + qemu-nbd -c $BOX_NBD_DEV $BOX_NBD_BASE_QCOW -modprobe nbd -qemu-img $BOX_NBD_BASE_QCOW 20G -qemu-nbd -c $BOX_NBD_DEV $BOX_NBD_BASE_QCOW - -and put these values into the hosts.yml file in the pentoo or devuan -target, depending on your host operating system. The filesytem that -holds base qcow2 $BOX_NBD_BASE_QCOW must have at least 12G available, -and may grow to almost 20G. + and put these values into the hosts.yml file in the pentoo or devuan + target, depending on your host operating system. The filesytem that + holds base qcow2 $BOX_NBD_BASE_QCOW must have at least 12G available, + and may grow to almost 20G. After you have finished building the base qcow2 image, you will want to dismount it with qemu-nbd -d $BOX_NBD_DEV. Be careful and look diff --git a/hosts.yml b/hosts.yml index b7fa02e..5b7a23d 100644 --- a/hosts.yml +++ b/hosts.yml @@ -259,7 +259,7 @@ all: # for a non-root login: ansible_ssh_extra_args: "--userspec=foo:adm" vars: # linux_unix_group # toxcore - BOX_NBD_DEV: nbd3 + BOX_NBD_DEV: nbd1 BOX_NBD_MP: /mnt/gentoo BOX_NBD_OVERLAY_NAME: "gentoo1" BOX_NBD_FILES: "/i/data/Agile/tmp/Topics/GentooImgr" diff --git a/library/ansible_gentooimgr.py b/library/ansible_gentooimgr.py index 845f202..4b75bbc 100755 --- a/library/ansible_gentooimgr.py +++ b/library/ansible_gentooimgr.py @@ -163,7 +163,8 @@ def run_module(): action=dict(type='str', required=True), loglevel=dict(type='int', required=False, default=logging.INFO), threads=dict(type='int', required=False, default=1), - config=dict(type='path', default=def_config, required=True), + # Module error: required and default are mutually exclusive for config + config=dict(type='path', default=def_config), profile=dict(type='str', required=False), kernel_dir=dict(type='path', required=False), portage=dict(type='path', required=False), diff --git a/roles/ansible-gentoo_install/tasks/#local.yml# b/roles/ansible-gentoo_install/tasks/#local.yml# deleted file mode 100644 index 04932c0..0000000 --- a/roles/ansible-gentoo_install/tasks/#local.yml# +++ /dev/null @@ -1,183 +0,0 @@ -# -*- mode: yaml; indent-tabs-mode: nil; tab-width: 2; coding: utf-8-unix -*- ---- -- name: "DEBUG: ansible-gentoo_install local" - debug: - verbosity: 0 - msg: "DEBUG: ansible-gentoo_install local BOX_NBD_DEV={{BOX_NBD_DEV}}" - check_mode: no - -- assert: - that: - - "'{{BOX_NBD_DEV}}' != ''" - when: ansible_connection in ['local', 'chroot'] - -- set_fact: - AGI_use_local_kernel: true - when: - - ansible_distribution == 'Gentoo' or BOX_GENTOO_FROM_MP not in ['/', ''] - -- set_fact: - AGI_PROXY_MODE: "{{PROXY_MODE|default('')}}" - when: - - PROXY_MODE|default('') != '' - check_mode: no - -- set_fact: - AGI_PROXY_MODE: "{{BOX_PROXY_MODE|default('')}}" - when: - - AGI_PROXY_MODE == '' - check_mode: no - -- block: - - - name: check for mounted disk - shell: | - grep '/dev/{{AGI_NBD_DEV}}' /proc/mounts && exit 0 - ps ax | grep -v grep | \ - grep "qemu-nbd.*/dev/nbd.*{{BOX_NBD_BASE_QCOW}}" && \ - echo WARN looks like theres an active nbd mount of \ - "${BOX_NBD_BASE_QCOW}" && exit 1 - exit 2 - failed_when: false - changed_when: false - register: check_mounted_disk - check_mode: no - - - name: partition if disk not mounted - fail: - msg: "looks like theres an active nbd mount of {{BOX_NBD_BASE_QCOW}}" - when: - - check_mounted_disk.rc == 1 - check_mode: no - - - name: partition if disk not mounted or active - include: disk.yml - when: - - check_mounted_disk.rc > 1 - check_mode: no - - - name: mount root partition - mount: - name: "{{AGI_NBD_MP}}" - src: "{{ AGI_install_disk }}p3" - fstype: ext4 - state: mounted - check_mode: false - - - name: create /boot mountpoint - file: - path: "{{AGI_NBD_MP}}/boot" - state: directory - check_mode: false - - - name: mount boot partition - mount: - name: "{{AGI_NBD_MP}}/boot" - src: "{{ AGI_install_disk }}p1" - fstype: ext2 - state: mounted - check_mode: false - - - include: tarball.yml - - include: copy.yml - when: AGI_use_local_kernel - - - name: mount distfiles - delegate_to: localhost - shell: | - [ -d "{{MOUNT_GENTOO_DISTFILES_ARCHIVES}}" ] || exit 1 - grep {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} /proc/mounts && exit 0 - [ -d {{AGI_NBD_MP}}/usr/portage/ ] || exit 0 - [ -d {{AGI_NBD_MP}}/usr/portage/distfiles ] || mkdir {{AGI_NBD_MP}}/usr/portage/distfiles - mount --bind {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} {{AGI_NBD_MP}}/usr/portage/distfiles - when: - - "MOUNT_GENTOO_DISTFILES_ARCHIVES != ''" - - "AGI_NBD_MP != ''" - - - include: chroot.yml - - delegate_to: localhost - when: ansible_connection in ['chroot', 'local'] # libvirt? - -- block: - - - name: check chroot wrapper installed - shell: | - [ -x /var/tmp/chroot_wrapper.sh ] || exit 1 - df /mnt/gentoo || exit 2 - /var/tmp/chroot_wrapper.sh /bin/df | grep /mnt/gentoo && exit 4 - exit 0 - register: chroot_out - check_mode: false - - - name: enable chroot wrapper - set_fact: - ansible_shell_executable: /var/tmp/chroot_wrapper.sh - old_ansible_python_interpreter: "{{ansible_python_interpreter}}" - ansible_python_interpreter: "/usr/bin/python3" - check_mode: false - - when: ansible_connection in ['local'] - -- block: - - - include: portage.yml - - include: misc.yml - - - include: network.yml - - - include: kernel.yml - when: not AGI_use_local_kernel - - - include: bootloader.yml - - include: daemons.yml - - # - include: finish.yml - - check_mode: false - when: - - "ansible_connection in ['chroot'] or (ansible_connection in ['local'] or and chroot_out.rc|default(1) == 0)" - rescue: - - debug: - msg: "ERROR: error during chroot execution" - -- name: disable chroot wrapper - set_fact: - ansible_shell_executable: /bin/sh - ansible_python_interpreter: "{{old_ansible_python_interpreter}}" - when: - - "ansible_connection in ['local'] and chroot_out.rc|default(1) == 0" - check_mode: false - -- name: unmount filesystems - mount: - name: "{{AGI_NBD_MP}}/{{ item }}" - state: unmounted - with_items: - - proc - - sys - - dev/pts - - dev/shm - - dev - - boot - - '' - loop_control: - label: "{{AGI_NBD_MP}}/{{ item }}" - when: - - "ansible_connection in ['local'] and chroot_out.rc|default(1) == 0" - - false # leave it mounted for testing - -- name: dismount any other mounts - shell: | - if [ -z "{{MOUNT_GENTOO_DISTFILES_ARCHIVES}}" ] && \ - [ -d "{{MOUNT_GENTOO_DISTFILES_ARCHIVES}}" ] && \ - grep {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} /proc/mounts ; then - umount {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} - fi - df -a | grep "{{AGI_NBD_MP}}" | sed -e 's/.* //' | tac | while read elt;do - umount $elt - done - base_chroot_unbind.bash "{{AGI_NBD_MP}}" - when: - - "ansible_connection in ['chroot'] or chroot_out.rc|default(1) == 0" - - false # leave it mounted for testing diff --git a/roles/ansible-gentoo_install/tasks/copy.yml b/roles/ansible-gentoo_install/tasks/copy.yml index be3277c..8057726 100644 --- a/roles/ansible-gentoo_install/tasks/copy.yml +++ b/roles/ansible-gentoo_install/tasks/copy.yml @@ -39,12 +39,12 @@ - name: copy kernel sources copy: src: "{{AGI_GENTOO_FROM_MP}}/usr/src/{{kernel_out.stdout}}" - dest: "{{AGI_NBD_MP}}/usr/src" + dest: "{{AGI_NBD_MP}}/usr/src/{{kernel_out.stdout}}" remote_src: no - creates: "{{AGI_NBD_MP}}/usr/src/{{kernel_out.stdout}}" when: - kernel_out.rc|default(1) == 0 - AGI_use_local_kernel + - false - name: resolve kver shell: | @@ -99,6 +99,8 @@ when: - AGI_use_local_kernel - ramfs_out.rc|default(1) == 0 + - false + ignore_errors: true - name: make directories shell: | @@ -111,6 +113,6 @@ [ -f "{{AGI_NBD_MP}}/$file" ] && continue cp -np "$file" "{{AGI_NBD_MP}}/$file" done - ignore_errors: false + ignore_errors: true # dracut diff --git a/roles/ansible-gentoo_install/tasks/local.yml b/roles/ansible-gentoo_install/tasks/local.yml index 04932c0..50d7143 100644 --- a/roles/ansible-gentoo_install/tasks/local.yml +++ b/roles/ansible-gentoo_install/tasks/local.yml @@ -32,12 +32,19 @@ - name: check for mounted disk shell: | - grep '/dev/{{AGI_NBD_DEV}}' /proc/mounts && exit 0 - ps ax | grep -v grep | \ - grep "qemu-nbd.*/dev/nbd.*{{BOX_NBD_BASE_QCOW}}" && \ - echo WARN looks like theres an active nbd mount of \ - "${BOX_NBD_BASE_QCOW}" && exit 1 - exit 2 + grep '/dev/{{AGI_NBD_DEV}}p3' /proc/mounts && exit 0 + if [ ! -f "{{BOX_NBD_BASE_QCOW}}" ] ; then + echo ERROR: not created BOX_NBD_DEV="{{BOX_NBD_DEV}}" - use + echo qemu-img create -f qcow2 "{{BOX_NBD_BASE_QCOW}}" 20G + exit 1 + fi + if ! ps ax | grep -v grep | \ + grep "qemu-nbd.*{{AGI_NBD_DEV}}.*{{BOX_NBD_BASE_QCOW}}" ; then + echo ERROR: not mounted BOX_NBD_DEV="{{BOX_NBD_DEV}}" - use + echo qemu-nbd -n -f qcow2 -c /dev/nbd1 {{BOX_NBD_BASE_QCOW}} + exit 2 + fi + exit 3 failed_when: false changed_when: false register: check_mounted_disk @@ -45,15 +52,15 @@ - name: partition if disk not mounted fail: - msg: "looks like theres an active nbd mount of {{BOX_NBD_BASE_QCOW}}" + msg: "make and nbd mount {{BOX_NBD_BASE_QCOW}}" when: - - check_mounted_disk.rc == 1 + - check_mounted_disk.rc in [2, 1] check_mode: no - name: partition if disk not mounted or active include: disk.yml when: - - check_mounted_disk.rc > 1 + - check_mounted_disk.rc > 2 check_mode: no - name: mount root partition diff --git a/roles/ansible-gentoo_install/tasks/main.yml b/roles/ansible-gentoo_install/tasks/main.yml index 68b740d..80b3d43 100644 --- a/roles/ansible-gentoo_install/tasks/main.yml +++ b/roles/ansible-gentoo_install/tasks/main.yml @@ -52,6 +52,8 @@ fi if ! grep -q "{{BOX_NBD_DEV}}" /proc/partitions ; then echo ERROR: not mounted BOX_NBD_DEV="{{BOX_NBD_DEV}}" - use qemu-nbd + echo qemu-img create -f qcow2 /a/tmp/GentooImgr/gentoo.qcow2 20G + echo qemu-nbd -n -f qcow2 -c /dev/nbd1 /a/tmp/GentooImgr/gentoo.qcow exit 2 fi exit 0 @@ -71,19 +73,27 @@ fi fi # should operate on json with jq - # "imgsize": "20G", - # "memory": 4096, - # "mountpoint": "/mnt/gentoo", - # "imagename": null, - # "initsys": "openrc", - # "iso": null, - # "portage": null, - # "stage3": null, - # "partition": 1 if [ -f "$tofile" ] ; then + # "imgsize": "20G", + sed -i -e 's@"imgsize": ".*"@"imgsize": "{{BOX_NBD_OVERLAY_GB}}"@' $tofile + # "memory": 4096, + sed -i -e 's@"memory": ".*"@"imgsize": "{{BOX_NBD_OVERLAY_RAM}}"@' $tofile + # "mountpoint": "/mnt/gentoo", + sed -i -e 's@"mountpoint": ".*"@"mountpoint": "{{BOX_NBD_MP}}"@' $tofile + # "imagename": null, + sed -i -e 's@"imagename": ".*"@"imagename": "{{BOX_NBD_OVERLAY_NAME}}"@' $tofile + # "initsys": "openrc", + sed -i -e 's@"initsys": ".*"@"initsys: "{{BOX_NBD_BASE_PROFILE}}"@' $tofile + # "iso": null, + # "portage": null, + sed -i -e 's@""portage": ".*"@""portage: "{{BOX_NBD_PORTAGE_FILE}}"@' $tofile + # "stage3": null, + sed -i -e 's@""stage3": ".*"@""stage3: "{{BOX_NBD_STAGE3_FILE}}"@' $tofile + # "partition": 1 + sed -i -e 's@"partition": ".*"@"partition": 3@' $tofile # but this is crucial # "disk": "/dev/sda", - sed -i -e 's@"disk": ".*"@"disk": "'{{BOX_NBD_DEV}}'"@' $tofile + sed -i -e 's@"disk": ".*"@"disk": "{{BOX_NBD_DEV}}"@' $tofile grep {{BOX_NBD_DEV}} $tofile || exit 4 fi diff --git a/roles/toxcore/files/gentoo.xml b/roles/toxcore/files/gentoo.xml new file mode 100644 index 0000000..931e082 --- /dev/null +++ b/roles/toxcore/files/gentoo.xml @@ -0,0 +1,193 @@ + + + + gentoo + 1362f7fd-ea55-4f2f-91c5-3b61ec26c1a0 + + + + + + 3121152 + 2097152 + + + + + 1 + + hvm + + + + + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + + + + +
+ + + + + +
+ + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + +
+ + + + + +
+ + + + + + + + + + + +
+ + + +
+ + +
+ + + + + + + + +
+ +