Added new config module for setting/retrieving vm configs

This commit is contained in:
Chad Nelson 2023-11-26 16:53:25 -07:00
parent 9a2a3ff3f2
commit 15218de075
4 changed files with 105 additions and 36 deletions

View file

@ -33,6 +33,7 @@ SUFFIXES = .scm .go
$(AM_V_GEN)$(top_builddir)/pre-inst-env $(GUILE_TOOLS) compile $(GUILE_TARGET) $(GUILE_WARNINGS) -o "$@" "$<" $(AM_V_GEN)$(top_builddir)/pre-inst-env $(GUILE_TOOLS) compile $(GUILE_TARGET) $(GUILE_WARNINGS) -o "$@" "$<"
SOURCES = msg.scm \ SOURCES = msg.scm \
msg/config.scm \
msg/machine.scm \ msg/machine.scm \
msg/hconfig.scm \ msg/hconfig.scm \
msg/shell.scm \ msg/shell.scm \

View file

@ -16,7 +16,8 @@
((scheme-file "msg") ((scheme-file "msg")
(directory (directory
"msg" "msg"
((scheme-file "machine") ((scheme-file "config")
(scheme-file "machine")
(scheme-file "hconfig") (scheme-file "hconfig")
(scheme-file "shell") (scheme-file "shell")
(scheme-file "helpers"))))) (scheme-file "helpers")))))

51
msg/config.scm Normal file
View file

