
Match vFAT label using a given label instead of hardcoding 'config-2'. The NoCloud and ConfigDrive metadata services both attempt to find vFAT formatted drives but the label used was hardcoded to 'config-2' which only works with ConfigDrive. BaseConfigDriveService which both inherit from already has a drive_label property set appropriately for both so let's use that for the vFAT finding logic as well. Change-Id: I8004a8565338b0615450bb28cecc86901be94766
71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
# Copyright 2015 Cloudbase Solutions Srl
|
|
#
|
|
# 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.
|
|
|
|
import os
|
|
import re
|
|
|
|
from oslo_log import log as oslo_logging
|
|
|
|
from cloudbaseinit import conf as cloudbaseinit_conf
|
|
from cloudbaseinit import exception
|
|
|
|
|
|
CONF = cloudbaseinit_conf.CONF
|
|
LOG = oslo_logging.getLogger(__name__)
|
|
VOLUME_LABEL_REGEX = re.compile("Volume label is (.*?)$")
|
|
|
|
|
|
def _check_mtools_path():
|
|
if not CONF.mtools_path:
|
|
raise exception.CloudbaseInitException(
|
|
'"mtools_path" needs to be provided in order '
|
|
'to access VFAT drives')
|
|
|
|
|
|
def is_vfat_drive(osutils, drive_path, drive_label):
|
|
"""Check if the given drive contains a VFAT filesystem."""
|
|
_check_mtools_path()
|
|
mlabel = os.path.join(CONF.mtools_path, "mlabel.exe")
|
|
args = [mlabel, "-i", drive_path, "-s"]
|
|
|
|
out, err, exit_code = osutils.execute_process(args, shell=False)
|
|
if exit_code:
|
|
LOG.debug("Could not retrieve label for VFAT drive path %r",
|
|
drive_path)
|
|
LOG.debug("mlabel failed with error %r", err)
|
|
return False
|
|
|
|
LOG.debug("Obtained label information for drive %r: %r", drive_path, out)
|
|
out = out.decode().strip()
|
|
match = VOLUME_LABEL_REGEX.search(out)
|
|
drive_labels = [drive_label.lower(), drive_label.upper()]
|
|
return match.group(1) in drive_labels if match else False
|
|
|
|
|
|
def copy_from_vfat_drive(osutils, drive_path, target_path):
|
|
"""Copy everything from the given VFAT drive into the given target."""
|
|
_check_mtools_path()
|
|
cwd = os.getcwd()
|
|
try:
|
|
os.chdir(target_path)
|
|
|
|
# A mcopy call looks like this:
|
|
#
|
|
# mcopy -n -i \\.\PHYSICALDRIVEx ::/file/path destination/path
|
|
mcopy = os.path.join(CONF.mtools_path, "mcopy.exe")
|
|
args = [mcopy, "-s", "-n", "-i", drive_path, "::/", "."]
|
|
osutils.execute_process(args, shell=False)
|
|
finally:
|
|
os.chdir(cwd)
|