Example #1
0
def main():
    common.check_filesystem()

    pkgmngr = None
    for sfile in ('aptitude', 'aptitude-curses', 'synaptic', 'apper'):
        for sdir in ('bin', 'sbin', 'usr/bin', 'usr/sbin'):
            full_file = misc.join_paths(config.FILESYSTEM_DIR, sdir, sfile)
            if os.path.exists(full_file) and os.access(full_file, os.X_OK):
                pkgmngr = misc.join_paths(sdir, sfile)
                message.sub_debug('Package manager detected', sfile)

    if not pkgmngr:
        raise(message.exception('No package manager available'))

    if not pkgmngr.startswith('aptitude'):
        try:
            message.sub_info('Allowing local access to X-server')
            misc.system_command((misc.whereis('xhost'), '+local:'))

            message.sub_info('Executing package manager')
            misc.chroot_exec((pkgmngr))

        finally:
            message.sub_info('Blocking local access to X-server')
            misc.system_command((misc.whereis('xhost'), '-local:'))
    else:
        message.sub_info('Executing package manager')
        misc.chroot_exec((pkgmngr))
Example #2
0
def main():
    common.check_filesystem()

    message.sub_info('Detecting available sessions')
    xsession = None
    for sfile in misc.list_files(misc.join_paths(config.FILESYSTEM_DIR, \
        'usr/share/xsessions')):
        if sfile.endswith('.desktop'):
            xsession = common.get_value(sfile, 'Exec=')
            message.sub_debug('Session detected', xsession)

    if not xsession:
        raise (message.exception('No session avaialable'))

    # FIXME: race condition between session and Xephyr - if session
    # starts before Xephyr it fails saying it does not find the DISPLAY
    message.sub_info('Starting Xephyr')
    x = subprocess.Popen((misc.whereis('Xephyr'), '-ac', '-screen', \
        config.RESOLUTION, '-br', ':13'))
    x.poll()
    if x.returncode > 0:
        raise (message.exception('Failed to start Xephyr', x.returncode))

    try:
        message.sub_info('Allowing local access to X-server')
        misc.system_command((misc.whereis('xhost'), '+local:13'))

        message.sub_info('Starting nested X session', xsession)
        misc.chroot_exec((xsession), xnest=True)

        message.sub_info('Blocking local access to X-server')
        misc.system_command((misc.whereis('xhost'), '-local:13'))
    finally:
        message.sub_info('Terminating Xephyr')
        x.terminate()
Example #3
0
def main():
    common.check_filesystem()

    pkgmngr = None
    for sfile in ('aptitude', 'aptitude-curses', 'synaptic', 'apper'):
        for sdir in ('bin', 'sbin', 'usr/bin', 'usr/sbin'):
            full_file = misc.join_paths(config.FILESYSTEM_DIR, sdir, sfile)
            if os.path.exists(full_file) and os.access(full_file, os.X_OK):
                pkgmngr = misc.join_paths(sdir, sfile)
                message.sub_debug('Package manager detected', sfile)

    if not pkgmngr:
        raise (message.exception('No package manager available'))

    if not pkgmngr.startswith('aptitude'):
        try:
            message.sub_info('Allowing local access to X-server')
            misc.system_command((misc.whereis('xhost'), '+local:'))

            message.sub_info('Executing package manager')
            misc.chroot_exec((pkgmngr))

        finally:
            message.sub_info('Blocking local access to X-server')
            misc.system_command((misc.whereis('xhost'), '-local:'))
    else:
        message.sub_info('Executing package manager')
        misc.chroot_exec((pkgmngr))
