Last active
July 4, 2019 21:24
-
-
Save h0tw1r3/b50956d8dfb31864ed1d725b32bea032 to your computer and use it in GitHub Desktop.
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 python2 | |
# | |
# lxc.hook.pre-start | |
# | |
# remounts proxmox container mounts with noatime for performance | |
# | |
# copy this file to /usr/share/lxc/hooks/lxc-proxmox-noatime | |
# create /usr/share/lxc/config/common.conf.d/99-mount-noatime.conf | |
# with contents: lxc.hook.pre-mount /usr/share/lxc/hooks/lxc-proxmox-noatime | |
import argparse | |
import ctypes | |
import ctypes.util | |
import os | |
import platform | |
import psutil | |
import sys | |
import logging | |
import logging.handlers | |
from logging.handlers import SysLogHandler | |
MS_MGC_VAL = 0xc0ed0000 # Magic flag number to indicate "new" flags | |
MS_NOATIME = 1024 | |
MS_REMOUNT = 32 | |
libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True) | |
libc.mount.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_ulong, ctypes.c_char_p) | |
rootfs = os.environ['LXC_ROOTFS_PATH'] | |
if 'LXC_NAME' in os.environ: | |
hostname = os.environ['LXC_NAME'] | |
else: | |
hostname = platform.node().split('.')[0] | |
logger = logging.getLogger(os.path.basename(sys.argv[0])) | |
logger.setLevel(logging.DEBUG) | |
syslog_handler = SysLogHandler( | |
address='/dev/log', | |
facility=SysLogHandler.LOG_DAEMON | |
) | |
syslog_handler.setFormatter( | |
logging.Formatter('%(name)s: vmid={0} %(message)s'.format(hostname)) | |
) | |
logger.addHandler(syslog_handler) | |
def parse_arguments(): | |
parser = argparse.ArgumentParser(description='NMI mount pre-start container hooks') | |
parser.add_argument('vmid', | |
help='container name', type=str) | |
parser.add_argument('section', | |
help='always "lxc"', type=str) | |
parser.add_argument('type', | |
help='hook type', type=str) | |
return parser.parse_args() | |
def remount_noatime(source, target, fs): | |
ret = libc.mount(source, target, fs, MS_REMOUNT | MS_NOATIME, '') | |
if ret < 0: | |
errno = ctypes.get_errno() | |
raise OSError(errno, "Error remounting {} with noatime: {}". | |
format(target, os.strerror(errno))) | |
if __name__ == '__main__': | |
try: | |
args = parse_arguments() | |
partitions = psutil.disk_partitions() | |
for p in partitions: | |
if p.mountpoint.startswith(rootfs): | |
logger.info('remounting {0} with noatime'.format(p.mountpoint)) | |
remount_noatime(p.device, p.mountpoint, p.fstype) | |
except Exception: | |
logger.exception('failed to pre-start mounts') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment