From f5a42ddb1eaa2ff6843cc2f6c7dea38202179ab0 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Tue, 5 Dec 2023 22:22:21 +0100 Subject: [PATCH] =?UTF-8?q?qemush=20:=20suppression,=20maintenant=20h?= =?UTF-8?q?=C3=A9berg=C3=A9=20dans=20le=20repo=20`ahurac/qemush`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/qemush | 124 ----------------------------------------- qemu/bin/linux | 8 --- qemu/bin/linux-spice | 8 --- qemu/bin/redox | 10 ---- qemu/bin/redox-spice | 4 -- qemu/bin/windows | 9 --- qemu/bin/windows-spice | 7 --- src/first-free-port.c | 112 ------------------------------------- 8 files changed, 282 deletions(-) delete mode 100755 bin/qemush delete mode 100755 qemu/bin/linux delete mode 100755 qemu/bin/linux-spice delete mode 100755 qemu/bin/redox delete mode 100755 qemu/bin/redox-spice delete mode 100755 qemu/bin/windows delete mode 100755 qemu/bin/windows-spice delete mode 100644 src/first-free-port.c diff --git a/bin/qemush b/bin/qemush deleted file mode 100755 index b1ce775..0000000 --- a/bin/qemush +++ /dev/null @@ -1,124 +0,0 @@ -#!/bin/bash - -exec_as() { - local user - user="$1" - shift - - if [ "$(whoami)" != "$user" ]; then - exec sudo -u "$user" "$0" "$@" - else - cd || exit - fi -} - -exec_as qemu "$@" - -bin=bin -images=images -if [ -t 1 ]; then - cat=src-hilite-lesspipe.sh -else - cat='cat' -fi -PATH="./${bin}:${PATH}" -EDITOR="${EDITOR:-nvim}" -alias ls='ls --color=auto' -alias exec='exec ' -shopt -s expand_aliases -umask 027 - -perror() { - >&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*" -} - -public_help() { - local name - name="$(basename "$0")" - exec cat << EOF -${name}: usage: - ${name} active - (default behaviour) list active VMs - ${name} start - start a VM - ${name} watch - attach VM screen session - ${name} ls - list available VMs - ${name} edit - edit VM launching script - ${name} rm - delete launch script - ${name} diskls - list available disk images - ${name} diskadd - create a disk image - ${name} diskrm - delete disk image - ${name} shell - start a shell as user qemu - ${name} help - show this help -EOF -} - -error_usage() { - perror "Invalid usage" - >&2 public_help - return 1 -} - -public_start() { - local vm_name="$1" - shift - - exec screen -S "$vm_name" "$vm_name" "$@" -} - -public_watch() { - exec screen -dr "$1" -} - -public_active() { - exec screen -ls -} - -public_ls() { - echo "Available machines:" - exec ls "$bin" -} - -diskpath() { - printf "%s.qcow2" "${images}/${1}" -} - -public_diskadd() { - exec qemu-img create -f qcow2 "$(diskpath "$1")" "$2" -} - -public_diskrm() { - exec rm -vi -- "$(diskpath "$1")" -} - -public_diskls() { - echo "Available disks:" - exec ls "$images" -} - -public_edit() { - local file="${bin}/${1}" - "$EDITOR" "$file" - exec chmod u+x "$file" -} - -public_rm() { - exec rm -vi -- "${bin}/${1}" -} - -public_shell() { - exec bash -i -} - -public_cat() { - exec "$cat" "${bin}/${1}" -} - -function="$1" -shift - -if [ -z "$function" ]; then - public_active -elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then - "public_${function}" "$@" -else - error_usage -fi diff --git a/qemu/bin/linux b/qemu/bin/linux deleted file mode 100755 index 513afbc..0000000 --- a/qemu/bin/linux +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -x -qemu-system-x86_64 \ - -enable-kvm \ - -M q35 \ - -cpu host -smp "$(("$(nproc)" / 2))" \ - -m "$(("$(free | awk '($1 == "Mem:") { print $2 }')" / 2))K" \ - -net nic \ - "$@" diff --git a/qemu/bin/linux-spice b/qemu/bin/linux-spice deleted file mode 100755 index d38aeb8..0000000 --- a/qemu/bin/linux-spice +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -x -exec linux \ - -vga qxl \ - -device virtio-serial \ - -chardev spicevmc,id=vdagent,debug=0,name=vdagent \ - -device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \ - -spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \ - "$@" diff --git a/qemu/bin/redox b/qemu/bin/redox deleted file mode 100755 index d801324..0000000 --- a/qemu/bin/redox +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh -x -exec qemu-system-x86_64 \ - -enable-kvm \ - -M q35 \ - -cpu host -smp "$(("$(nproc)" / 2))" \ - -m "$(("$(free | grep '^Mem:\s' | awk '{ print $NF }')" / 2))K" \ - -device e1000,netdev=net0 \ - -device nec-usb-xhci,id=xhci \ - -device ich9-intel-hda -device hda-duplex \ - "$@" diff --git a/qemu/bin/redox-spice b/qemu/bin/redox-spice deleted file mode 100755 index f550aa6..0000000 --- a/qemu/bin/redox-spice +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -x -exec redox \ - -vga qxl -spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \ - "$@" diff --git a/qemu/bin/windows b/qemu/bin/windows deleted file mode 100755 index acf4f15..0000000 --- a/qemu/bin/windows +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh -x -qemu-system-x86_64 \ - -enable-kvm \ - -cpu host -smp "$(("$(nproc)" / 2))" \ - -m "$(("$(free | grep '^Mem: ' | awk '{ print $NF }')" / 2))K" \ - -net nic \ - -device virtio-serial \ - -usbdevice tablet \ - "$@" diff --git a/qemu/bin/windows-spice b/qemu/bin/windows-spice deleted file mode 100755 index da49e67..0000000 --- a/qemu/bin/windows-spice +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -x -exec windows \ - -vga qxl \ - -spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \ - -chardev spicevmc,id=vdagent,name=vdagent \ - -device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \ - "$@" diff --git a/src/first-free-port.c b/src/first-free-port.c deleted file mode 100644 index c823684..0000000 --- a/src/first-free-port.c +++ /dev/null @@ -1,112 +0,0 @@ -#include -#include -#include -#include -#include - -#define MIN_TCP_PORT 1 -#define TCP_TABLE "/proc/net/tcp" -#define TCP_TABLE_LINE_LENGTH 151 -#define LOCALHOST_HEX "0100007F" -#define WILDCARD_HEX "00000000" -#define LISTENING_HEX "0A" -#define PORTS_BLOCKS_TO_ALLOW 4 - -/** - * Get all the listening TCP ports - */ -static unsigned short *get_listening_ports(unsigned short *listening_ports, FILE *tcp_table_fptr) { - char line[TCP_TABLE_LINE_LENGTH]; - char delimiter[] = " "; - unsigned short len = 0; - char *address; - char *state; - char *field; - size_t allowed_for_ports = PORTS_BLOCKS_TO_ALLOW; - - // Skip first line (header) - fgets(line, sizeof(line), tcp_table_fptr); - - // Tokenize lines one by one - while (fgets(line, sizeof(line), tcp_table_fptr) != NULL) { - strtok(line, delimiter); - - address = strtok(NULL, delimiter); - strtok(NULL, delimiter); - state = strtok(NULL, delimiter); - field = strtok(NULL, delimiter); - - while (field != NULL) { - field = strtok(NULL, delimiter); - } - - if ((!strncmp(address, LOCALHOST_HEX, 8) || !strncmp(address, WILDCARD_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) { - if (len == allowed_for_ports) { - allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW; - listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short)); - } - listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16); - len++; - } - } - - if (len == allowed_for_ports) { - allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW; - listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short)); - } - listening_ports[len] = 0; - - return listening_ports; -} - -static unsigned char is_port_available(const unsigned short port, const unsigned short *listening_ports) { - unsigned short i = 0; - while (listening_ports[i] != 0 && listening_ports[i] != port) { - i++; - } - return listening_ports[i] == 0; -} - -static inline unsigned char is_valid_tcp_port(const unsigned short tcp_port) { - return tcp_port >= MIN_TCP_PORT; -} - -int main(int argc, char *argv[]) { - unsigned short current_port; - - if (argc >= 2) { - current_port = atoi(argv[1]); - } else { - current_port = 0; - } - - if (!is_valid_tcp_port(current_port)) { - fprintf(stderr, "Provide a valid TCP port number as first argument.\n"); - return EX_USAGE; - } - - // Open TCP table - FILE *tcp_table_fptr = fopen(TCP_TABLE, "r"); - if (tcp_table_fptr == NULL) { - fprintf(stderr, "Error opening the TCP table.\n"); - return EX_OSFILE; - } - - unsigned short *listening_ports = malloc(PORTS_BLOCKS_TO_ALLOW * sizeof(unsigned short)); - listening_ports = get_listening_ports(listening_ports, tcp_table_fptr); - - // Check if the current port is available, add - while (!is_port_available(current_port, listening_ports)) { - current_port++; - } - - free(listening_ports); - - if (is_valid_tcp_port(current_port)) { - printf("%d\n", current_port); - return EXIT_SUCCESS; - } else { - fprintf(stderr, "No more ports available. How did you fuck up that bad ???\n"); - return EX_TEMPFAIL; - } -}