Example #4
0
def main():
    common.check_filesystem()

    message.sub_info('Detecting available sessions')
    xsession = None
    for sfile in misc.list_files(misc.join_paths(config.FILESYSTEM_DIR, \
        'usr/share/xsessions')):
        if sfile.endswith('.desktop'):
            xsession = common.get_value(sfile, 'Exec=')
            message.sub_debug('Session detected', xsession)

    if not xsession:
        raise(message.exception('No session available'))

    # FIXME: race condition between session and Xephyr - if session
    # starts before Xephyr it fails saying it does not find the DISPLAY
    message.sub_info('Starting Xephyr')
    x = subprocess.Popen((misc.whereis('Xephyr'), '-ac', '-screen', \
        config.RESOLUTION, '-br', ':13'))
    x.poll()
    if x.returncode is not None and x.returncode > 0:
        raise(message.exception('Failed to start Xephyr', x.returncode))

    try:
        message.sub_info('Allowing local access to X-server')
        misc.system_command((misc.whereis('xhost'), '+local:13'))

        message.sub_info('Starting nested X session', xsession)
        misc.chroot_exec((xsession), xnest=True)

        message.sub_info('Blocking local access to X-server')
        misc.system_command((misc.whereis('xhost'), '-local:13'))
    finally:
        message.sub_info('Terminating Xephyr')
        x.terminate()
Example #5
0
def main():
    common.check_filesystem()

    message.sub_info('Gathering information')
    arch = misc.chroot_exec(('dpkg', '--print-architecture'), prepare=False, \
        mount=False, output=True)
    distrib = common.get_value(config.FILESYSTEM_DIR + '/etc/lsb-release', \
        'DISTRIB_ID=')
    release = common.get_value(config.FILESYSTEM_DIR + '/etc/lsb-release', \
        'DISTRIB_RELEASE=')
    message.sub_debug('Architecture', arch)
    message.sub_debug('Distribution (DISTRIB_ID)', distrib)
    message.sub_debug('Release (DISTRIB_RELEASE)', release)

    if isinstance(arch, bytes):  # For some reason this is of type 'bytes'.
        if int(sys.version_info[0]) >= 3:  # If we're running under python3
            arch = str(arch, 'utf-8')
        else:  # Otherwise just cast it to a str without the 'utf-8' option.
            arch = str(arch)
    
    iso_file = '%s/%s-%s-%s.iso' % (config.WORK_DIR, distrib, arch, release)
    if not os.path.exists(iso_file):
        raise(message.exception('ISO image does not exist', iso_file))

    message.sub_info('Running QEMU with ISO image', iso_file)
    host_arch = os.uname()[4]
    if host_arch == 'x86_64':
        qemu = misc.whereis('qemu-system-x86_64')
    else:
        qemu = misc.whereis('qemu-system-i386')
    if not qemu:
        raise(message.exception('QEMU is not installed'))

    qemu_kvm = False
    command = [qemu, '-m', config.VRAM, '-cdrom', iso_file]
    if misc.search_string('-enable-kvm', misc.get_output((qemu, '-h'))):
        qemu_kvm = True
    # CPU flag: "vmx" for Intel processors, "svm" for AMD processors
    # These flags are hidden in Xen environment
    # https://wiki.xenproject.org/wiki/Xen_Common_Problems
    host_kvm = False
    if os.path.exists('/dev/kvm') and os.path.exists('/proc/cpuinfo') and \
        misc.search_file('(?:\\s|^)flags.*(?:\\s)(vmx|svm)(?:\\s|$)', \
        '/proc/cpuinfo', escape=False):
        host_kvm = True
    if qemu_kvm and host_kvm:
        command.append('-enable-kvm')
    message.sub_debug('Host architecture', host_arch)
    message.sub_debug('QEMU KVM', qemu_kvm)
    message.sub_debug('Host KVM', host_kvm)
    misc.system_command(command)
Example #6
0
def unmount_iso():
    if not mount_dir:
        return
    message.sub_info('Unmounting', mount_dir)
    misc.system_command((misc.whereis('umount'), '-fl', mount_dir))

    message.sub_info('Removing', mount_dir)
    os.rmdir(mount_dir)
Example #7
0
def unmount_iso():
    if not mount_dir:
        return
    message.sub_info('Unmounting', mount_dir)
    misc.system_command((misc.whereis('umount'), '-fl', mount_dir))

    message.sub_info('Removing', mount_dir)
    os.rmdir(mount_dir)
