Skip to content

run: Add a --script-dir to run user-defined init-scripts #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions virtme/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def make_parser() -> argparse.ArgumentParser:
help='Run a one-line shell script in the guest.')
g.add_argument('--script-exec', action='store', metavar='BINARY',
help='Run the specified binary in the guest.')
g.add_argument('--script-dir', action='store', metavar='SHELL_COMMAND',
help='Run user init scripts in the guest.')

g = parser.add_argument_group(
title='Architecture',
Expand Down Expand Up @@ -317,8 +319,14 @@ def do_it() -> int:
'virtme.guesttools')

initcmds = ['mkdir -p /run/virtme/guesttools',
'/bin/mount -n -t 9p -o ro,version=9p2000.L,trans=virtio,access=any virtme.guesttools /run/virtme/guesttools',
'exec /run/virtme/guesttools/virtme-init']
'/bin/mount -n -t 9p -o ro,version=9p2000.L,trans=virtio,access=any virtme.guesttools /run/virtme/guesttools']

if args.script_dir is not None:
export_virtfs(qemu, arch, qemuargs, os.path.abspath(args.script_dir), 'virtme.userscriptdir')
initcmds.append('mkdir -p /run/virtme/user')
initcmds.append('/bin/mount -n -t 9p -o ro,version=9p2000.L,trans=virtio,access=any virtme.userscriptdir /run/virtme/user')

initcmds.append('exec /run/virtme/guesttools/virtme-init')

# Arrange for modules to end up in the right place
if kernel.moddir is not None:
Expand Down
6 changes: 6 additions & 0 deletions virtme/guest/virtme-init
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ echo "virtme-init: console is $consdev"
install -d -m 0755 /tmp/roothome
export HOME=/tmp/roothome

if [[ -d /run/virtme/user ]]; then
for s in /run/virtme/user/*.sh; do
setsid $s 0<>"/dev/$consdev" 1>&0 2>&0
done
fi

# Bring up a functioning shell on the console. This is a bit magical:
# We have no controlling terminal because we're attached to a fake
# console device (probably something like /dev/console), which can't
Expand Down