grand python rewrite + portable json system

This commit is contained in:
Patrick 2024-11-10 01:10:54 +01:00
parent c0f3491adc
commit de19951611
5 changed files with 163 additions and 214 deletions

View file

@ -0,0 +1,7 @@
{
"identifier": "noble-kubuntu-amd64",
"link": "https://cdimage.ubuntu.com/ubuntu-base/noble/daily/current/noble-base-amd64.tar.gz",
"download_name": "noble-base-amd64.tar.gz",
"packages": "grub-pc linux-image-generic kubuntu-desktop",
"script": "install.sh"
}

View file

@ -0,0 +1,7 @@
{
"identifier": "noble-server-amd64",
"link": "https://cdimage.ubuntu.com/ubuntu-base/noble/daily/current/noble-base-amd64.tar.gz",
"download_name": "noble-base-amd64.tar.gz",
"packages": "nginx grub-pc linux-image-generic",
"script": "install.sh"
}

View file

@ -0,0 +1,7 @@
{
"identifier": "noble-ubuntu-amd64",
"link": "https://cdimage.ubuntu.com/ubuntu-base/noble/daily/current/noble-base-amd64.tar.gz",
"download_name": "noble-base-amd64.tar.gz",
"packages": "grub-pc linux-image-generic ubuntu-desktop",
"script": "install.sh"
}

142
main.py Executable file
View file

@ -0,0 +1,142 @@
#!/usr/bin/python3
# linux-deployment-scripts
# Copyright (C) 2024 VM-Experiments
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import subprocess
import sys
import os
import json
downloads = "downloads/"
output = "output/"
chroot = "chroot/"
resources = "resources/"
installer_packages = "linux-image-generic grub-pc parted tar dosfstools util-linux e2fsprogs arch-install-scripts"
installer_amd64 = (
"https://cdimage.ubuntu.com/ubuntu-base/noble/daily/current/noble-base-amd64.tar.gz"
)
installer_amd64_name = "noble-base-amd64.tar.gz"
def command(command):
result = subprocess.run(command, capture_output=False, text=True, shell=True)
return result
# chroot associated functions
def prepare_chroot():
command(f"sudo mount --bind /dev '{chroot}/dev'")
command(f"sudo mount --bind /proc '{chroot}/proc'")
command(f"sudo mount --bind /sys '{chroot}/sys'")
command(f"sudo cp /etc/resolv.conf '{chroot}/etc/'")
def do_chroot_command(command_input):
command(f"sudo chroot '{chroot}' /bin/bash -c '{command_input}'")
def close_chroot():
command(f"sudo umount '{chroot}/dev'")
command(f"sudo umount '{chroot}/proc'")
command(f"sudo umount '{chroot}/sys'")
# handle rootfs image from internet
def rootfs_downloader(link, filename):
os.chdir(downloads)
command(f"wget -nc {link}")
command(f"sudo tar -xzf {filename} -C '../{chroot}'")
os.chdir("..")
def rootfs_creator(filename):
os.chdir(output)
command(f"sudo tar -czf {filename} -C '../{chroot}' .")
os.chdir("..")
# packaging rootfs
def rootfs_package(filename, installer):
infile = f"{filename}.tar.gz"
outfile = f"{filename}.iso"
close_chroot()
command(f"sudo rm -rf '{chroot}'")
command(f"sudo mkdir '{chroot}'")
rootfs_downloader(installer_amd64, installer_amd64_name)
command(f"sudo cp '{infile}' '{chroot}/rootfs.tar.gz'")
prepare_chroot()
do_chroot_command("apt update")
do_chroot_command(
f"DEBIAN_FRONTEND=noninteractive apt install -y {installer_packages}"
)
do_chroot_command("mkdir -p /etc/systemd/system/getty@tty1.service.d/")
do_chroot_command("touch /etc/systemd/system/getty@tty1.service.d/override.conf")
do_chroot_command(
'echo -e "[Service]\nExecStart=\nExecStart=/sbin/agetty --noclear --autologin root %I \\$TERM" | tee /etc/systemd/system/getty@tty1.service.d/override.conf'
)
do_chroot_command("systemctl daemon-reload")
do_chroot_command("systemctl enable getty@tty1.service")
close_chroot()
command(f"sudo cp '{resources}/grub.cfg' '{chroot}/boot/grub/grub.cfg'")
command(f"sudo cp '{resources}/{installer}' '{chroot}/root'")
command(f"echo './{installer}' | sudo tee -a '{chroot}/root/.profile'")
command(f"echo './{installer}' | sudo tee -a '{chroot}/root/.bashrc'")
command(f"sudo grub2-mkrescue -o '{outfile}' '{chroot}'")
# distro specific
def ubuntu_rootfs_prepare(link, filename, packages, outputname, script):
rootfs_downloader(link, filename)
prepare_chroot()
do_chroot_command("apt update -y")
do_chroot_command("apt full-upgrade -y")
do_chroot_command(f"DEBIAN_FRONTEND=noninteractive apt install -y {packages}")
close_chroot()
rootfs_creator(f"{outputname}.tar.gz")
rootfs_package(f"{output}{outputname}", script)
def main():
for index, arg in enumerate(sys.argv):
if index == 0:
continue
command(f"mkdir -p '{downloads}' '{output}'")
close_chroot()
command(f"sudo rm -rf '{chroot}'")
command(f"sudo mkdir '{chroot}'")
filename = arg
data = json.load(open(filename, "r"))
link = data["link"]
download_name = data["download_name"]
packages = data["packages"]
identifier = data["identifier"]
script = data["script"]
ubuntu_rootfs_prepare(link, download_name, packages, identifier, script)
close_chroot()
command(f"sudo rm -rf '{chroot}'")
main()