Example #8
0
def main():
    common.check_filesystem()

    message.sub_info('Gathering information')
    arch = misc.chroot_exec(('dpkg', '--print-architecture'), prepare=False, \
        mount=False, output=True)
    distrib = common.get_value(config.FILESYSTEM_DIR + '/etc/lsb-release', \
        'DISTRIB_ID=')
    release = common.get_value(config.FILESYSTEM_DIR + '/etc/lsb-release', \
        'DISTRIB_RELEASE=')
    message.sub_debug('Architecture', arch)
    message.sub_debug('Distribution (DISTRIB_ID)', distrib)
    message.sub_debug('Release (DISTRIB_RELEASE)', release)

    iso_file = '%s/%s-%s-%s.iso' % (config.WORK_DIR, distrib, arch, release)
    if not os.path.exists(iso_file):
        raise(message.exception('ISO image does not exist', iso_file))

    message.sub_info('Running QEMU with ISO image', iso_file)
    host_arch = os.uname()[4]
    if host_arch == 'x86_64':
        qemu = misc.whereis('qemu-system-x86_64')
    else:
        qemu = misc.whereis('qemu-system-i386')
    if not qemu:
        raise(message.exception('QEMU is not installed'))

    qemu_kvm = False
    command = [qemu, '-m', config.VRAM, '-cdrom', iso_file]
    if misc.search_string('-enable-kvm', misc.get_output((qemu, '-h'))):
        qemu_kvm = True
    # CPU flag: "vmx" for Intel processors, "svm" for AMD processors
    # These flags are hidden in Xen environment
    # https://wiki.xenproject.org/wiki/Xen_Common_Problems
    host_kvm = False
    if os.path.exists('/dev/kvm') and os.path.exists('/proc/cpuinfo') and \
        misc.search_file('(?:\\s|^)flags.*(?:\\s)(vmx|svm)(?:\\s|$)', \
        '/proc/cpuinfo', escape=False):
        host_kvm = True
    if qemu_kvm and host_kvm:
        command.append('-enable-kvm')
    message.sub_debug('Host architecture', host_arch)
    message.sub_debug('QEMU KVM', qemu_kvm)
    message.sub_debug('Host KVM', host_kvm)
    misc.system_command(command)
Example #9
0
def main():
    common.check_filesystem()

    message.sub_info('Gathering information')
    arch = misc.chroot_exec(('dpkg', '--print-architecture'), prepare=False, \
        mount=False, output=True)
    distrib = common.get_value(config.FILESYSTEM_DIR + '/etc/lsb-release', \
        'DISTRIB_ID=')
    release = common.get_value(config.FILESYSTEM_DIR + '/etc/lsb-release', \
        'DISTRIB_RELEASE=')
    message.sub_debug('Architecture', arch)
    message.sub_debug('Distribution (DISTRIB_ID)', distrib)
    message.sub_debug('Release (DISTRIB_RELEASE)', release)

    iso_file = '%s/%s-%s-%s.iso' % (config.WORK_DIR, distrib, arch, release)
    if not os.path.exists(iso_file):
        raise (message.exception('ISO image does not exist', iso_file))

    message.sub_info('Running QEMU with ISO image', iso_file)
    host_arch = os.uname()[4]
    if host_arch == 'x86_64':
        qemu = misc.whereis('qemu-system-x86_64')
    else:
        qemu = misc.whereis('qemu-system-i386')
    if not qemu:
        raise (message.exception('QEMU is not installed'))

    qemu_kvm = False
    command = [qemu, '-m', config.VRAM, '-cdrom', iso_file]
    if misc.search_string('-enable-kvm', misc.get_output((qemu, '-h'))):
        qemu_kvm = True
    # vmx for intel, svm for amd processors. it will most likely not work in
    # XEN environment
    host_kvm = False
    if os.path.exists('/dev/kvm') and os.path.exists('/proc/cpuinfo') and \
        misc.search_file('(?:\\s|^)flags.*(?:\\s)(vme|vmx)(?:\\s|$)', \
        '/proc/cpuinfo', escape=False):
        host_kvm = True
    if qemu_kvm and host_kvm:
        command.append('-enable-kvm')
    message.sub_debug('Host architecture', host_arch)
    message.sub_debug('QEMU KVM', qemu_kvm)
    message.sub_debug('Host KVM', host_kvm)
    misc.system_command(command)