@ -0,0 +1,51 @@
(define-module (msg config)
#:use-module (ice-9 ftw)
#:use-module (ice-9 rdelim)
#:declarative? #f
#:export (manage-config))
(define config-data #f)
;;; Function to load a config file
(define (load-config config-file)
(if (file-exists? config-file)
(call-with-input-file config-file
(lambda (port)
(read port)))
(begin
(display "Config file not found, creating default config")
(newline)
;; Return a default value (in this case, #f)
(values #f))))
;;; Function to save a config file
(define (save-config filename config)
(with-output-to-file filename
(lambda ()
(display (format #f "~A" config)))))
;;; Function to create a default config
(define (create-default-config )
'( ;; Default configuration values
(cpu . "4")
(mem . "4")
;; Add more default key-value pairs as needed
))
(define (manage-config filename)
(let* ((config (load-config filename))
(default-config (create-default-config)))
(cond
((eq? config #f)
(begin
(display "Creating default config...\n")
(save-config filename default-config)
(set! config default-config)
(values config)))
((unspecified? config)
(begin
(display "Invalid config file. Reverting to default config...\n")
(save-config filename default-config)
(set! config default-config)
(values config)))
(else
(values config)))))

View file

@ -4,28 +4,47 @@
#:use-module (ice-9 textual-ports) #:use-module (ice-9 textual-ports)
#:use-module (system foreign) #:use-module (system foreign)
#:use-module (msg helpers) #:use-module (msg helpers)
#:use-module (msg config)
#:export (machine)) #:export (machine))
(define home-path (getenv "HOME"))
(define arch (run-shell-command "uname -m"))
(define cpu-conf #f)
(define mem-conf #f)
(define* (load-config #:optional args)
(define config-file (format #f "~a/.guix/qemu/config.scm" home-path))
(define loaded-config (manage-config config-file))
(if (not (null? loaded-config))
(begin
(set! cpu-conf (cdr (assoc 'cpu loaded-config)))
(set! mem-conf (cdr (assoc 'mem loaded-config)))
#t)
#f))
(define* (machine #:optional (args (command-line))) (define* (machine #:optional (args (command-line)))
;;(define clean-args (cdr args)) ;;(define clean-args (cdr args))
(match args (match args
(("-h") (("-h")
(help)) (help))
(("init") (("init" rest ...)
(init)) (init rest))
(("reinit") (("reinit")
(clear-install) (clear-install)
(init)) (init))
(("start") (("start" rest ...)
(start)) (start rest))
(("stop") (("stop")
(stop)) (stop))
(_ (_
(display "Please enter a valid command or -h for help.\n")))) (display "Please enter a valid command or -h for help.\n"))))
(define home-path (getenv "HOME"))
(define arch (run-shell-command "uname -m"))
(define* (help) (define* (help)
(display "\nHere are the available commands:\n\n") (display "\nHere are the available commands:\n\n")
@ -37,7 +56,8 @@
(define response (read-line)) (define response (read-line))
(cond (cond
((string=? response "y") ((string=? response "y")
(system "rm -r ~/.guix")) (system "rm -r ~/.guix")
)
((string=? response "n") ((string=? response "n")
(display "Operation canceled.\n") (display "Operation canceled.\n")
(exit 0)) (exit 0))
@ -45,25 +65,26 @@
(display "Please enter a valid response.\n") (display "Please enter a valid response.\n")
(loop))))) (loop)))))
(define* (start) (define* (start #:optional args)
(define config (load-config))
(if (not (directory-exists? (format #f "~a/.guix" home-path))) (if (not (directory-exists? (format #f "~a/.guix" home-path)))
(begin (begin
(display "MSG not initialized. Please run `msg machine init` to continue.") (display "MSG not initialized. Please run `msg machine init` to continue. If you already tried to install and are receiving this message, try running `msg machine reinit`")
(exit 0))) (exit 0)))
(cond (cond
((string=? arch "arm64") ((string=? arch "arm64")
(system "/opt/homebrew/bin/qemu-system-aarch64 \ (system (format #f "/opt/homebrew/bin/qemu-system-aarch64 \
-machine virt,highmem=on \ -machine virt,highmem=on \
-accel hvf \ -accel hvf \
-cpu host \ -cpu host \
-smp 4 \ -smp ~a \
-display none \ -display none \
-hda $HOME/.guix/qemu/guix-user.qcow2 \ -hda $HOME/.guix/qemu/guix-user.qcow2 \
-m 4G \ -m ~aG \
-virtfs local,path=/Users,security_model=mapped,mount_tag=macos \ -virtfs local,path=$HOME/.guix/home,security_model=mapped,mount_tag=macos \
-bios /opt/homebrew/opt/qemu/share/qemu/edk2-aarch64-code.fd \ -bios /opt/homebrew/opt/qemu/share/qemu/edk2-aarch64-code.fd \
-device virtio-net,netdev=vmnic \ -device virtio-net,netdev=vmnic \
-netdev user,id=vmnic,hostfwd=tcp:127.0.0.1:9001-:22 &")) -netdev user,id=vmnic,hostfwd=tcp:127.0.0.1:9001-:22 &" cpu-conf mem-conf)))
((string=? arch "x86_64") ((string=? arch "x86_64")
;; (system "/usr/local/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-x86_64guix-user-x86.qcow2.tar.gz -O $HOME/.guix/qemu/guix.qcow2.tar.gz") ;; (system "/usr/local/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-x86_64guix-user-x86.qcow2.tar.gz -O $HOME/.guix/qemu/guix.qcow2.tar.gz")
;; (system "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/") ;; (system "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/")
@ -85,31 +106,22 @@
(define* (stop) (define* (stop)
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo shutdown'")) (system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo shutdown'"))
(define* (init)
(define* (init #:optional args)
(if (not (directory-exists? (format #f "~a/.guix" home-path))) (if (not (directory-exists? (format #f "~a/.guix" home-path)))
(begin (begin
;; Grab ssh cert for installer ;; Grab ssh cert for installer
(system "mkdir -p $HOME/.guix/qemu $HOME/.guix/home $HOME/.guix/ssh-cert") (system "mkdir -p $HOME/.guix/qemu $HOME/.guix/home $HOME/.guix/ssh-cert")
(system "ssh-keygen -R \"[127.0.0.1]:9001\"") (system "ssh-keygen -R \"[127.0.0.1]:9001\"")
;; Download system image and and start vm ;; Download system image and and start vm
(cond (cond
((string=? arch "arm64") ((string=? arch "arm64")
(system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-aarch64guix-installer.qcow2.tar.gz -O $HOME/.guix/qemu/guix.qcow2.tar.gz") (system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-aarch64guix-installer.qcow2.tar.gz -O $HOME/.guix/qemu/guix.qcow2.tar.gz")
(system "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/") (system "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/")
(system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-aarch64config.scm -O $HOME/.guix/home/config.scm") (system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-aarch64config.scm -O $HOME/.guix/home/config.scm")
(system "/opt/homebrew/bin/qemu-system-aarch64 \ (start)
-machine virt,highmem=on \ )
-accel hvf \
-cpu host \
-smp 8 \
-display none \
-hda $HOME/.guix/qemu/guix-user.qcow2 \
-m 8G \
-virtfs local,path=$HOME/.guix/home,security_model=mapped,mount_tag=macos \
-bios /opt/homebrew/opt/qemu/share/qemu/edk2-aarch64-code.fd \
-device virtio-net,netdev=vmnic \
-netdev user,id=vmnic,hostfwd=tcp:127.0.0.1:9001-:22 &"))
((string=? arch "x86_64") ((string=? arch "x86_64")
;; (system "/usr/local/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-x86_64guix-user-x86.qcow2.tar.gz -O $HOME/.guix/qemu/guix.qcow2.tar.gz") ;; (system "/usr/local/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-x86_64guix-user-x86.qcow2.tar.gz -O $HOME/.guix/qemu/guix.qcow2.tar.gz")
;; (system "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/") ;; (system "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/")
@ -142,6 +154,10 @@
(start) (start)
(sleep 15) (sleep 15)
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo mkdir /Users /boot/efi'") (system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo mkdir /Users /boot/efi'")
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo mount /dev/vda1 /boot/efi'")
(newline)
(display "Reconfiguring MSG, this may take a few minutes...")
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo guix system reconfigure --allow-downgrades /etc/config.scm'")
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo shutdown'") (system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo shutdown'")
(display "\nMSG is ready to be started. Run 'msg machine start' to begin.\n")) (display "\nMSG is ready to be started. Run 'msg machine start' to begin.\n"))
(display "MSG already initialized. Please run `msg machine reinit` if you would like to recreate it.")) (display "MSG already initialized. Please run `msg machine reinit` if you would like to recreate it."))