def make_partition(dev, begin, end, ptype): LOG.debug('Trying to create a partition: dev=%s begin=%s end=%s' % (dev, begin, end)) if ptype not in ('primary', 'logical'): raise errors.WrongPartitionSchemeError( 'Wrong partition type: %s' % ptype) # check begin >= end if begin >= end: raise errors.WrongPartitionSchemeError( 'Wrong boundaries: begin >= end') # check if begin and end are inside one of free spaces available if not any(x['fstype'] == 'free' and begin >= x['begin'] and end <= x['end'] for x in info(dev)['parts']): raise errors.WrongPartitionSchemeError( 'Invalid boundaries: begin and end ' 'are not inside available free space') utils.udevadm_settle() out, err = utils.execute( 'parted', '-a', 'optimal', '-s', dev, 'unit', 'MiB', 'mkpart', ptype, str(begin), str(end), check_exit_code=[0, 1]) LOG.debug('Parted output: \n%s' % out) reread_partitions(dev, out=out)
def set_partition_flag(dev, num, flag, state='on'): """Sets flag on a partition :param dev: A device file, e.g. /dev/sda. :param num: Partition number :param flag: Flag name. Must be one of 'bios_grub', 'legacy_boot', 'boot', 'raid', 'lvm' :param state: Desiable flag state. 'on' or 'off'. Default is 'on'. :returns: None """ LOG.debug('Trying to set partition flag: dev=%s num=%s flag=%s state=%s' % (dev, num, flag, state)) # parted supports more flags but we are interested in # setting only this subset of them. # not all of these flags are compatible with one another. if flag not in ('bios_grub', 'legacy_boot', 'boot', 'raid', 'lvm'): raise errors.WrongPartitionSchemeError( 'Unsupported partition flag: %s' % flag) if state not in ('on', 'off'): raise errors.WrongPartitionSchemeError( 'Wrong partition flag state: %s' % state) utils.udevadm_settle() out, err = utils.execute('parted', '-s', dev, 'set', str(num), flag, state, check_exit_code=[0, 1]) LOG.debug('Parted output: \n%s' % out) reread_partitions(dev, out=out)
def boot_device(self, grub_version=2): # We assume /boot is a separate partition. If it is not # then we try to use root file system boot_fs = self.fs_by_mount('/boot') or self.fs_by_mount('/') if not boot_fs: raise errors.WrongPartitionSchemeError( 'Error while trying to find boot device: ' 'boot file system not fount, ' 'it must be a separate mount point') if grub_version == 1: # Legacy GRUB has a limitation. It is not able to mount MD devices. # If it is MD compatible it is only able to ignore MD metadata # and to mount one of those devices which are parts of MD device, # but it is possible only if MD device is a MIRROR. md = self.md_by_name(boot_fs.device) if md: try: return md.devices[0] except IndexError: raise errors.WrongPartitionSchemeError( 'Error while trying to find boot device: ' 'md device %s does not have devices attached' % md.name) # Legacy GRUB is not able to mount LVM devices. if self.lv_by_device_name(boot_fs.device): raise errors.WrongPartitionSchemeError( 'Error while trying to find boot device: ' 'found device is %s but legacy grub is not able to ' 'mount logical volumes' % boot_fs.device) return boot_fs.device
def _process_pv(self, volume, disk, parted, partition_schema): partition = self._add_partition(volume, disk, parted) LOG.debug('Creating pv on partition: pv=%s vg=%s' % (partition.name, volume['vg'])) lvm_meta_size = volume.get('lvm_meta_size', DEFAULT_LVM_META_SIZE) # The reason for that is to make sure that # there will be enough space for creating logical volumes. # Default lvm extension size is 4M. Nailgun volume # manager does not care of it and if physical volume size # is 4M * N + 3M and lvm metadata size is 4M * L then only # 4M * (N-L) + 3M of space will be available for # creating logical extensions. So only 4M * (N-L) of space # will be available for logical volumes, while nailgun # volume manager might reguire 4M * (N-L) + 3M # logical volume. Besides, parted aligns partitions # according to its own algorithm and actual partition might # be a bit smaller than integer number of mebibytes. if lvm_meta_size < 10: raise errors.WrongPartitionSchemeError( 'Error while creating physical volume: ' 'lvm metadata size is too small') metadatasize = int(math.floor((lvm_meta_size - 8) / 2)) metadatacopies = 2 partition_schema.vg_attach_by_name(pvname=partition.name, vgname=volume['vg'], metadatasize=metadatasize, metadatacopies=metadatacopies)
def root_device(self): fs = self.fs_by_mount('/') if not fs: raise errors.WrongPartitionSchemeError( 'Error while trying to find root device: ' 'root file system not found') return fs.device
def _mount2uuid(self, os_id, check_root=True): mount2uuid = {} for fs in self.driver.partition_scheme.fs_by_os_id(os_id): mount2uuid[fs.mount] = pu.get_uuid(fs.device) if check_root and '/' not in mount2uuid: raise errors.WrongPartitionSchemeError( 'Error: device with / mountpoint has not been found') return mount2uuid
def validate(data, schema_file='nailgun'): """Validates a given partition scheme using jsonschema. :param scheme: partition scheme to validate """ base_path = os.path.dirname(__file__) schemas_path = os.path.join(base_path, 'json_schemes') with open(os.path.join(schemas_path, '%s.json' % schema_file)) as file: schema = json.load(file) try: checker = jsonschema.FormatChecker() jsonschema.validate(data, schema, format_checker=checker) except Exception as exc: LOG.exception(exc) raise errors.WrongPartitionSchemeError(str(exc)) # scheme is not valid if the number of disks is 0 if not [d for d in data if d['type'] == 'disk']: raise errors.WrongPartitionSchemeError( 'Partition scheme seems empty')
def _mount_bootloader(self, mount_dir): fs = filter(lambda fss: fss.mount == 'multiboot', self.driver.partition_scheme.fss) if len(fs) > 1: raise errors.WrongPartitionSchemeError( 'Multiple multiboot partitions found') utils.makedirs_if_not_exists(mount_dir) fu.mount_fs(fs[0].type, str(fs[0].device), mount_dir) yield pu.get_uuid(fs[0].device) fu.umount_fs(mount_dir)
def _add_configdrive_image(self): configdrive_device = self.partition_scheme.configdrive_device() if configdrive_device is None: raise errors.WrongPartitionSchemeError( 'Error while trying to get configdrive device: ' 'configdrive device not found') size = os.path.getsize(CONF.config_drive_path) md5 = utils.calculate_md5(CONF.config_drive_path, size) self.image_scheme.add_image( uri='file://%s' % CONF.config_drive_path, target_device=configdrive_device, format='iso9660', container='raw', size=size, md5=md5, )
def parse_partition_scheme(self): LOG.debug('--- Preparing partition scheme ---') data = self.partition_data() ks_spaces_validator.validate(data) partition_scheme = objects.PartitionScheme() ceph_osds = self._num_ceph_osds() journals_left = ceph_osds ceph_journals = self._num_ceph_journals() LOG.debug('Looping over all disks in provision data') for disk in self.ks_disks: # skipping disk if there are no volumes with size >0 # to be allocated on it which are not boot partitions if all((v["size"] <= 0 for v in disk["volumes"] if v["type"] != "boot" and v.get("mount") != "/boot")): continue LOG.debug('Processing disk %s' % disk['name']) LOG.debug('Adding gpt table on disk %s' % disk['name']) parted = partition_scheme.add_parted(name=self._disk_dev(disk), label='gpt') if disk in self.boot_disks: # we install bootloader only on every suitable disk LOG.debug('Adding bootloader stage0 on disk %s' % disk['name']) parted.install_bootloader = True # legacy boot partition LOG.debug('Adding bios_grub partition on disk %s: size=24' % disk['name']) parted.add_partition(size=24, flags=['bios_grub']) # uefi partition (for future use) LOG.debug('Adding UEFI partition on disk %s: size=200' % disk['name']) parted.add_partition(size=200) LOG.debug('Looping over all volumes on disk %s' % disk['name']) for volume in disk['volumes']: LOG.debug('Processing volume: ' 'name=%s type=%s size=%s mount=%s vg=%s' % (volume.get('name'), volume.get('type'), volume.get('size'), volume.get('mount'), volume.get('vg'))) if volume['size'] <= 0: LOG.debug('Volume size is zero. Skipping.') continue if volume.get('name') == 'cephjournal': LOG.debug('Volume seems to be a CEPH journal volume. ' 'Special procedure is supposed to be applied.') # We need to allocate a journal partition for each ceph OSD # Determine the number of journal partitions we need on # each device ratio = int(math.ceil(float(ceph_osds) / ceph_journals)) # No more than 10GB will be allocated to a single journal # partition size = volume["size"] / ratio if size > 10240: size = 10240 # This will attempt to evenly spread partitions across # multiple devices e.g. 5 osds with 2 journal devices will # create 3 partitions on the first device and 2 on the # second if ratio < journals_left: end = ratio else: end = journals_left for i in range(0, end): journals_left -= 1 if volume['type'] == 'partition': LOG.debug('Adding CEPH journal partition on ' 'disk %s: size=%s' % (disk['name'], size)) prt = parted.add_partition(size=size) LOG.debug('Partition name: %s' % prt.name) if 'partition_guid' in volume: LOG.debug('Setting partition GUID: %s' % volume['partition_guid']) prt.set_guid(volume['partition_guid']) continue if volume['type'] in ('partition', 'pv', 'raid'): if volume.get('mount') != '/boot': LOG.debug('Adding partition on disk %s: size=%s' % (disk['name'], volume['size'])) prt = parted.add_partition(size=volume['size'], keep_data=volume.get( 'keep_data', False)) LOG.debug('Partition name: %s' % prt.name) elif volume.get('mount') == '/boot' \ and not self._boot_partition_done \ and disk in self.boot_disks: LOG.debug( 'Adding /boot partition on disk %s: ' 'size=%s', disk['name'], volume['size']) prt = parted.add_partition(size=volume['size'], keep_data=volume.get( 'keep_data', False)) LOG.debug('Partition name: %s', prt.name) self._boot_partition_done = True else: LOG.debug( 'No need to create partition on disk %s. ' 'Skipping.', disk['name']) continue if volume['type'] == 'partition': if 'partition_guid' in volume: LOG.debug('Setting partition GUID: %s' % volume['partition_guid']) prt.set_guid(volume['partition_guid']) if 'mount' in volume and volume['mount'] != 'none': LOG.debug('Adding file system on partition: ' 'mount=%s type=%s' % (volume['mount'], volume.get('file_system', 'xfs'))) partition_scheme.add_fs( device=prt.name, mount=volume['mount'], fs_type=volume.get('file_system', 'xfs'), fs_label=volume.get('disk_label')) if volume['mount'] == '/boot' and not self._boot_done: self._boot_done = True if volume['type'] == 'pv': LOG.debug('Creating pv on partition: pv=%s vg=%s' % (prt.name, volume['vg'])) lvm_meta_size = volume.get('lvm_meta_size', 64) # The reason for that is to make sure that # there will be enough space for creating logical volumes. # Default lvm extension size is 4M. Nailgun volume # manager does not care of it and if physical volume size # is 4M * N + 3M and lvm metadata size is 4M * L then only # 4M * (N-L) + 3M of space will be available for # creating logical extensions. So only 4M * (N-L) of space # will be available for logical volumes, while nailgun # volume manager might reguire 4M * (N-L) + 3M # logical volume. Besides, parted aligns partitions # according to its own algorithm and actual partition might # be a bit smaller than integer number of mebibytes. if lvm_meta_size < 10: raise errors.WrongPartitionSchemeError( 'Error while creating physical volume: ' 'lvm metadata size is too small') metadatasize = int(math.floor((lvm_meta_size - 8) / 2)) metadatacopies = 2 partition_scheme.vg_attach_by_name( pvname=prt.name, vgname=volume['vg'], metadatasize=metadatasize, metadatacopies=metadatacopies) if volume['type'] == 'raid': if 'mount' in volume and \ volume['mount'] not in ('none', '/boot'): LOG.debug('Attaching partition to RAID ' 'by its mount point %s' % volume['mount']) metadata = 'default' if self.have_grub1_by_default: metadata = '0.90' LOG.debug( 'Going to use MD metadata version {0}. ' 'The version was guessed at the data has ' 'been given about the operating system.'.format( metadata)) partition_scheme.md_attach_by_mount( device=prt.name, mount=volume['mount'], fs_type=volume.get('file_system', 'xfs'), fs_label=volume.get('disk_label'), metadata=metadata) if 'mount' in volume and volume['mount'] == '/boot' and \ not self._boot_done: LOG.debug('Adding file system on partition: ' 'mount=%s type=%s' % (volume['mount'], volume.get('file_system', 'ext2'))) partition_scheme.add_fs( device=prt.name, mount=volume['mount'], fs_type=volume.get('file_system', 'ext2'), fs_label=volume.get('disk_label')) self._boot_done = True # this partition will be used to put there configdrive image if (partition_scheme.configdrive_device() is None and CONF.prepare_configdrive or os.path.isfile(CONF.config_drive_path)): LOG.debug('Adding configdrive partition on disk %s: size=20' % disk['name']) parted.add_partition(size=20, configdrive=True) # checking if /boot is expected to be created if self._have_boot_partition(self.ks_disks) and \ (not self._boot_partition_done or not self._boot_done): raise errors.WrongPartitionSchemeError( '/boot partition has not been created for some reasons') LOG.debug('Looping over all volume groups in provision data') for vg in self.ks_vgs: LOG.debug('Processing vg %s' % vg['id']) LOG.debug('Looping over all logical volumes in vg %s' % vg['id']) for volume in vg['volumes']: LOG.debug('Processing lv %s' % volume['name']) if volume['size'] <= 0: LOG.debug('LogicalVolume size is zero. Skipping.') continue if volume['type'] == 'lv': LOG.debug('Adding lv to vg %s: name=%s, size=%s' % (vg['id'], volume['name'], volume['size'])) lv = partition_scheme.add_lv(name=volume['name'], vgname=vg['id'], size=volume['size']) if 'mount' in volume and volume['mount'] != 'none': LOG.debug('Adding file system on lv: ' 'mount=%s type=%s' % (volume['mount'], volume.get('file_system', 'xfs'))) partition_scheme.add_fs( device=lv.device_name, mount=volume['mount'], fs_type=volume.get('file_system', 'xfs'), fs_label=volume.get('disk_label')) partition_scheme.elevate_keep_data() return partition_scheme
def do_bootloader(self): LOG.debug('--- Installing bootloader (do_bootloader) ---') chroot = '/tmp/target' partition_scheme = self.driver.partition_scheme with self.mount_target(chroot): mount2uuid = {} for fs in partition_scheme.fss: mount2uuid[fs.mount] = utils.execute( 'blkid', '-o', 'value', '-s', 'UUID', fs.device, check_exit_code=[0])[0].strip() if '/' not in mount2uuid: raise errors.WrongPartitionSchemeError( 'Error: device with / mountpoint has not been found') grub = self.driver.grub guessed_version = gu.guess_grub_version(chroot=chroot) if guessed_version != grub.version: grub.version = guessed_version LOG.warning('Grub version differs from which the operating ' 'system should have by default. Found version in ' 'image: %s', guessed_version) boot_device = partition_scheme.boot_device(grub.version) install_devices = [d.name for d in partition_scheme.parteds if d.install_bootloader] grub.append_kernel_params('root=UUID=%s ' % mount2uuid['/']) kernel = grub.kernel_name or \ gu.guess_kernel(chroot=chroot, regexp=grub.kernel_regexp) initrd = grub.initrd_name or \ gu.guess_initrd(chroot=chroot, regexp=grub.initrd_regexp) if grub.version == 1: gu.grub1_cfg(kernel=kernel, initrd=initrd, kernel_params=grub.kernel_params, chroot=chroot, grub_timeout=CONF.timeout) gu.grub1_install(install_devices, boot_device, chroot=chroot) else: # TODO(kozhukalov): implement which kernel to use by default # Currently only grub1_cfg accepts kernel and initrd # parameters. gu.grub2_cfg(kernel_params=grub.kernel_params, chroot=chroot, grub_timeout=CONF.timeout) gu.grub2_install(install_devices, chroot=chroot) # TODO(agordeev): move to separate actions? if CONF.fix_udev_net_rules: # FIXME(agordeev) There's no convenient way to perfrom NIC # remapping in Ubuntu, so injecting files prior the first boot # should work with open(chroot + '/etc/udev/rules.d/70-persistent-net.rules', 'wt', encoding='utf-8') as f: f.write(u'# Generated by bareon during provisioning: ' u'BEGIN\n') # pattern is aa:bb:cc:dd:ee:ff_eth0,aa:bb:cc:dd:ee:ff_eth1 for mapping in self.driver.configdrive_scheme. \ common.udevrules.split(','): mac_addr, nic_name = mapping.split('_') f.write(u'SUBSYSTEM=="net", ACTION=="add", ' u'DRIVERS=="?*", ATTR{address}=="%s", ' u'ATTR{type}=="1", KERNEL=="eth*", ' u'NAME="%s"\n' % (mac_addr, nic_name)) f.write( u'# Generated by bareon during provisioning: END\n') # FIXME(agordeev): Disable net-generator that adds new entries # to 70-persistent-net.rules with open(chroot + '/etc/udev/rules.d/' '75-persistent-net-generator.rules', 'wt', encoding='utf-8') as f: f.write(u'# Generated by bareon during provisioning:\n' u'# DO NOT DELETE. It is needed to disable ' u'net-generator\n') # FIXME(kozhukalov): Prevent nailgun-agent from doing anything. # This ugly hack is to be used together with the command removing # this lock file not earlier than /etc/rc.local # The reason for this hack to appear is to prevent nailgun-agent # from changing mcollective config at the same time when cloud-init # does the same. Otherwise, we can end up with corrupted # mcollective config. # For details see https://bugs.launchpad.net/fuel/+bug/1449186 LOG.debug('Preventing nailgun-agent from doing ' 'anything until it is unlocked') utils.makedirs_if_not_exists(os.path.join(chroot, 'etc/nailgun-agent')) with open(os.path.join(chroot, 'etc/nailgun-agent/nodiscover'), 'w'): pass # FIXME(kozhukalov): When we have just os-root fs image and don't # have os-var-log fs image while / and /var/log are supposed to be # separate file systems and os-var-log is mounted into # non-empty directory on the / file system, those files in /var/log # directory become unavailable. # The thing is that among those file there is /var/log/upstart # where upstart daemon writes its logs. We have specific upstart # job which is to flush open files once all file systems are # mounted. # This job needs upstart directory to be available on os-var-log # file system. # This is just a temporary fix and correct fix will be available # soon via updates. utils.execute('mkdir', '-p', chroot + '/var/log/upstart') with open(chroot + '/etc/fstab', 'wt', encoding='utf-8') as f: for fs in self.driver.partition_scheme.fss: # TODO(kozhukalov): Think of improving the logic so as to # insert a meaningful fsck order value which is last zero # at fstab line. Currently we set it into 0 which means # a corresponding file system will never be checked. We # assume puppet or other configuration tool will care of # it. if fs.mount == '/': f.write(u'UUID=%s %s %s defaults,errors=panic 0 0\n' % (mount2uuid[fs.mount], fs.mount, fs.type)) else: f.write(u'UUID=%s %s %s defaults 0 0\n' % (mount2uuid[fs.mount], fs.mount, fs.type))