Example #10
0
def clean_work_dirs():
    # ensure that nothing is mounted to the working directories before cleanup,
    # see https://github.com/clearkimura/Customizer/issues/82
    if os.path.exists('/proc/mounts'):
        for line in misc.readlines_file('/proc/mounts'):
            # spaces are recorded as "\\040", handle them unconditionally
            # TODO: test if this actually works with special characters
            mpoint = line.split()[1].replace('\\040', ' ')
            if mpoint.startswith((config.FILESYSTEM_DIR, config.ISO_DIR)):
                message.sub_info('Unmounting', mpoint)
                misc.system_command((misc.whereis('umount'), '-f', '-l', mpoint))
    else:
        message.sub_debug('/proc/mounts does not exists!')

    if os.path.isdir(config.FILESYSTEM_DIR):
        message.sub_info('Removing', config.FILESYSTEM_DIR)
        shutil.rmtree(config.FILESYSTEM_DIR)

    if os.path.isdir(config.ISO_DIR):
        message.sub_info('Removing', config.ISO_DIR)
        shutil.rmtree(config.ISO_DIR)
Example #11
0
def clean_work_dirs():
    # ensure that nothing is mounted to the working directories before cleanup,
    # see https://github.com/clearkimura/Customizer/issues/82
    if os.path.exists('/proc/mounts'):
        for line in misc.readlines_file('/proc/mounts'):
            # spaces are recorded as "\\040", handle them unconditionally
            # TODO: test if this actually works with special characters
            mpoint = line.split()[1].replace('\\040', ' ')
            if mpoint.startswith((config.FILESYSTEM_DIR, config.ISO_DIR)):
                message.sub_info('Unmounting', mpoint)
                misc.system_command(
                    (misc.whereis('umount'), '-f', '-l', mpoint))
    else:
        message.sub_debug('/proc/mounts does not exists!')

    if os.path.isdir(config.FILESYSTEM_DIR):
        message.sub_info('Removing', config.FILESYSTEM_DIR)
        shutil.rmtree(config.FILESYSTEM_DIR)

    if os.path.isdir(config.ISO_DIR):
        message.sub_info('Removing', config.ISO_DIR)
        shutil.rmtree(config.ISO_DIR)
Example #12
0
def main():
    global mount_dir
    if not os.path.isfile(config.ISO):
        raise (message.exception('ISO does not exists', config.ISO))
    elif not config.ISO.endswith('.iso'):
        raise (message.exception('File is not ISO', config.ISO))

    common.clean_work_dirs()
    common.create_work_dirs()

    message.sub_info('Creating mount directory')
    mount_dir = tempfile.mkdtemp(prefix=config.MOUNT_DIR + '/')
    message.sub_debug('Mount directory is', mount_dir)

    message.sub_info('Mounting ISO', config.ISO)
    try:
        # load the required kernel modules in cases the are not, if they are
        # builtin the command fails so ignoring that. the "-b" argument is to
        # ensure blacklisted modules are respected
        try:
            misc.system_command(
                (misc.whereis('modprobe'), '-ba', 'loop', 'iso9660'))
        except:
            pass
        misc.system_command((misc.whereis('mount'), '-t', 'iso9660', '-o', \
            'ro,loop', config.ISO, mount_dir))
    except:
        message.sub_info('Removing', mount_dir)
        os.rmdir(mount_dir)
        common.clean_work_dirs()
        raise

    message.sub_info('Checking ISO')
    for spath in (mount_dir + '/casper/filesystem.squashfs', \
        mount_dir + '/casper/filesystem.manifest', \
        mount_dir + '/casper/filesystem.manifest-remove', \
        mount_dir + '/.disk', mount_dir + '/isolinux', ):
        if not os.path.exists(spath):
            message.sub_debug('Non-existing path', spath)
            common.clean_work_dirs()
            unmount_iso()
            raise (message.exception('Invalid ISO', config.ISO))

    message.sub_info('Unsquashing filesystem')
    try:
        misc.system_command((misc.whereis('unsquashfs'), '-f', '-d', \
            config.FILESYSTEM_DIR, mount_dir + '/casper/filesystem.squashfs'))
    except:
        unmount_iso()
        raise

    message.sub_info('Checking architecture')
    fs_arch = misc.chroot_exec(('dpkg', '--print-architecture'), \
        prepare=False, mount=False, output=True)
    host_arch = os.uname()[4]
    message.sub_debug('Filesystem architecture', fs_arch)
    message.sub_debug('Host architecture', host_arch)
    if fs_arch == 'amd64' and not host_arch == 'x86_64':
        message.sub_debug('The ISO architecture is amd64 and yours is not')
        common.clean_work_dirs()
        unmount_iso()
        raise (message.exception(
            'The ISO architecture is amd64 and yours is not'))

    message.sub_info('Copying ISO files')
    for sfile in misc.list_files(mount_dir):
        if sfile.endswith('casper/filesystem.squashfs'):
            continue
        else:
            message.sub_debug('Copying', sfile)
            misc.copy_file(sfile, sfile.replace(mount_dir, config.ISO_DIR))

    unmount_iso()
