Skip to content

Commit

Permalink
d-run: replace custom fakeroot by standard User
Browse files Browse the repository at this point in the history
use `-u 0:0` instead of -r
  • Loading branch information
xi committed Sep 8, 2023
1 parent e4c3c66 commit 759ff4e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This is the full list of values from docker's
[`config.json`](https://github.com/opencontainers/image-spec/blob/main/config.md)
that are actually used by `d-run`:

- `User`
- `Hostname`
- `WorkingDir`
- `Env`
Expand All @@ -22,7 +23,6 @@ that are actually used by `d-run`:
`d-run` uses the following additional values:

- `net` (bool) - enable networking (default: false)
- `fakeroot` (bool) - map UID and GID to 0 (default: false)
- `rw` (bool) - allow to modify the base image (default: false)

You are encouraged to modify this file, e.g. to add a volume or change the
Expand Down
33 changes: 25 additions & 8 deletions d-run
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import json
import argparse
import grp
import pwd


def make_volume(path, dir):
Expand All @@ -25,6 +27,21 @@ def make_volume(path, dir):
return [op, hostpath, path]


def parse_user(user):
uid = user
gid = None

if ':' in user:
uid, gid = uid.split(':', 1)
if not gid.isdigit():
gid = grp.getgrnam(gid).gr_gid

if not uid.isdigit():
uid = pwd.getpwnam(uid).pw_uid

return uid, gid


def build_cmd(dir, config):
cmd = [
'bwrap',
Expand Down Expand Up @@ -59,11 +76,11 @@ def build_cmd(dir, config):
if not config.get('rw'):
cmd += ['--remount-ro', '/']

if config.get('fakeroot'):
cmd += [
'--uid', '0',
'--gid', '0',
]
if config.get('User'):
uid, gid = parse_user(config['User'])
cmd += ['--uid', str(uid)]
if gid is not None:
cmd += ['--gid', str(gid)]

if config.get('Entrypoint'):
cmd += config['Entrypoint']
Expand All @@ -77,8 +94,8 @@ def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('dir')
parser.add_argument('-v', '--volume', action='append')
parser.add_argument('-u', '--user')
parser.add_argument('-n', '--net', action='store_true')
parser.add_argument('-r', '--fakeroot', action='store_true')
parser.add_argument('-w', '--rw', action='store_true')
parser.add_argument('cmd', nargs='*')
return parser.parse_args()
Expand All @@ -96,8 +113,8 @@ if __name__ == '__main__':
config['net'] = True
if args.rw:
config['rw'] = True
if args.fakeroot:
config['fakeroot'] = True
if args.user:
config['User'] = args.user
if args.volume:
if not config.get('Volumes'):
config['Volumes'] = {}
Expand Down

0 comments on commit 759ff4e

Please sign in to comment.