#!/usr/bin/python3 # -*- coding: utf-8 -*- import argparse import subprocess import sys parser = argparse.ArgumentParser(description='Print shifted UID / GID of an LXC user') parser.add_argument('--group', '-g', action='store_true', help='Get primary GID') parser.add_argument('container', help='LXC container in which to lookup') parser.add_argument('user', help='User to lookup') if len(sys.argv) < 2: parser.print_usage() sys.exit(1) args = parser.parse_args() # Check if lxc-ls prints back the container name is_running = subprocess.run(['lxc-ls', '--running', args.container], stdout=subprocess.PIPE, check=True) lxc_command = 'lxc-attach' if is_running else 'lxc-execute' # Run id command inside container id = subprocess.run([lxc_command, args.container, '--', 'id', '-g' if args.group else '-u', args.user], stdout=subprocess.PIPE, check=True) print(int(id.stdout) + 100000)