|
| 1 | +# Copyright 2014 Red Hat, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +Code for creating partitions on a disk. |
| 18 | +
|
| 19 | +Imported from ironic-lib's disk_utils as of the following commit: |
| 20 | +https://opendev.org/openstack/ironic-lib/commit/42fa5d63861ba0f04b9a4f67212173d7013a1332 |
| 21 | +""" |
| 22 | + |
| 23 | +import logging |
| 24 | + |
| 25 | +from ironic_lib.common.i18n import _ |
| 26 | +from ironic_lib import exception |
| 27 | +from ironic_lib import utils |
| 28 | +from oslo_config import cfg |
| 29 | + |
| 30 | +CONF = cfg.CONF |
| 31 | + |
| 32 | +LOG = logging.getLogger(__name__) |
| 33 | + |
| 34 | + |
| 35 | +class DiskPartitioner(object): |
| 36 | + |
| 37 | + def __init__(self, device, disk_label='msdos', alignment='optimal'): |
| 38 | + """A convenient wrapper around the parted tool. |
| 39 | +
|
| 40 | + :param device: The device path. |
| 41 | + :param disk_label: The type of the partition table. Valid types are: |
| 42 | + "bsd", "dvh", "gpt", "loop", "mac", "msdos", |
| 43 | + "pc98", or "sun". |
| 44 | + :param alignment: Set alignment for newly created partitions. |
| 45 | + Valid types are: none, cylinder, minimal and |
| 46 | + optimal. |
| 47 | +
|
| 48 | + """ |
| 49 | + self._device = device |
| 50 | + self._disk_label = disk_label |
| 51 | + self._alignment = alignment |
| 52 | + self._partitions = [] |
| 53 | + |
| 54 | + def _exec(self, *args): |
| 55 | + # NOTE(lucasagomes): utils.execute() is already a wrapper on top |
| 56 | + # of processutils.execute() which raises specific |
| 57 | + # exceptions. It also logs any failure so we don't |
| 58 | + # need to log it again here. |
| 59 | + utils.execute('parted', '-a', self._alignment, '-s', self._device, |
| 60 | + '--', 'unit', 'MiB', *args, use_standard_locale=True) |
| 61 | + |
| 62 | + def add_partition(self, size, part_type='primary', fs_type='', |
| 63 | + boot_flag=None, extra_flags=None): |
| 64 | + """Add a partition. |
| 65 | +
|
| 66 | + :param size: The size of the partition in MiB. |
| 67 | + :param part_type: The type of the partition. Valid values are: |
| 68 | + primary, logical, or extended. |
| 69 | + :param fs_type: The filesystem type. Valid types are: ext2, fat32, |
| 70 | + fat16, HFS, linux-swap, NTFS, reiserfs, ufs. |
| 71 | + If blank (''), it will create a Linux native |
| 72 | + partition (83). |
| 73 | + :param boot_flag: Boot flag that needs to be configured on the |
| 74 | + partition. Ignored if None. It can take values |
| 75 | + 'bios_grub', 'boot'. |
| 76 | + :param extra_flags: List of flags to set on the partition. Ignored |
| 77 | + if None. |
| 78 | + :returns: The partition number. |
| 79 | +
|
| 80 | + """ |
| 81 | + self._partitions.append({'size': size, |
| 82 | + 'type': part_type, |
| 83 | + 'fs_type': fs_type, |
| 84 | + 'boot_flag': boot_flag, |
| 85 | + 'extra_flags': extra_flags}) |
| 86 | + return len(self._partitions) |
| 87 | + |
| 88 | + def get_partitions(self): |
| 89 | + """Get the partitioning layout. |
| 90 | +
|
| 91 | + :returns: An iterator with the partition number and the |
| 92 | + partition layout. |
| 93 | +
|
| 94 | + """ |
| 95 | + return enumerate(self._partitions, 1) |
| 96 | + |
| 97 | + def commit(self): |
| 98 | + """Write to the disk.""" |
| 99 | + LOG.debug("Committing partitions to disk.") |
| 100 | + cmd_args = ['mklabel', self._disk_label] |
| 101 | + # NOTE(lucasagomes): Lead in with 1MiB to allow room for the |
| 102 | + # partition table itself. |
| 103 | + start = 1 |
| 104 | + for num, part in self.get_partitions(): |
| 105 | + end = start + part['size'] |
| 106 | + cmd_args.extend(['mkpart', part['type'], part['fs_type'], |
| 107 | + str(start), str(end)]) |
| 108 | + if part['boot_flag']: |
| 109 | + cmd_args.extend(['set', str(num), part['boot_flag'], 'on']) |
| 110 | + if part['extra_flags']: |
| 111 | + for flag in part['extra_flags']: |
| 112 | + cmd_args.extend(['set', str(num), flag, 'on']) |
| 113 | + start = end |
| 114 | + |
| 115 | + self._exec(*cmd_args) |
| 116 | + |
| 117 | + try: |
| 118 | + from ironic_python_agent import disk_utils # circular dependency |
| 119 | + disk_utils.wait_for_disk_to_become_available(self._device) |
| 120 | + except exception.IronicException as e: |
| 121 | + raise exception.InstanceDeployFailure( |
| 122 | + _('Disk partitioning failed on device %(device)s. ' |
| 123 | + 'Error: %(error)s') |
| 124 | + % {'device': self._device, 'error': e}) |
0 commit comments