214
script.sh
View file

@ -1,214 +0,0 @@
#!/bin/bash
# linux-deployment-scripts
# Copyright (C) 2024 VM-Experiments
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# hardcoded paths
downloads="downloads/"
output="output/"
chroot="chroot/"
resources="resources/"
# hardcoded package lists
ubuntu_server_packages="nginx grub-pc linux-image-generic"
ubuntu_client_packages="grub-pc linux-image-generic"
ubuntu_client_gnome_packages="ubuntu-desktop"
ubuntu_client_kde_packages="kubuntu-desktop"
installer_packages="linux-image-generic grub-pc parted tar dosfstools util-linux e2fsprogs arch-install-scripts"
# hardcoded links
noble_amd64="https://cdimage.ubuntu.com/ubuntu-base/noble/daily/current/noble-base-amd64.tar.gz"
noble_amd64_name="noble-base-amd64.tar.gz"
jammy_amd64="https://cdimage.ubuntu.com/ubuntu-base/jammy/daily/current/jammy-base-amd64.tar.gz"
jammy_amd64_name="jammy-base-amd64.tar.gz"
# hardcoded names
ubuntu_noble_server_output="noble-server-amd64"
ubuntu_noble_ubuntu_output="noble-ubuntu-amd64"
ubuntu_noble_kubuntu_output="noble-kubuntu-amd64"
ubuntu_jammy_server_output="jammy-server-amd64"
ubuntu_jammy_ubuntu_output="jammy-ubuntu-amd64"
ubuntu_jammy_kubuntu_output="jammy-kubuntu-amd64"
# creates a menu
menu() {
iter=1
echo "$1"
echo ""
shift
for arg in "$@"; do
echo "$iter) $arg"
iter=$((iter + 1))
done
read option
}
# chroot associated functions
prepare_chroot() {
sudo mount --bind /dev "$chroot/dev"
sudo mount --bind /proc "$chroot/proc"
sudo mount --bind /sys "$chroot/sys"
sudo cp /etc/resolv.conf "$chroot/etc/"
}
do_chroot_command() {
sudo chroot "$chroot" /bin/bash -c "$*"
}
do_chroot() {
prepare_chroot "$chroot"
sudo chroot "$chroot"
close_chroot "$chroot"
}
close_chroot() {
sudo umount "$chroot/dev"
sudo umount "$chroot/proc"
sudo umount "$chroot/sys"
}
# handle rootfs image from internet
rootfs_downloader() {
cd "$downloads"
wget -nc "$1"
sudo tar -xzf "$2" -C "../$chroot"
cd ..
}
rootfs_creator() {
cd $output
sudo tar -czf "$1" -C "../$chroot" .
cd ..
}
# packaging rootfs
rootfs_package() {
infile="$1.tar.gz"
outfile="$1.iso"
close_chroot
sudo rm -rf "$chroot"
sudo mkdir "$chroot"
rootfs_downloader "$noble_amd64" "$noble_amd64_name"
sudo cp "$infile" "$chroot/rootfs.tar.gz"
prepare_chroot
do_chroot_command "apt update"
do_chroot_command "DEBIAN_FRONTEND=noninteractive apt install -y $installer_packages"
do_chroot_command "mkdir -p /etc/systemd/system/getty@tty1.service.d/"
do_chroot_command "touch /etc/systemd/system/getty@tty1.service.d/override.conf"
do_chroot_command "echo -e '[Service]\nExecStart=\nExecStart=/sbin/agetty --noclear --autologin root %I \$TERM' | tee /etc/systemd/system/getty@tty1.service.d/override.conf"
do_chroot_command "systemctl daemon-reload"
do_chroot_command "systemctl enable getty@tty1.service"
close_chroot
sudo cp "$resources/grub.cfg" "$chroot/boot/grub/grub.cfg"
sudo cp "$resources/install.sh" "$chroot/root"
echo "./install.sh" | sudo tee -a "$chroot/root/.profile"
echo "./install.sh" | sudo tee -a "$chroot/root/.bashrc"
sudo grub2-mkrescue -o "$outfile" "$chroot"
}
# distro specific
ubuntu_rootfs_prepare() {
rootfs_downloader "$1" "$2"
prepare_chroot
do_chroot_command "apt update -y"
do_chroot_command "apt full-upgrade -y"
do_chroot_command "DEBIAN_FRONTEND=noninteractive apt install -y $3"
close_chroot
rootfs_creator "$4.tar.gz"
rootfs_package "$output$4"
}
main() {
mkdir -p "$downloads" "$output"
close_chroot
sudo rm -rf "$chroot"
sudo mkdir "$chroot"
clear
menu "Welcome to the Ubuntu installer creation script. Choose an option." "Ubuntu 24.04" "Ubuntu 22.04"
case $option in
1)
menu "Choose an option." "Server" "Client"
case $option in
1)
ubuntu_rootfs_prepare "$noble_amd64" "$noble_amd64_name" "$ubuntu_server_packages" "$ubuntu_noble_server_output"
;;
2)
menu "Choose an option." "Ubuntu" "Kubuntu"
case $option in
1)
ubuntu_rootfs_prepare "$noble_amd64" "$noble_amd64_name" "$ubuntu_client_packages $ubuntu_client_gnome_packages" "$ubuntu_noble_ubuntu_output"
;;
2)
ubuntu_rootfs_prepare "$noble_amd64" "$noble_amd64_name" "$ubuntu_client_packages $ubuntu_client_kde_packages" "$ubuntu_noble_kubuntu_output"
;;
*) ;;
esac
;;
*) ;;
esac
;;
2)
menu "Choose an option." "Server" "Client"
case $option in
1)
ubuntu_rootfs_prepare "$jammy_amd64" "$jammy_amd64_name" "$ubuntu_server_packages" "$ubuntu_jammy_server_output"
;;
2)
menu "Choose an option." "Ubuntu" "Kubuntu"
case $option in
1)
ubuntu_rootfs_prepare "$jammy_amd64" "$jammy_amd64_name" "$ubuntu_client_packages $ubuntu_client_gnome_packages" "$ubuntu_jammy_ubuntu_output"
;;
2)
ubuntu_rootfs_prepare "$jammy_amd64" "$jammy_amd64_name" "$ubuntu_client_packages $ubuntu_client_kde_packages" "$ubuntu_jammy_kubuntu_output"
;;
*) ;;
esac
;;
*) ;;
esac
;;
*) ;;
esac
}
main $*