2017-01-29 23:52:40 +00:00
|
|
|
# Copyright 2017 Andreas Florath (andreas@florath.net)
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2017-06-01 05:53:23 +00:00
|
|
|
import functools
|
2017-01-29 23:52:40 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2023-02-01 17:05:51 +00:00
|
|
|
import time
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-05-18 22:42:00 +00:00
|
|
|
from diskimage_builder.block_device.exception \
|
2017-01-29 23:52:40 +00:00
|
|
|
import BlockDeviceSetupException
|
2017-05-25 01:28:13 +00:00
|
|
|
from diskimage_builder.block_device.plugin import NodeBase
|
|
|
|
from diskimage_builder.block_device.plugin import PluginBase
|
2017-01-29 23:52:40 +00:00
|
|
|
from diskimage_builder.block_device.utils import exec_sudo
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-05-25 01:28:13 +00:00
|
|
|
class MountPointNode(NodeBase):
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
def __init__(self, mount_base, config, state):
|
|
|
|
super(MountPointNode, self).__init__(config['name'], state)
|
2017-05-25 01:28:13 +00:00
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
# Parameter check
|
|
|
|
self.mount_base = mount_base
|
2017-05-25 01:28:13 +00:00
|
|
|
for pname in ['base', 'mount_point']:
|
2017-01-29 23:52:40 +00:00
|
|
|
if pname not in config:
|
2017-05-09 23:59:26 +00:00
|
|
|
raise BlockDeviceSetupException(
|
|
|
|
"MountPoint config needs [%s]" % pname)
|
2017-01-29 23:52:40 +00:00
|
|
|
setattr(self, pname, config[pname])
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("MountPoint created [%s]", self)
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-05-23 23:57:32 +00:00
|
|
|
def get_edges(self):
|
2017-01-29 23:52:40 +00:00
|
|
|
"""Insert all edges
|
|
|
|
|
|
|
|
The dependency edge is created in all cases from the base
|
|
|
|
element (typically a mkfs) and, if this is not the 'first'
|
2017-05-31 00:41:02 +00:00
|
|
|
mount-point, an edge is created from the mount-point before in
|
|
|
|
"sorted order" (see :func:`sort_mount_points`). This ensures
|
|
|
|
that during mounting (and umounting) the globally correct
|
2017-01-29 23:52:40 +00:00
|
|
|
order is used.
|
|
|
|
"""
|
2017-05-23 23:57:32 +00:00
|
|
|
edge_from = []
|
|
|
|
edge_to = []
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-06-01 05:53:23 +00:00
|
|
|
# should have been added by __init__...
|
|
|
|
assert 'sorted_mount_points' in self.state
|
|
|
|
sorted_mount_points = self.state['sorted_mount_points']
|
|
|
|
|
2017-05-31 00:41:02 +00:00
|
|
|
# If we are not first, add our parent in the global dependency
|
2017-06-01 05:53:23 +00:00
|
|
|
# list. sorted_mount_points is tuples (mount_point, node_name).
|
|
|
|
# find ourselves in the mount_points, and our parent node
|
|
|
|
# is one before us in node_name list.
|
|
|
|
mount_points = [x[0] for x in sorted_mount_points]
|
|
|
|
node_name = [x[1] for x in sorted_mount_points]
|
|
|
|
mpi = mount_points.index(self.mount_point)
|
2017-01-29 23:52:40 +00:00
|
|
|
if mpi > 0:
|
2017-06-01 05:53:23 +00:00
|
|
|
dep = node_name[mpi - 1]
|
|
|
|
edge_from.append(dep)
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-05-23 23:57:32 +00:00
|
|
|
edge_from.append(self.base)
|
|
|
|
return (edge_from, edge_to)
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-06-02 00:57:06 +00:00
|
|
|
def create(self):
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.debug("mount called [%s]", self.mount_point)
|
2017-01-29 23:52:40 +00:00
|
|
|
rel_mp = self.mount_point if self.mount_point[0] != '/' \
|
|
|
|
else self.mount_point[1:]
|
|
|
|
mount_point = os.path.join(self.mount_base, rel_mp)
|
|
|
|
if not os.path.exists(mount_point):
|
|
|
|
# Need to sudo this because of permissions in the new
|
|
|
|
# file system tree.
|
|
|
|
exec_sudo(['mkdir', '-p', mount_point])
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.info("Mounting [%s] to [%s]", self.name, mount_point)
|
2017-06-01 04:31:49 +00:00
|
|
|
exec_sudo(["mount", self.state['filesys'][self.base]['device'],
|
2017-01-29 23:52:40 +00:00
|
|
|
mount_point])
|
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
if 'mount' not in self.state:
|
|
|
|
self.state['mount'] = {}
|
|
|
|
self.state['mount'][self.mount_point] \
|
2017-01-29 23:52:40 +00:00
|
|
|
= {'name': self.name, 'base': self.base, 'path': mount_point}
|
|
|
|
|
2017-06-01 04:31:49 +00:00
|
|
|
if 'mount_order' not in self.state:
|
|
|
|
self.state['mount_order'] = []
|
|
|
|
self.state['mount_order'].append(self.mount_point)
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-06-01 04:57:34 +00:00
|
|
|
def umount(self):
|
2017-05-28 05:19:05 +00:00
|
|
|
logger.info("Called for [%s]", self.name)
|
2018-01-17 21:31:31 +00:00
|
|
|
# Before calling umount, call 'fstrim' on suitable mounted
|
|
|
|
# file systems. This discards unused blocks from the mounted
|
2017-09-16 09:43:16 +00:00
|
|
|
# file system and therefore decreases the resulting image
|
|
|
|
# size.
|
2018-01-17 21:31:31 +00:00
|
|
|
#
|
|
|
|
# A race condition can occur when trying to fstrim immediately
|
|
|
|
# after deleting a file resulting in that free space not being
|
|
|
|
# reclaimed. Calling sync before fstrim is a workaround for
|
|
|
|
# this behaviour.
|
2017-09-16 09:43:16 +00:00
|
|
|
# https://lists.gnu.org/archive/html/qemu-devel/2014-03/msg02978.html
|
|
|
|
exec_sudo(["sync"])
|
2018-01-17 21:31:31 +00:00
|
|
|
if self.state['filesys'][self.base]['fstype'] != 'vfat':
|
|
|
|
exec_sudo(["fstrim", "--verbose",
|
|
|
|
self.state['mount'][self.mount_point]['path']])
|
2023-02-01 17:05:51 +00:00
|
|
|
# Even 'fstrim' call sometimes don't solve the issue with 'busy'
|
|
|
|
# filesystem, so we need to catch the exception and repeat unount.
|
|
|
|
mount_point = self.state['mount'][self.mount_point]['path']
|
|
|
|
catch = None
|
|
|
|
for try_cnt in range(10, 1, -1):
|
|
|
|
try:
|
|
|
|
exec_sudo(["umount", mount_point])
|
|
|
|
return
|
|
|
|
except BlockDeviceSetupException as e:
|
|
|
|
catch = e
|
|
|
|
logger.error("umount failed (%s)", e.output.strip())
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
logger.debug("Gave up trying to umount [%s]", mount_point)
|
|
|
|
raise catch
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-06-01 04:57:34 +00:00
|
|
|
def delete(self):
|
|
|
|
self.umount()
|
2017-01-29 23:52:40 +00:00
|
|
|
|
|
|
|
|
2017-06-01 05:53:23 +00:00
|
|
|
def cmp_mount_order(this, other):
|
|
|
|
"""Sort comparision function for mount-point sorting
|
|
|
|
|
|
|
|
See if ``this`` comes before ``other`` in mount-order list. In
|
|
|
|
words: if the other mount-point has us as it's parent, we come
|
|
|
|
before it (are less than it). e.g. ``/var < /var/log <
|
|
|
|
/var/log/foo``
|
|
|
|
|
|
|
|
:param this: tuple of mount_point, node name
|
|
|
|
:param other: tuple of mount_point, node name
|
|
|
|
:returns int: cmp value
|
|
|
|
|
|
|
|
"""
|
|
|
|
# sort is only based on the mount_point.
|
|
|
|
this, _ = this
|
|
|
|
other, _ = other
|
|
|
|
if this == other:
|
|
|
|
return 0
|
|
|
|
if other.startswith(this):
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2017-05-25 01:28:13 +00:00
|
|
|
class Mount(PluginBase):
|
2017-06-01 04:31:49 +00:00
|
|
|
def __init__(self, config, defaults, state):
|
2017-05-25 01:28:13 +00:00
|
|
|
super(Mount, self).__init__()
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-05-25 01:28:13 +00:00
|
|
|
if 'mount-base' not in defaults:
|
2017-05-09 23:59:26 +00:00
|
|
|
raise BlockDeviceSetupException(
|
|
|
|
"Mount default config needs 'mount-base'")
|
2017-06-01 04:31:49 +00:00
|
|
|
self.node = MountPointNode(defaults['mount-base'], config, state)
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-05-31 00:41:02 +00:00
|
|
|
# save this new node to the global mount-point list and
|
2017-06-01 05:53:23 +00:00
|
|
|
# re-order it to keep it in mount-order. Used in get_edges()
|
|
|
|
# to ensure we build the mount graph in order
|
|
|
|
#
|
|
|
|
# note we can't just put the MountPointNode into the state,
|
|
|
|
# because it's not json serialisable and we still dump the
|
|
|
|
# state to json. that's why we have this (mount_point, name)
|
|
|
|
# tuples and sorting trickery
|
|
|
|
sorted_mount_points = state.get('sorted_mount_points', [])
|
|
|
|
mount_points = [mp for mp, name in sorted_mount_points]
|
2017-05-31 00:41:02 +00:00
|
|
|
if self.node.mount_point in mount_points:
|
|
|
|
raise BlockDeviceSetupException(
|
|
|
|
"Mount point [%s] specified more than once"
|
|
|
|
% self.node.mount_point)
|
2017-06-01 05:53:23 +00:00
|
|
|
sorted_mount_points.append((self.node.mount_point, self.node.name))
|
2017-09-18 01:50:54 +00:00
|
|
|
sorted_mount_points.sort(key=functools.cmp_to_key(cmp_mount_order))
|
|
|
|
# Save the state if it's new (otherwise this is idempotent update)
|
2017-06-01 05:53:23 +00:00
|
|
|
state['sorted_mount_points'] = sorted_mount_points
|
2017-05-31 00:41:02 +00:00
|
|
|
logger.debug("Ordered mounts now: %s", sorted_mount_points)
|
2017-01-29 23:52:40 +00:00
|
|
|
|
2017-05-23 23:57:32 +00:00
|
|
|
def get_nodes(self):
|
2017-05-31 00:41:02 +00:00
|
|
|
return [self.node]
|