def add_fstab(device, mountpoint, filesystem): """ Add entry to /etc/fstab for mountpoint :param device: Device to add :type device: str :param mountpoint: Mountpoint on which device is mounted :type mountpoint: str :param filesystem: Filesystem used :type filesystem: str :return: None """ new_content = [] with open('/etc/fstab', 'r') as fstab_file: lines = [line.strip() for line in fstab_file.readlines()] found = False for line in lines: if line.startswith(device) and re.match( '^{0}\s+'.format(re.escape(device)), line): new_content.append( OSManager.get_fstab_entry(device, mountpoint, filesystem)) found = True else: new_content.append(line) if found is False: new_content.append( OSManager.get_fstab_entry(device, mountpoint, filesystem)) with open('/etc/fstab', 'w') as fstab_file: fstab_file.write('{0}\n'.format('\n'.join(new_content)))
def add_fstab(device, mountpoint, filesystem): """ Add entry to /etc/fstab for mountpoint :param device: Device to add :type device: str :param mountpoint: Mountpoint on which device is mounted :type mountpoint: str :param filesystem: Filesystem used :type filesystem: str :return: None """ new_content = [] with open('/etc/fstab', 'r') as fstab_file: lines = [line.strip() for line in fstab_file.readlines()] found = False for line in lines: if line.startswith(device) and re.match('^{0}\s+'.format(re.escape(device)), line): new_content.append(OSManager.get_fstab_entry(device, mountpoint, filesystem)) found = True else: new_content.append(line) if found is False: new_content.append(OSManager.get_fstab_entry(device, mountpoint, filesystem)) with open('/etc/fstab', 'w') as fstab_file: fstab_file.write('{0}\n'.format('\n'.join(new_content)))
def add_fstab(partition_aliases, mountpoint, filesystem): """ Add entry to /etc/fstab for mountpoint :param partition_aliases: Possible aliases of the partition to add :type partition_aliases: list :param mountpoint: Mountpoint on which device is mounted :type mountpoint: str :param filesystem: Filesystem used :type filesystem: str :return: None """ if len(partition_aliases) == 0: raise ValueError('No partition aliases provided') with open('/etc/fstab', 'r') as fstab_file: lines = [line.strip() for line in fstab_file.readlines()] used_path = None used_index = None mount_line = None for device_alias in partition_aliases: for index, line in enumerate(lines): if line.startswith('#'): continue if line.startswith(device_alias) and re.match( '^{0}\s+'.format(re.escape(device_alias)), line): used_path = device_alias used_index = index if len(line.split()) == 6 and line.split( )[1] == mountpoint: # Example line: 'UUID=40d99523-a1e7-4374-84f2-85b5d14b516e / swap sw 0 0' mount_line = line if used_path is not None: break if used_path is None: # Partition not yet present with any of its possible aliases lines.append( OSManager.get_fstab_entry(partition_aliases[0], mountpoint, filesystem)) else: # Partition present, update information lines.pop(used_index) lines.insert( used_index, OSManager.get_fstab_entry(used_path, mountpoint, filesystem)) if mount_line is not None: # Mountpoint already in use by another device (potentially same device, but other device_path) lines.remove(mount_line) with file_mutex('ovs-fstab-lock'): with open('/etc/fstab', 'w') as fstab_file: fstab_file.write('{0}\n'.format('\n'.join(lines)))
def add_fstab(device, mountpoint, filesystem): new_content = [] with open('/etc/fstab', 'r') as fstab_file: lines = [line.strip() for line in fstab_file.readlines()] found = False for line in lines: if line.startswith(device) and re.match('^{0}\s+'.format(re.escape(device)), line): new_content.append(OSManager.get_fstab_entry(device, mountpoint, filesystem)) found = True else: new_content.append(line) if found is False: new_content.append(OSManager.get_fstab_entry(device, mountpoint, filesystem)) with open('/etc/fstab', 'w') as fstab_file: fstab_file.write('{0}\n'.format('\n'.join(new_content)))
def add_fstab(partition_aliases, mountpoint, filesystem): """ Add entry to /etc/fstab for mountpoint :param partition_aliases: Possible aliases of the partition to add :type partition_aliases: list :param mountpoint: Mountpoint on which device is mounted :type mountpoint: str :param filesystem: Filesystem used :type filesystem: str :return: None """ if len(partition_aliases) == 0: raise ValueError('No partition aliases provided') with open('/etc/fstab', 'r') as fstab_file: lines = [line.strip() for line in fstab_file.readlines()] used_path = None used_index = None mount_line = None for device_alias in partition_aliases: for index, line in enumerate(lines): if line.startswith('#'): continue if line.startswith(device_alias) and re.match('^{0}\s+'.format(re.escape(device_alias)), line): used_path = device_alias used_index = index if len(line.split()) == 6 and line.split()[1] == mountpoint: # Example line: 'UUID=40d99523-a1e7-4374-84f2-85b5d14b516e / swap sw 0 0' mount_line = line if used_path is not None: break if used_path is None: # Partition not yet present with any of its possible aliases lines.append(OSManager.get_fstab_entry(partition_aliases[0], mountpoint, filesystem)) else: # Partition present, update information lines.pop(used_index) lines.insert(used_index, OSManager.get_fstab_entry(used_path, mountpoint, filesystem)) if mount_line is not None: # Mountpoint already in use by another device (potentially same device, but other device_path) lines.remove(mount_line) with file_mutex('ovs-fstab-lock'): with open('/etc/fstab', 'w') as fstab_file: fstab_file.write('{0}\n'.format('\n'.join(lines)))