+def _PrepareFileBasedStorage(
+ enabled_disk_templates, file_storage_dir,
+ default_dir, file_disk_template,
+ init_fn=_InitFileStorageDir, acceptance_fn=None):
+ """Checks if a file-base storage type is enabled and inits the dir.
+
+ @type enabled_disk_templates: list of string
+ @param enabled_disk_templates: list of enabled disk templates
+ @type file_storage_dir: string
+ @param file_storage_dir: the file storage directory
+ @type default_dir: string
+ @param default_dir: default file storage directory when C{file_storage_dir}
+ is 'None'
+ @type file_disk_template: string
+ @param file_disk_template: a disk template whose storage type is 'ST_FILE'
+ @rtype: string
+ @returns: the name of the actual file storage directory
+
+ """
+ assert (file_disk_template in
+ utils.storage.GetDiskTemplatesOfStorageType(constants.ST_FILE))
+ if file_storage_dir is None:
+ file_storage_dir = default_dir
+ if not acceptance_fn:
+ acceptance_fn = \
+ lambda path: filestorage.CheckFileStoragePathAcceptance(
+ path, exact_match_ok=True)
+
+ cluster.CheckFileStoragePathVsEnabledDiskTemplates(
+ logging.warning, file_storage_dir, enabled_disk_templates)
+
+ file_storage_enabled = file_disk_template in enabled_disk_templates
+ if file_storage_enabled:
+ try:
+ acceptance_fn(file_storage_dir)
+ except errors.FileStoragePathError as e:
+ raise errors.OpPrereqError(str(e))
+ result_file_storage_dir = init_fn(file_storage_dir)
+ else:
+ result_file_storage_dir = file_storage_dir
+ return result_file_storage_dir
+
+
+def _PrepareFileStorage(
+ enabled_disk_templates, file_storage_dir, init_fn=_InitFileStorageDir,
+ acceptance_fn=None):
+ """Checks if file storage is enabled and inits the dir.
+
+ @see: C{_PrepareFileBasedStorage}
+
+ """
+ return _PrepareFileBasedStorage(
+ enabled_disk_templates, file_storage_dir,
+ pathutils.DEFAULT_FILE_STORAGE_DIR, constants.DT_FILE,
+ init_fn=init_fn, acceptance_fn=acceptance_fn)
+
+
+def _PrepareSharedFileStorage(
+ enabled_disk_templates, file_storage_dir, init_fn=_InitFileStorageDir,
+ acceptance_fn=None):
+ """Checks if shared file storage is enabled and inits the dir.
+
+ @see: C{_PrepareFileBasedStorage}
+
+ """
+ return _PrepareFileBasedStorage(
+ enabled_disk_templates, file_storage_dir,
+ pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR, constants.DT_SHARED_FILE,
+ init_fn=init_fn, acceptance_fn=acceptance_fn)
+
+
+def _InitCheckEnabledDiskTemplates(enabled_disk_templates):
+ """Checks the sanity of the enabled disk templates.
+
+ """
+ if not enabled_disk_templates:
+ raise errors.OpPrereqError("Enabled disk templates list must contain at"
+ " least one member", errors.ECODE_INVAL)
+ invalid_disk_templates = \
+ set(enabled_disk_templates) - constants.DISK_TEMPLATES
+ if invalid_disk_templates:
+ raise errors.OpPrereqError("Enabled disk templates list contains invalid"
+ " entries: %s" % invalid_disk_templates,
+ errors.ECODE_INVAL)
+
+
+def _RestrictIpolicyToEnabledDiskTemplates(ipolicy, enabled_disk_templates):
+ """Restricts the ipolicy's disk templates to the enabled ones.
+
+ This function clears the ipolicy's list of allowed disk templates from the
+ ones that are not enabled by the cluster.
+
+ @type ipolicy: dict
+ @param ipolicy: the instance policy
+ @type enabled_disk_templates: list of string
+ @param enabled_disk_templates: the list of cluster-wide enabled disk
+ templates
+
+ """
+ assert constants.IPOLICY_DTS in ipolicy
+ allowed_disk_templates = ipolicy[constants.IPOLICY_DTS]
+ restricted_disk_templates = list(set(allowed_disk_templates)
+ .intersection(set(enabled_disk_templates)))
+ ipolicy[constants.IPOLICY_DTS] = restricted_disk_templates
+
+
+def _InitCheckDrbdHelper(drbd_helper, drbd_enabled):
+ """Checks the DRBD usermode helper.
+
+ @type drbd_helper: string
+ @param drbd_helper: name of the DRBD usermode helper that the system should
+ use
+
+ """
+ if not drbd_enabled:
+ return
+
+ if drbd_helper is not None:
+ try:
+ curr_helper = drbd.DRBD8.GetUsermodeHelper()
+ except errors.BlockDeviceError, err:
+ raise errors.OpPrereqError("Error while checking drbd helper"
+ " (disable drbd with --enabled-disk-templates"
+ " if you are not using drbd): %s" % str(err),
+ errors.ECODE_ENVIRON)
+ if drbd_helper != curr_helper:
+ raise errors.OpPrereqError("Error: requiring %s as drbd helper but %s"
+ " is the current helper" % (drbd_helper,
+ curr_helper),
+ errors.ECODE_INVAL)
+
+