Finished configuring the init/shell commands

This commit is contained in:
Chad Nelson 2023-11-25 09:51:23 -07:00
parent 235ec69321
commit c60fe9493f
9 changed files with 456 additions and 49 deletions

View file

@ -4,8 +4,7 @@
#:use-module (ice-9 rdelim)
#:use-module (system foreign)
#:export (directory-exists?
run-shell-command
start-command-in-background))
run-shell-command))
(define (directory-exists? path)
(catch 'system-error
@ -16,6 +15,6 @@
(define (run-shell-command command)
(let* ((port (open-input-pipe command))
(str (read-line port)))
;;(close-pipe port)
port))
(close-pipe port)
str))

View file

@ -17,63 +17,144 @@
(("reinit")
(clear-install)
(init))
(("start")
(start))
(("stop")
(stop))
(_
(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)
(display "\nHere are the available commands:\n\n")
(display "start: Start the MSG envrionment.\n"))
(define* (clear-install)
(display "This is a destructive command and will remove your entire guix instance. Proceed? (y/n)\n")
(define response (read-line))
(if (string=? response "y")
(begin
(system "rm -r ~/.guix"))
(display "Please enter a valid response.")))
(let loop ()
(display "This is a destructive command and will remove your entire Guix instance. Proceed? (y/n)\n")
(define response (read-line))
(cond
((string=? response "y")
(system "rm -r ~/.guix"))
((string=? response "n")
(display "Operation canceled.\n")
(exit 0))
(else
(display "Please enter a valid response.\n")
(loop)))))
(define* (init)
(define home-path (getenv "HOME"))
(define arch (run-shell-command "uname -m"))
(define* (start)
(if (not (directory-exists? (format #f "~a/.guix" home-path)))
(begin
(system "mkdir -p $HOME/.guix/qemu $HOME/.guix/home $HOME/.guix/ssh-cert")
;; Get msg image
(display "MSG not initialized. Please run `msg machine init` to continue.")
(exit 0)))
(cond
((string=? arch "arm64")
(system "/opt/homebrew/bin/qemu-system-aarch64 \
-machine virt,highmem=on \
-accel hvf \
-cpu host \
-smp 4 \
-display none \
-hda $HOME/.guix/qemu/guix-user.qcow2 \
-m 4G \
-virtfs local,path=/Users,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")
;; (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 "/usr/local/bin/qemu-system-x86_64 \
;; -machine type=q35,accel=hvf \
;; -smp 4 \
;; -hda $HOME/.guix/qemu/guix-user.qcow2 \
;; -m 4G \
;; -display none \
;; -cpu Nehalem \
;; -virtfs local,path=$HOME/.guix,security_model=mapped,mount_tag=macos \
;; -device virtio-net,netdev=vmnic \
;; -netdev user,id=vmnic,hostfwd=tcp:127.0.0.1:9001-:22")
(display "x86_64 is not currently supported")
(exit 0)))
(display "MSG has been started")
)
(cond ((string=? arch "arm64")
(system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg-system-aarch64guix-user.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/")
(define pid (run-shell-command "/opt/homebrew/bin/qemu-system-aarch64 \
-machine virt,highmem=on \
-accel hvf \
-cpu host \
-smp 4 \
-display none \
-hda $HOME/.guix/qemu/guix-user.qcow2 \
-m 4G \
-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 &"))
(display pid))
((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 "tar -xvzf $HOME/.guix/qemu/guix.qcow2.tar.gz -C $HOME/.guix/qemu/")
(system "/usr/local/bin/qemu-system-x86_64 \
-machine type=q35,accel=hvf \
-smp 4 \
-hda $HOME/.guix/qemu/guix-user.qcow2 \
-m 4G \
-display none \
-cpu Nehalem \
-virtfs local,path=$HOME/.guix,security_model=mapped,mount_tag=macos \
-device virtio-net,netdev=vmnic \
-netdev user,id=vmnic,hostfwd=tcp:127.0.0.1:9001-:22")
))
(define* (stop)
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo shutdown'"))
(define* (init)
(if (not (directory-exists? (format #f "~a/.guix" home-path)))
(begin
;; Grab ssh cert for installer
(system "mkdir -p $HOME/.guix/qemu $HOME/.guix/home $HOME/.guix/ssh-cert")
(system "ssh-keygen -R \"[127.0.0.1]:9001\"")
;; Download system image and and start vm
(cond
((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 "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/qemu-system-aarch64 \
-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")
;; (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 "/usr/local/bin/qemu-system-x86_64 \
;; -machine type=q35,accel=hvf \
;; -smp 4 \
;; -hda $HOME/.guix/qemu/guix-user.qcow2 \
;; -m 4G \
;; -display none \
;; -cpu Nehalem \
;; -virtfs local,path=$HOME/.guix,security_model=mapped,mount_tag=macos \
;; -device virtio-net,netdev=vmnic \
;; -netdev user,id=vmnic,hostfwd=tcp:127.0.0.1:9001-:22")
(display "x86_64 is not currently supported")
(exit 0)))
;; Get msg ssh-key
(system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg_rsa -O $HOME/.guix/ssh-cert/msg_rsa")))
(system "/opt/homebrew/bin/wget https://objectstorage.us-phoenix-1.oraclecloud.com/n/axfgkze2xif1/b/guix-system/o/msg_rsa -O $HOME/.guix/ssh-cert/msg_rsa")
(system "chmod 400 ~/.guix/ssh-cert/msg_rsa ")
(system "ssh -o StrictHostKeychecking=no -i $HOME/.guix/ssh-cert/msg_rsa root@127.0.0.1 -p 9001 'guix'"))
(update-uid)
(build-msg)
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa root@127.0.0.1 -p 9001 'shutdown'")
(system "ssh-keygen -R \"[127.0.0.1]:9001\"")
(sleep 30)
(system "rm ~/.guix/qemu/guix-user.qcow2")
(system "mv ~/.guix/home/guix-user.qcow2 ~/.guix/qemu/")
(start)
(sleep 15)
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001 'sudo mkdir /Users'")
(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 "MSG already initialized. Please run `msg machine reinit` if you would like to recreate it."))
)
(define (build-msg)
;; Build MSG
(define* filename (run-shell-command "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa root@127.0.0.1 -p 9001 'guix system image -t qcow2 --save-provenance --image-size=100G /etc/config.scm'"))
(system (format #f "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa root@127.0.0.1 -p 9001 'cp ~a /mnt/macos/guix-user.qcow2'" filename)))
(define (update-uid)
;; Update UID for msg image
(system "sed -i \"s/\\$UID/$(id -u)/g\" ~/.guix/home/config.scm")
(system "ssh -o StrictHostKeyChecking=no -i $HOME/.guix/ssh-cert/msg_rsa root@127.0.0.1 -p 9001 'cp /mnt/macos/config.scm /etc/config.scm'"))

5
msg/shell.scm Normal file
View file

@ -0,0 +1,5 @@
(define-module (msg shell)
#:export (shell))
(define* (shell)
(system "ssh -o StrictHostKeyChecking=no -X -i $HOME/.guix/ssh-cert/msg_rsa admin@127.0.0.1 -p 9001"))