How to run a script from dom0 in a template?

Is it possible to run a script from dom0 in a template? I was playing around with qvm-run -u root template xfce4-terminal --command="bash -c 'command'" and was wondering if one could run a whole script which is residing in dom0 in a template.

1 Like

By default, no. There is an open issue about this (with some suggested workarounds):

2 Likes

You can trivially do this with a salt call to
cmd.script

salt://PATH_TO_SCRIPT;
  cmd.script
2 Likes

There are many things I do not understand about Salt, and this is yet another one, but it re-ignites my motivation to get a handle on salt. Thank you.

More generally, does anyone know some documentation source, or a heavily commented set of files which might have helped me discover this?

…and which would help me more generally to understand the concepts behind and underlying salt.

[ I’ve tried the salt upstream, and various other sites, but I found myself confused by a lot of the docs and guides which seem to confound the concepts I am familiar with, like execution, state, action, etc.
Maybe it’s more obvious if one comes from ansible or something similar, but I don’t… and every time I try to work out what is happening in Salt it makes me feel really stupid! ]

1 Like

Well, yes… with an artifice. Read on.

Yeah, I gave up on salt, and until Ansible is fully supported, I use dom0 scripts to set up custom templates (for Librewolf, Brave etc). To run scripts, I use a construct like this:

# [...]
# Let's say I want to run the script /home/user/scripts/setup_brave_tpl.sh ...
# ...  in a clone of debian-12-minimal called deb12min-brave

dom0scr="/home/user/scripts/setup_brave_tpl.sh"
bravetpl="deb12min-brave"

cat $dom0scr | \
  qvm-run --pass-io -u root $bravetpl "cat >> /root/setup.sh"

echo "chmod 755 /root/setup.sh && /root/setup.sh" | \
  qvm-run --pass-io -u root $bravetpl bash

# [...]

Error handling is weak, but I use this for simple things and salt error handling is not great either :smile:

EDIT: the source script is in dom0:/home/user/scripts, not in /root!

3 Likes

I apologise to @qubesfan35267 - I only meant to say thanks to Unman, but then I got carried away. I did not mean to hijack.

For my problem with salt I made a new topic, here :

Ok thank you for your replies, very helpful. But I wanted to keep it simple so I came up with a little work around that seems sufficient to me. I wrote a little script - it copies the script from dom0 to the template, executes it and afterwards it deletes everything:

#!/bin/bash
echo "Name template you want to setup:"
read qube
echo "Name path to script:"
read pathtoscript
qvm-run -u root $qube xterm -e "rm /home/user/QubesIncoming/dom0/*.*"
qvm-copy-to-vm $qube $pathtoscript
qvm-run -u root $qube xterm -e "bash /home/user/QubesIncoming/dom0/*.sh && rm /home/user/QubesIncoming/dom0/*.sh && rmdir /home/user/QubesIncoming/dom0 && poweroff"

Feel free to comment!

1 Like

I think I made a similar script once…

For sure, I made it to use command line arguments, because it means I can use ctrl-r key in bash shell to modify or to repeat my previous commands. (history command is very useful, but it took me years to discover ctrl-r, and it changed my life.)

If I had your script, it is 100% sure I will one day give the name of “the qube where I am doing a really important work assignment”, and poweroff will spoil my day. I would surely test that the name is for a template, and only do poweroff if it is true.

1 Like

Ah, the voice of experience :sweat_smile: Been there, too.

1 Like