# encoding: UTF-8 # # Copyright 2011, Dell # # 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. # # Author: andi abes # require 'chef/mixin/shell_out' include Chef::Mixin::ShellOut # rubocop:disable CyclomaticComplexity, MethodLength def load_current_resource dev_name = @new_resource.name @current = Chef::Resource::OpenstackObjectStorageDisk.new(dev_name) parted_partition_parse dev_name parts = @current.part unless @current.blocks # parted didn't return anything -- empty disk. # get size from sfdisk sfdisk_get_size(dev_name) end Chef::Log.info('About to print partition table') s = < cur_min) && (cur_size < cur_max) current_block += cur[idx][:size] Chef::Log.info("partition #{idx} #{(recreate ? 'differs' : 'is same')}: #{cur_size}/#{req_size}") idx += 1 end end if !recreate Chef::Log.info('partition table matches - not recreating') else ### make sure to ensure that there are no mounted ### filesystems on the device re = /^(#{Regexp.escape(dev_name)}[0-9]+)/ mounted = [] shell_out!('mount').stdout.each_line do |line| md = re.match(line) next unless md mounted << md[1] end mounted.each do |m| Chef::Log.info("unmounting #{m}") shell_out!("umount #{m}") end # Nuke current partition table. execute 'create new partition table' do command "parted -s -m #{dev_name} mktable gpt" end # create new partitions idx = 0 req.each do |params| start_block = 0 start_block = '1M' if idx == 0 if params[:size] == :remaining requested_size = '100%' else requested_size = "#{params[:size]}M" end s = "parted -m -s #{dev_name} " s << "mkpart #{idx} #{start_block} #{requested_size}" # #{params[:type]} Chef::Log.info("creating new partition #{idx + 1} with:" + s) execute "creating partition #{idx}" do command s end idx += 1 end update = true end # walk through the partitions and enforce disk format idx = 1 req.each do |params| device = "#{dev_name}#{idx}" Chef::Log.info("Checking #{device}") if ::File.exist?(device) # FIXME: check the format on the file system. This should be # handled by a disk format provider. Maybe the xfs/btrfs/etc # providers? Chef::Log.info("Testing file system on #{device} for type #{params[:type]}") case params[:type] when 'xfs' unless Mixlib::ShellOut.new("xfs_admin -l #{device}").run_command.exitstatus Mixlib::ShellOut.new("mkfs.xfs -f -i size=512 #{device}").run_command update = true end when 'ext4' unless Mixlib::ShellOut.new("tune2fs -l #{device} | grep \"Filesystem volume name:\" | awk \'{print $4}\' | grep -v \"\"").run_command.exitstatus Mixlib::ShellOut.new("mkfs.ext4 #{device}").run_command update = true end end end end new_resource.updated_by_last_action(update) end