Example #13
0
def main():
    global mount_dir
    if not os.path.isfile(config.ISO):
        raise(message.exception('ISO does not exist', config.ISO))
    elif not config.ISO.endswith('.iso'):
        raise(message.exception('File is not ISO', config.ISO))

    common.clean_work_dirs()
    common.create_work_dirs()

    message.sub_info('Creating mount directory')
    mount_dir = tempfile.mkdtemp(prefix=config.MOUNT_DIR + '/')
    message.sub_debug('Mount directory is', mount_dir)

    message.sub_info('Mounting ISO', config.ISO)
    try:
        # load the required kernel modules in cases the are not, if they are
        # builtin the command fails so ignoring that. the "-b" argument is to
        # ensure blacklisted modules are respected
        try:
            misc.system_command((misc.whereis('modprobe'), '-ba', 'loop', 'iso9660'))
        except:
            pass
        misc.system_command((misc.whereis('mount'), '-t', 'iso9660', '-o', \
            'ro,loop', config.ISO, mount_dir))
    except:
        message.sub_info('Removing', mount_dir)
        os.rmdir(mount_dir)
        common.clean_work_dirs()
        raise

    message.sub_info('Checking ISO')
    for spath in (mount_dir + '/casper/filesystem.squashfs', \
        mount_dir + '/casper/filesystem.manifest', \
        mount_dir + '/casper/filesystem.manifest-remove', \
        mount_dir + '/.disk', mount_dir + '/isolinux', ):
        if not os.path.exists(spath):
            message.sub_debug('Non-existing path', spath)
            common.clean_work_dirs()
            unmount_iso()
            raise(message.exception('Invalid ISO', config.ISO))

    message.sub_info('Unsquashing filesystem')
    try:
        misc.system_command((misc.whereis('unsquashfs'), '-f', '-d', \
            config.FILESYSTEM_DIR, mount_dir + '/casper/filesystem.squashfs'))
    except:
        unmount_iso()
        raise

    message.sub_info('Checking architecture')
    fs_arch = misc.chroot_exec(('dpkg', '--print-architecture'), \
        prepare=False, mount=False, output=True)
    host_arch = os.uname()[4]
    message.sub_debug('Filesystem architecture', fs_arch)
    message.sub_debug('Host architecture', host_arch)
    if fs_arch == 'amd64' and not host_arch == 'x86_64':
        message.sub_debug('The ISO architecture is amd64 and yours is not')
        common.clean_work_dirs()
        unmount_iso()
        raise(message.exception('The ISO architecture is amd64 and yours is not'))

    message.sub_info('Copying ISO files')
    for sfile in misc.list_files(mount_dir):
        if sfile.endswith('casper/filesystem.squashfs'):
            continue
        else:
            message.sub_debug('Copying', sfile)
            misc.copy_file(sfile, sfile.replace(mount_dir, config.ISO_DIR))

    unmount_iso()