From 75c3228c5eeded1be2fb71913ba5b72b12d61199 Mon Sep 17 00:00:00 2001 From: Artur Gurgul1 Date: Sat, 9 Aug 2025 18:10:34 +0200 Subject: [PATCH] add script for creating VM image --- bin/certbot | 7 +++ bin/make-winux | 122 +++++++++++++++++++++++++++++++++++++ bin/use | 1 + lib/certbot.rb | 13 ++++ lib/execute.rb | 7 +++ recipes/certbot/debian.yml | 21 +++++++ recipes/gcc.yml | 4 +- recipes/nginx/debian.yml | 23 +++++++ 8 files changed, 196 insertions(+), 2 deletions(-) create mode 100755 bin/certbot create mode 100755 bin/make-winux create mode 100644 bin/use create mode 100644 lib/certbot.rb create mode 100644 recipes/certbot/debian.yml create mode 100644 recipes/nginx/debian.yml diff --git a/bin/certbot b/bin/certbot new file mode 100755 index 0000000..d759648 --- /dev/null +++ b/bin/certbot @@ -0,0 +1,7 @@ +#!/usr/bin/env zsh + +export PYTHONPATH="/pkg/certbot/4.2.0/local/lib/python3.11/dist-packages" +export PATH="/pkg/certbot/4.2.0/local/bin:$PATH" + +/pkg/certbot/4.2.0/local/bin/certbot + diff --git a/bin/make-winux b/bin/make-winux new file mode 100755 index 0000000..777489c --- /dev/null +++ b/bin/make-winux @@ -0,0 +1,122 @@ +#!/usr/bin/env zsh + +export LINUX_DISK_IMG=/home/artur/winux/VM/linux/linux.img +export WINUX_DISK_IMG=./winux.img + +# Mount the image of oryginal +# + +# Mounting the image +function wxumount { + echo "unmounting :$1" + mkdir -p $1 + if mountpoint -q $1; then + echo "Unmounting $1..." + umount /mnt/winux + if [ $? -eq 0 ]; then + echo "Successfully unmounted $1." + else + echo "Failed to unmount $1." + fi + else + echo "$1 is not mounted." + fi +} + +wxumount /mnt/winux +wxumount /mnt/linux + +# (re)create an empty sparsed file +rm -rf $WINUX_DISK_IMG +dd if=/dev/zero of=$WINUX_DISK_IMG bs=1 count=0 seek=20G + +# Check size +# real size: du -h winux.img +# appeared as: ls -lh winux.img + + +# Copy boot + +IMG="/home/artur/winux/VM/linux/linux.img" + +SECTOR_SIZE=$(LC_ALL=C sfdisk -d "$LINUX_DISK_IMG" | awk 'match($0,/sector-size:\s*([0-9]+)/,m){print m[1]; exit}') +START=$(LC_ALL=C sfdisk -d "$LINUX_DISK_IMG" | awk 'match($0,/start=\s*([0-9]+)/,m){print m[1]; exit}') + +echo "Sector size: $SECTOR_SIZE" +echo "Start sector: $START" + + +dd if="$LINUX_DISK_IMG" of="$WINUX_DISK_IMG" bs="$SECTOR_SIZE" count="$START" conv=notrunc + +# Formatting the disk +# No altering the first 1MB +echo '2048,,83,*' | sudo sfdisk --no-reread $WINUX_DISK_IMG + +# List all loops: sudo losetup -a +WINUX_LOOP_DEV=$(sudo losetup --find --show $WINUX_DISK_IMG) +# Scan also for partitions: sudo losetup --find --show --partscan winux.img + +echo "Loop device: $WINUX_LOOP_DEV" + + + +export WINUX_LOOP_DEV="$(losetup --find --show -P $WINUX_LOOP_DEV)" + +mkfs.ext2 ${WINUX_LOOP_DEV}p1 + +mount ${WINUX_LOOP_DEV}p1 /mnt/winux + +## The image is formatted and ready to copy files over + + +export LINUX_LOOP_DEV="$(losetup --find --show --partscan $LINUX_DISK_IMG)" +mount ${LINUX_LOOP_DEV}p1 /mnt/linux + + +# copying files + +cd /mnt/linux +tar --numeric-owner --xattrs --acls -cpf - \ + --exclude='lost+found' \ + --exclude='var/log/*' \ + --exclude='var/tmp/*' \ + --exclude='tmp/*' \ + --exclude='var/cache/*' \ + --exclude='swapfile' \ + --exclude='dev/*' \ + --exclude='proc/*' \ + --exclude='sys/*' \ + --exclude='run/*' \ + . | tar --numeric-owner --xattrs --acls -xpf - -C /mnt/winux + + + +#### TESTTING CODE +losetup -j $WINUX_DISK_IMG + +# Clean up after building + +wxumount /mnt/winux +wxumount /mnt/linux + +for dev in $(losetup -j $WINUX_DISK_IMG | cut -d: -f1); do + losetup -d "$dev" +done + +for dev in $(losetup -j $LINUX_DISK_IMG | cut -d: -f1); do + losetup -d "$dev" +done + + +# Create an archive +# + +echo "making the archive" +zstd -T0 --ultra -22 winux.img -o winux.img.zst + +# decompression with +# unzstd --sparse winux.img.zst +# or +# zstd -d --sparse winux.img.zst -o winux.img + + diff --git a/bin/use b/bin/use new file mode 100644 index 0000000..c179399 --- /dev/null +++ b/bin/use @@ -0,0 +1 @@ +use python 3.11 diff --git a/lib/certbot.rb b/lib/certbot.rb new file mode 100644 index 0000000..32c3b39 --- /dev/null +++ b/lib/certbot.rb @@ -0,0 +1,13 @@ + +module Certbot + + def self.create_certificate(domain, wildcard = false) + command = "/home/artur/.dat/bin/certbot" + if wildcard + system "sudo #{command} certonly --manual --preferred-challenges=dns -d \"*.#{domain}\" -d \"#{domain}\"" + else + system "sudo #{command} --nginx -d #{domain}" + end + end + +end diff --git a/lib/execute.rb b/lib/execute.rb index 2bd9ed9..3418959 100644 --- a/lib/execute.rb +++ b/lib/execute.rb @@ -119,6 +119,13 @@ module Execute end end + def domain(name) + require 'certbot' + wildcard = name.strip.start_with?("*.") + domain = name.strip.delete_prefix("*.") + Certbot.create_certificate(domain, wildcard) + end + def user(*users) require 'user' users.each do |name| diff --git a/recipes/certbot/debian.yml b/recipes/certbot/debian.yml new file mode 100644 index 0000000..904e3fe --- /dev/null +++ b/recipes/certbot/debian.yml @@ -0,0 +1,21 @@ +environment: + PYTHONPATH: /home/artur/test + +dependencies: + - python + +packages: + - libffi-dev + - libssl-dev + +repository: + url: https://github.com/certbot/certbot + branch: v4.2.0 + version: 4.2.0 + +steps: + - $SUDO pip3 install --prefix=$PREFIX certbot + - $SUDO pip3 install --prefix=$PREFIX certbot-nginx + + # can be executed like: pip install --prefix=$PREFIX certbot certbot-nginx + # to see installed plugins: `certbot plugins` diff --git a/recipes/gcc.yml b/recipes/gcc.yml index 0cd1029..f668a08 100644 --- a/recipes/gcc.yml +++ b/recipes/gcc.yml @@ -14,6 +14,6 @@ repository: steps: - ./contrib/download_prerequisites - - ./configure --prefix=$HOME/.local --enable-languages=c,c++ --disable-multilib + - ./configure --prefix=$PREFIX --enable-languages=c,c++ --disable-multilib - make -j$(nproc) - - make install + - $SUDO make install diff --git a/recipes/nginx/debian.yml b/recipes/nginx/debian.yml new file mode 100644 index 0000000..ed9772c --- /dev/null +++ b/recipes/nginx/debian.yml @@ -0,0 +1,23 @@ + +packages: + - build-essential + - libpcre3 + - libpcre3-dev + - zlib1g + - zlib1g-dev + - libssl-dev + +repository: + url: https://github.com/nginx/nginx.git + branch: release-1.29.0 + version: 1.29.0 + +build: + - ./configure \| + --prefix=$PREFIX \| + --with-http_ssl_module \| + --with-http_gzip_static_module + --with-stream + --with-http_v2_module + - make + - $SUDO make install