Skip to content

Commit 8f1517f

Browse files
committed
Re-introducing USB passthough but using YAML argument "usb" instead of twiking with "video"
1 parent 508d1d3 commit 8f1517f

5 files changed

Lines changed: 885 additions & 2 deletions

File tree

pkg/cidata/cloud-config.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/cidata/cloud-config.yaml

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#cloud-config
2+
# vim:syntax=yaml
3+
4+
growpart:
5+
mode: auto
6+
devices: ['/']
7+
8+
{{- if eq .OS "FreeBSD" }}
9+
packages:
10+
# boot.sh depends on sudo.
11+
# TODO: consider replacing sudo with doas.
12+
# FIXME: The hostagent script depends on sudo too.
13+
# https://github.com/lima-vm/lima/issues/4594
14+
- sudo
15+
{{- end }}
16+
17+
{{- if .UpgradePackages }}
18+
package_update: true
19+
package_upgrade: true
20+
package_reboot_if_required: true
21+
{{- end }}
22+
23+
{{- if or .RosettaEnabled (and .Mounts (or (eq .MountType "9p") (eq .MountType "virtiofs"))) }}
24+
mounts:
25+
{{- if .RosettaEnabled }}{{/* Mount the rosetta volume before systemd-binfmt.service(8) starts */}}
26+
- [vz-rosetta, /mnt/lima-rosetta, virtiofs, defaults, "0", "0"]
27+
{{- end }}
28+
{{- if and .Mounts (or (eq .MountType "9p") (eq .MountType "virtiofs")) }}
29+
{{- range $m := $.Mounts}}
30+
- [{{$m.Tag}}, {{$m.MountPoint}}, {{$m.Type}}, "{{$m.Options}}", "0", "0"]
31+
{{- end }}
32+
{{- end }}
33+
{{- end }}
34+
35+
{{- if .TimeZone }}
36+
timezone: {{.TimeZone}}
37+
{{- end }}
38+
39+
users:
40+
- name: "{{.User}}"
41+
{{- if ne .OS "FreeBSD" }}
42+
# nuageinit does not support specifying the UID.
43+
# The UID is fixed up in boot.essential.FreeBSD/00-freebsd-user-group.sh
44+
uid: "{{.UID}}"
45+
{{- end }}
46+
{{- if .Comment }}
47+
gecos: {{ printf "%q" .Comment }}
48+
{{- end }}
49+
homedir: "{{.Home}}"
50+
shell: {{.Shell}}
51+
{{- if eq .OS "Darwin" }}
52+
{{/* On macOS, the password is not locked so as to allow GUI login. */}}
53+
{{/* Since the user can run sudo with their own password, basically we don't need to set up passwordless sudo. */}}
54+
{{/* However, it is still configured to allow `/sbin/shutdown -h now` without password, as it is invoked by `limactl stop` for graceful shutdown. */}}
55+
{{/* (Why doesn't macOS VM support graceful shutdown?) */}}
56+
sudo: ALL=(ALL) NOPASSWD:/sbin/shutdown -h now
57+
{{- else }}
58+
sudo: ALL=(ALL) NOPASSWD:ALL
59+
{{- if eq .OS "FreeBSD" }}
60+
groups:
61+
- wheel
62+
doas: permit nopass :wheel
63+
{{- end}}
64+
lock_passwd: true
65+
{{- end }}
66+
{{- if eq .OS "FreeBSD" }}
67+
ssh_authorized_keys:
68+
{{- else }}
69+
ssh-authorized-keys:
70+
{{- end }}
71+
{{- range $val := .SSHPubKeys }}
72+
- {{ printf "%q" $val }}
73+
{{- end }}
74+
75+
{{- if .BootScripts }}
76+
write_files:
77+
- content: |
78+
#!/bin/sh
79+
set -eux
80+
LIMA_CIDATA_MNT="/mnt/lima-cidata"
81+
UNAME="$(uname -s)"
82+
if [ "${UNAME}" = "Darwin" ]; then
83+
LIMA_CIDATA_MNT="/Volumes/cidata"
84+
# Should have been mounted automatically
85+
elif [ "${UNAME}" = "FreeBSD" ]; then
86+
LIMA_CIDATA_DEV="/dev/iso9660/cidata"
87+
if [ ! -e "${LIMA_CIDATA_DEV}" ]; then
88+
# When the iso is created with `hdiutil` on macOS,
89+
# apparently the volume name becomes "CIDATA" not "cidata"
90+
LIMA_CIDATA_DEV="/dev/iso9660/CIDATA"
91+
fi
92+
mkdir -p -m 700 "${LIMA_CIDATA_MNT}"
93+
mount_cd9660 -G wheel -U root -m 0700 -o ro,exec "${LIMA_CIDATA_DEV}" "${LIMA_CIDATA_MNT}"
94+
elif [ "${UNAME}" = "Linux" ]; then
95+
LIMA_CIDATA_DEV="/dev/disk/by-label/cidata"
96+
mkdir -p -m 700 "${LIMA_CIDATA_MNT}"
97+
mount -o ro,mode=0700,dmode=0700,overriderockperm,exec,uid=0 "${LIMA_CIDATA_DEV}" "${LIMA_CIDATA_MNT}"
98+
else
99+
echo "Unsupported OS: ${UNAME}" >&2
100+
exit 1
101+
fi
102+
export LIMA_CIDATA_MNT
103+
exec "${LIMA_CIDATA_MNT}"/boot.sh
104+
{{- if or (eq .OS "Darwin") (eq .OS "FreeBSD") }}
105+
owner: root:wheel
106+
{{- else }}
107+
owner: root:root
108+
{{- end }}
109+
{{- if eq .OS "FreeBSD" }}
110+
# nuageinit requires the path to be under an existing directory
111+
path: /usr/sbin/lima-freebsd-init.sh
112+
{{- else }}
113+
path: /var/lib/cloud/scripts/per-boot/00-lima.boot.sh
114+
{{- end }}
115+
permissions: '0755'
116+
{{- if eq .OS "FreeBSD" }}
117+
# nuageinit does not run /var/lib/cloud/scripts/per-boot/* scripts
118+
- content: |
119+
#!/bin/sh
120+
121+
# PROVIDE: lima_freebsd_init
122+
# REQUIRE: DAEMON
123+
# BEFORE: LOGIN
124+
125+
. /etc/rc.subr
126+
127+
name="lima_freebsd_init"
128+
rcvar="lima_freebsd_init_enable"
129+
command="/usr/sbin/lima-freebsd-init.sh"
130+
131+
load_rc_config "$name"
132+
run_rc_command "$1"
133+
owner: root:wheel
134+
path: /etc/rc.d/lima_freebsd_init
135+
permissions: '0755'
136+
- content: |
137+
lima_freebsd_init_enable="YES"
138+
owner: root:wheel
139+
path: /etc/rc.conf.d/lima_freebsd_init
140+
permissions: '0644'
141+
{{- end }}
142+
{{- end }}
143+
144+
{{- if .DNSAddresses }}
145+
# This has no effect on systems using systemd-resolved, but is used
146+
# on e.g. Alpine to set up /etc/resolv.conf on first boot.
147+
148+
manage_resolv_conf: true
149+
150+
resolv_conf:
151+
nameservers:
152+
{{- range $ns := $.DNSAddresses }}
153+
- {{$ns}}
154+
{{- end }}
155+
{{- end }}
156+
157+
{{- if or .CACerts.RemoveDefaults .CACerts.Trusted }}
158+
{{ with .CACerts }}
159+
ca_certs:
160+
{{- if .RemoveDefaults }}
161+
remove_defaults: {{ .RemoveDefaults }}
162+
{{- end }}
163+
{{- if .Trusted}}
164+
trusted:
165+
{{- range $cert := .Trusted }}
166+
- |
167+
{{- range $line := $cert.Lines }}
168+
{{ $line }}
169+
{{- end }}
170+
{{- end }}
171+
{{- end }}
172+
{{- end }}
173+
{{- end }}
174+
175+
{{- if .BootCmds }}
176+
bootcmd:
177+
{{- range $cmd := $.BootCmds }}
178+
- |
179+
# We need to embed the params.env as a here-doc because /mnt/lima-cidata is not yet mounted
180+
while read -r line; do [ -n "$line" ] && export "$line"; done <<'EOF'
181+
{{- range $key, $val := $.Param }}
182+
PARAM_{{ $key }}={{ $val }}
183+
{{- end }}
184+
EOF
185+
{{- range $line := $cmd.Lines }}
186+
{{ $line }}
187+
{{- end }}
188+
{{- end }}
189+
{{- end }}

pkg/driver/qemu/qemu.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,9 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er
852852
args = append(args, "-device", "virtio-keyboard-pci")
853853
args = append(args, "-device", "virtio-"+input+"-pci")
854854
args = append(args, "-device", "qemu-xhci,id=usb-bus")
855+
} else if *y.USB {
856+
// Add USB controller without display for USB device passthrough
857+
args = append(args, "-device", "qemu-xhci,id=usb-bus")
855858
}
856859

857860
// Parallel

pkg/limatype/lima_yaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type LimaYAML struct {
3535
Firmware Firmware `yaml:"firmware,omitempty" json:"firmware,omitempty"`
3636
Audio Audio `yaml:"audio,omitempty" json:"audio,omitempty"`
3737
Video Video `yaml:"video,omitempty" json:"video,omitempty"`
38+
USB *bool `yaml:"usb,omitempty" json:"usb,omitempty" jsonschema:"nullable"`
3839
Provision []Provision `yaml:"provision,omitempty" json:"provision,omitempty"`
3940
UpgradePackages *bool `yaml:"upgradePackages,omitempty" json:"upgradePackages,omitempty" jsonschema:"nullable"`
4041
Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"`

pkg/limayaml/default.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)