Last active
June 5, 2022 13:32
-
-
Save WinMin/07b5f30a8776ad72872ae4e140b0f2da to your computer and use it in GitHub Desktop.
MacOS subsystem Linux ( power by docker)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import docker | |
import os | |
import platform | |
import logging | |
import argparse | |
from distutils.dir_util import mkpath | |
APPNAME = 'msl' | |
APPAUTHOR = 'swing' | |
SUPPORTED_UBUNTU_VERSION_LTS = [ | |
# '14.04', Still many issues to be solved (version problems mostly) | |
'16.04', | |
'18.04', | |
'20.04', | |
'22.04', | |
] | |
client = docker.from_env() | |
container = client.containers | |
image = client.images | |
class ColorWrite(object): | |
COLOR_SET = { | |
'END': '\033[0m', | |
'yellow': '\033[38;5;226m', | |
'red': '\033[31m', | |
'blue': '\033[34m', | |
'magenta': '\033[35m', | |
'cyan': '\033[36m', | |
} | |
@staticmethod | |
def color_write(content, color): | |
print(ColorWrite.COLOR_SET[color] + content + ColorWrite.COLOR_SET['END']) | |
def colorwrite_init(): | |
for color in ColorWrite.COLOR_SET: | |
# Use default value for lambda to avoid lazy capture of closure | |
setattr(ColorWrite, color, staticmethod(lambda x, color=color: ColorWrite.color_write(x, color))) | |
# Static initialize ColorWrite | |
colorwrite_init() | |
def run_container(args): | |
if not args.ubuntu: | |
ubuntu = SUPPORTED_UBUNTU_VERSION_LTS[-1] | |
elif args.ubuntu in SUPPORTED_UBUNTU_VERSION_LTS: | |
ubuntu = args.ubuntu | |
if not args.directory: | |
args.directory = os.getenv('HOME') | |
if platform.system() == 'Darwin': | |
volumes = { | |
os.path.expanduser(args.directory) :{ | |
'bind':'/workhub', | |
'mode':'rw' | |
} | |
} | |
privileged = True if args.priv else False | |
print(privileged) | |
try: | |
running_container = container.run( | |
'beswing/swpwn:{}'.format(ubuntu), | |
'/bin/zsh', | |
cap_add=['SYS_ADMIN','SYS_PTRACE'], | |
security_opt=['seccomp:unconfined'], | |
detach=True, | |
tty=True, | |
volumes=volumes, | |
privileged=privileged, | |
network_mode = 'host', | |
name=args.name, | |
# remove=True, | |
) | |
except Exception as e: | |
logging.error('Failed to run : {}'.format(e)) | |
def check_container(): | |
try: | |
container_status = container.get('msl').status | |
if container_status != 'running': | |
container.get('msl').start() | |
logging.info('Container status: {}'.format(container.get('msl').status)) | |
return True | |
except Exception as e: | |
logging.error('some error: {}'.format(e)) | |
return False | |
def commit_container(): | |
try: | |
if check_container(): | |
msl_container = container.get('msl') | |
msl_container.commit(repository='beswing/swpwn',tag='22.04') | |
msl_container.stop() | |
msl_container.remove() | |
except Exception as e: | |
logging.error('some error: {}'.format(e)) | |
return False | |
def _attach_interactive(name,command): | |
cmd = "docker exec -it {} {}".format( | |
name,command | |
) | |
ColorWrite.yellow( | |
r''' | |
__________ .__ .__ _____ | |
\______ \__ _ ______ | | |__|/ ____\____ | |
| ___/\ \/ \/ / \| | | \ __\/ __ \ | |
| | \ / | \ |_| || | \ ___/ | |
|____| \/\_/|___| /____/__||__| \___ > | |
\/ \/ | |
no pwn no life | |
''' | |
) | |
os.system(cmd) | |
def main(args): | |
if check_container() == False: | |
run_container(args) | |
if args.commit ==True: | |
commit_container() | |
else: | |
mslCmd = ' '.join(args.run) | |
command = 'bash -c "cd \'{}\' ; {}"'.format(PATH, mslCmd) | |
logging.info('Running command: {}'.format(command)) | |
_attach_interactive(args.name, command) | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='MacOS subsystem Linux power by Docker') | |
parser.add_argument('--ubuntu', default='22.04',type=str, help='choice ubuntu version') | |
parser.add_argument('--name', default='msl', type=str, help='container name') | |
parser.add_argument('--priv', action='store_true', help='privileged boot , so you can use something like kvm') | |
parser.add_argument('--dir', dest='directory',type=str, help='shared directory to be mounted at /workhub') | |
parser.add_argument('--run', default=['zsh'], dest='run', type=str, help='run command', nargs='+') | |
parser.add_argument('--commit',action='store_true', help='commit container to image') | |
parser.add_argument('--loglevel', default='INFO', type=str, help='log level') | |
args = parser.parse_args() | |
# run_container(args) | |
LOGFORMAT = "[%(funcName)s() - %(filename)s:%(lineno)s ] %(message)s" | |
logging.basicConfig(level=args.loglevel, format=LOGFORMAT) | |
main(args) | |
if __name__ == '__main__': | |
PWD = os.getenv('PWD') | |
HOME = os.getenv('HOME') | |
PATH = PWD.replace(HOME, '/workhub') | |
parse_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment