fix: update remote LXD scripts to use OVN backed NAT-ed networks.

This commit is contained in:
slawek committed 2026-11-22 19:27:53 +01:00
1 parent da889b4865
commit 5fff009320
9 files changed
+500 -19

No files matched your search

+64 -19
View File
@@ -3,12 +3,16 @@
The VM is launched on the remote (--remote or LXD_REMOTE) in --project with that project's
default profile. It gets the newest out/linux-manager_*_all.deb with the administrator admin and
a generated password, and a configuration from configs/ (core.json by default), whose @ETH0@ is
the interface of the VM's default route, @CIDR0@ its network and @PSK@ a generated key. The
password and the key are written to tmp/vm-NAME.env. The VM's leased address is pinned on its
NIC, and NAT proxy devices forward ports 7080 and 7081 of the host address --listen (or LXD_LISTEN,
by default the IPv4 address of the host of the remote's URL) to the same ports of the VM, and port
2222 to its SSH port. --remove deletes the VM and that file.
a generated password; in a test project made by lxd-project.py, which tmp/lxd-project-PROJECT.json
describes, it gets the package instead from that project's repository, as its apt source, so later
dev builds reach it as updates. It also gets a configuration from configs/ (core.json by
default), whose @ETH0@ is the interface of the VM's default route, @CIDR0@ its network and @PSK@
a generated key. The password and the key are written to tmp/vm-NAME.env. The VM's leased
address is pinned on its NIC, and ports 7080 and 7081 of a listen address lead to the same ports
of the VM, and port 2222 to its SSH port. In a test project the listen address is a network
forward's on the project's WAN, allocated from its uplink's routes; elsewhere NAT proxy devices
listen on the host address --listen (or LXD_LISTEN, by default the IPv4 address of the host of the
remote's URL). --remove deletes the VM, its forward and that file.
"""
import argparse
@@ -51,13 +55,17 @@ def main():
help='the image of the VM (LXD_IMAGE, default ubuntu:26.04)')
parser.add_argument('--config', default='core.json', help='the configuration from configs/ (default core.json)')
parser.add_argument('--listen', default=os.environ.get('LXD_LISTEN'),
help='the LXD host address the proxies listen on (LXD_LISTEN, default the IPv4 address '
'of the remote URL\'s host)')
help='outside a test project, the LXD host address the proxies listen on (LXD_LISTEN, '
'default the IPv4 address of the remote URL\'s host)')
parser.add_argument('--remove', action='store_true', help='delete the VM and its env file')
args = parser.parse_args()
if not args.remote:
parser.error('--remote or LXD_REMOTE is required')
if not args.remove and not args.listen:
project_file = ROOT / 'tmp' / f'lxd-project-{args.project}.json'
project = json.loads(project_file.read_text(encoding='utf-8')) if project_file.is_file() else None
if project and project['remote'] != args.remote:
sys.exit(f'{project_file} is of remote {project["remote"]}, not {args.remote}')
if not project and not args.remove and not args.listen:
# The default listen address is the IPv4 address of the remote's host in its lxc URL.
remotes = json.loads(subprocess.run(['lxc', 'remote', 'list', '--format', 'json'], capture_output=True,
text=True, check=True).stdout)
@@ -73,9 +81,20 @@ def main():
capture_output=True).returncode == 0
env = ROOT / 'tmp' / f'vm-{args.name}.env'
# In a test project the VM is reached through a network forward on the project's WAN, tagged with user.vm.
wan = f'{args.remote}:{project["networks"][0]}' if project else None
def forward_of_vm():
listing = subprocess.run(['lxc', 'network', 'forward', 'list', wan, '-f', 'json', '--project', args.project],
capture_output=True, text=True, check=True).stdout
return next((forward['listen_address'] for forward in json.loads(listing) or []
if forward.get('config', {}).get('user.vm') == args.name), None)
if args.remove:
if not exists:
sys.exit(f'no VM {where}')
if project and (listen := forward_of_vm()):
subprocess.run(['lxc', 'network', 'forward', 'delete', wan, listen, '--project', args.project], check=True)
subprocess.run(['lxc', 'delete', '--force', f'{args.remote}:{args.name}', '--project', args.project],
check=True)
env.unlink(missing_ok=True)
@@ -88,8 +107,9 @@ def main():
if not config.is_file():
parser.error(f'no configs/{args.config}')
debs = sorted((ROOT / 'out').glob('linux-manager_*_all.deb'))
if not debs:
if not project and not debs:
sys.exit('no out/linux-manager_*_all.deb; run ./build.sh first')
source = f'the repository {project["repository"]}' if project else debs[-1].name
password, psk = secrets.token_hex(16), secrets.token_hex(24)
env.parent.mkdir(parents=True, exist_ok=True)
@@ -97,7 +117,8 @@ def main():
env.chmod(0o600)
env.write_text(f'ADMIN_USERNAME=admin\nADMIN_PASSWORD={password}\nPSK={psk}\n', encoding='utf-8')
print(f'launching {where} from {args.image} with {debs[-1].name}; proxies listen on {args.listen}')
reached = f'a forward on {project["networks"][0]}' if project else f'proxies on {args.listen}'
print(f'launching {where} from {args.image} with {source}; reached through {reached}')
subprocess.run(['lxc', 'launch', args.image, f'{args.remote}:{args.name}', '--vm', '-c', 'limits.cpu=2',
'-c', 'limits.memory=4GiB',
'-c', 'cloud-init.user-data=' + (HERE / 'cloud-init' / 'server.yaml').read_text(encoding='utf-8'),
@@ -121,7 +142,7 @@ def main():
sys.exit(f'{args.name} has no default route or address; remove it with --remove')
nic, interface = route.group(1), ipaddress.IPv4Interface(address.group(1))
# Pin the leased address on the profile's NIC and proxy the host's --listen ports to the VM.
# Pin the leased address on the profile's NIC, then forward or proxy the ports to the VM.
mac = inside(args, 'cat', f'/sys/class/net/{nic}/address').stdout.strip()
expanded = subprocess.run(['lxc', 'config', 'show', '--expanded', f'{args.remote}:{args.name}',
'--project', args.project], capture_output=True, text=True, check=True).stdout
@@ -131,19 +152,43 @@ def main():
sys.exit(f'no LXD device of {args.name} has the MAC address {mac} of {nic}')
subprocess.run(['lxc', 'config', 'device', 'override', f'{args.remote}:{args.name}', device,
f'ipv4.address={interface.ip}', '--project', args.project], check=True)
for host_port, vm_port in PROXIES:
subprocess.run(['lxc', 'config', 'device', 'add', f'{args.remote}:{args.name}', f'proxy-{vm_port}', 'proxy',
f'listen=tcp:{args.listen}:{host_port}', f'connect=tcp:{interface.ip}:{vm_port}', 'nat=true',
if project:
# An OVN network refuses NAT proxies; the forward's listen address is allocated from the uplink's routes.
subprocess.run(['lxc', 'network', 'forward', 'create', wan, '--allocate=ipv4', f'user.vm={args.name}',
'--project', args.project], check=True)
args.listen = forward_of_vm()
if not args.listen:
sys.exit(f'no forward of {args.name} on {wan}; remove the VM with --remove')
for host_port, vm_port in PROXIES:
subprocess.run(['lxc', 'network', 'forward', 'port', 'add', wan, args.listen, 'tcp', str(host_port),
str(interface.ip), str(vm_port), '--project', args.project], check=True)
else:
for host_port, vm_port in PROXIES:
subprocess.run(['lxc', 'config', 'device', 'add', f'{args.remote}:{args.name}', f'proxy-{vm_port}',
'proxy', f'listen=tcp:{args.listen}:{host_port}', f'connect=tcp:{interface.ip}:{vm_port}',
'nat=true', '--project', args.project], check=True)
# Install the package with the test administrator.
if not push(args, debs[-1], '/tmp/linux-manager.deb', '0644'):
sys.exit(f'could not copy {debs[-1].name} into {args.name}')
# Install the package with the test administrator, from the test project's repository when there is one.
if project:
sources = (f'Types: deb\nURIs: {project["repository"]}\nSuites: resolute\nComponents: main\n'
f'Signed-By: /etc/apt/keyrings/linux-manager-test.gpg\n')
if (inside(args, 'mkdir', '-p', '/etc/apt/keyrings').returncode != 0 or
not push(args, project['key'], '/etc/apt/keyrings/linux-manager-test.gpg', '0644') or
inside(args, 'tee', '/etc/apt/sources.list.d/linux-manager-test.sources', input=sources).returncode != 0):
sys.exit(f'could not add the repository of project {args.project} to {args.name}')
updated = inside(args, 'apt-get', 'update')
if updated.returncode != 0:
sys.exit(f'apt-get update failed in {args.name}: {updated.stderr.strip()}')
package = 'linux-manager'
else:
if not push(args, debs[-1], '/tmp/linux-manager.deb', '0644'):
sys.exit(f'could not copy {debs[-1].name} into {args.name}')
package = '/tmp/linux-manager.deb'
preseed = (f'linux-manager linux-manager/admin_username string admin\n'
f'linux-manager linux-manager/admin_password password {password}\n')
if inside(args, 'debconf-set-selections', input=preseed).returncode != 0:
sys.exit('debconf-set-selections failed')
result = inside(args, 'env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', '/tmp/linux-manager.deb')
result = inside(args, 'env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', package)
log = ROOT / 'tmp' / f'vm-{args.name}-install.log'
log.write_text(result.stdout + result.stderr, encoding='utf-8')
if result.returncode != 0: