I have written a recipe to create multiple logical volumes as part of setup for installing an application. I have a hash in the recipe that I've created by reading a JSON file which has all the metadata for the drives.
The recipe loops over this hash and is supposed to first create a logical volume and then execute a tune2fs command, immediately.
Now assume we're in the first iteration of the loop. It creates the logical volume just fine, for the first entry in the hash (lv_u01) but then when it notifies the execute block (in side the same loop, immediately), it somehow interpolates the key/values from the last entry in the hash (lv_u03). I have tried multiple times and it keeps doing the same thing. I searched and couldn't find any relevant information on why it's doing this. Is it something do with the timers? I want to run the tune2fs command right after it creates the logical volume, as later in the loop I mount the volume, and this steps needs to happen before the other steps run. Which is why I used immediately.
Can someone please help me understand what am I doing wrong here.
Here's the error that I see:
* lvm_logical_volume[lv_u01] action create
* execute[Run tune2fs] action run
================================================================================
Error executing action `run` on resource 'execute[Run tune2fs]'
================================================================================
Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of tune2fs -i 0 -c 0 /dev/my_vg/lv_u03 ----
STDOUT: tune2fs 1.45.4 (23-Sep-2019)
STDERR: tune2fs: No such file or directory while trying to open /dev/my_vg/lv_u03
Couldn't find valid filesystem superblock.
---- End output of tune2fs -i 0 -c 0 /dev/my_vg/lv_u03 ----
Ran tune2fs -i 0 -c 0 /dev/my_vg/lv_u03 returned 1
Resource Declaration:
---------------------
# In /tmp/kitchen/cache/cookbooks/cookbook_name/recipes/recipe.rb
88: execute 'Run tune2fs' do
89: command "tune2fs -i 0 -c 0 /dev/#{val_hash['vg_name']}/#{lv_name}"
90: action :nothing
91: end
92:
Compiled Resource:
------------------
# Declared in /tmp/kitchen/cache/cookbooks/cookbook_name/recipes/recipe.rb:88:in `block in from_file'
execute("Run tune2fs") do
action [:nothing]
default_guard_interpreter :execute
command "tune2fs -i 0 -c 0 /dev/my_vg/lv_u03"
backup 5
declared_type :execute
cookbook_name "cookbook_name"
recipe_name "recipe_name"
domain nil
user nil
end
System Info:
------------
chef_version=14.14.25
platform=centos
platform_version=8.2.2004
ruby=ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-linux]
program_name=/bin/chef-client
executable=/opt/chef/bin/chef-client
Here's the code:
recipe.rb
# Read metadata about drives into a hash
require 'json'
json_file = File.open 'lv_metadata.json'
lv_hash = JSON.load json_file
json_file.close
# Create and format logical volumes.
lv_hash.each do |lv_name, val_hash|
# This resource will create the logical volume based on the parameters available in lv_hash and then format it
# If sizing is static, use the fixed value else use 100% of the free space
lvm_logical_volume lv_name do
group val_hash['vg_name']
size lazy {val_hash['sizing'] == 'static' ? "#{val_hash['disk_sizes'][app_env][vm_size]}G" : "100%FREE"}
filesystem 'ext3'
filesystem_params lazy {lv_name == 'lv_u02' ? "-m 0 -b #{val_hash['block_size']} -J size=256" : "-m 0 -b #{val_hash['block_size']}"}
notifies :run, "execute[Run tune2fs]", :immediately
end
# Runs tune2fs to disable time-dependent checking.
# When setting max-mount-count to 0, the number of times the filesystem is mounted will be disregarded by e2fsck(8) and the kernel.
execute 'Run tune2fs' do
command "tune2fs -i 0 -c 0 /dev/#{val_hash['vg_name']}/#{lv_name}"
action :nothing
end
# Create the directory where the logical volume will be mounted. Give it a
directory "#{val_hash['filesystem_name']}" do
owner val_hash['owner_name']
group val_hash['group_name']
mode val_hash['permissions']
recursive true
action :create
end
# Mount the logical volume at the specified filesystem path and enable it to add entry to fstab.
mount "#{val_hash['filesystem_name']}" do
device "/dev/#{val_hash['vg_name']}/#{lv_name}"
fstype 'ext3'
options val_hash['options']
dump 0
pass 0
action [:mount, :enable]
end
end
lv_metadata.json
{
"lv_u01": {
"vg_name": "vg_username",
"filesystem_name": "/u01",
"owner_name": "username",
"group_name": "groupname",
"permissions": 755,
"options": "defaults",
"sizing": "static",
"disk_sizes": {
"dev": {
"small": 50,
"medium": 50,
"large": 50
},
"qa": {
"small": 100,
"medium": 100,
"large": 100
},
"prod": {
"small": 200,
"medium": 200,
"large": 200
}
},
"block_size": 4096
},
"lv_u02": {
"vg_name": "my_vg",
"filesystem_name": "/u02",
"owner_name": "username",
"group_name": "groupname",
"permissions": 755,
"options": "defaults",
"sizing": "static",
"disk_sizes": {
"dev": {
"small": 5,
"medium": 5,
"large": 5
},
"qa": {
"small": 8,
"medium": 8,
"large": 100
},
"prod": {
"small": 10,
"medium": 10,
"large": 200
}
},
"block_size": 4096
},
"lv_u03": {
"vg_name": "my_vg",
"filesystem_name": "/u03",
"owner_name": "username",
"group_name": "groupname",
"permissions": 755,
"options": "defaults",
"sizing": "free",
"disk_sizes": {
"dev": {
"small": 5,
"medium": 5,
"large": 5
},
"qa": {
"small": 5,
"medium": 5,
"large": 5
},
"prod": {
"small": 5,
"medium": 5,
"large": 5
}
},
"block_size": 1024
}
}
You shouldn't be using resources with the same name, that actually do different things. You have 3 resources in the resource collection eventually with the same name: execute 'Run tune2fs'
, but every one of them doing its own thing, because the command "tune2fs -i 0 -c 0 /dev/#{val_hash['vg_name']}/#{lv_name}"
are different.
Chef just does not know which resource of 3, you are notifying with
notifies :run, "execute[Run tune2fs]", :immediately
Use different names for your execute resources:
lvm_logical_volume lv_name do
group val_hash['vg_name']
size lazy {val_hash['sizing'] == 'static' ? "#{val_hash['disk_sizes'][app_env][vm_size]}G" : "100%FREE"}
filesystem 'ext3'
filesystem_params lazy {lv_name == 'lv_u02' ? "-m 0 -b #{val_hash['block_size']} -J size=256" : "-m 0 -b #{val_hash['block_size']}"}
notifies :run, "execute[Run tune2fs for #{lv_name}]", :immediately
end
# Runs tune2fs to disable time-dependent checking.
# When setting max-mount-count to 0, the number of times the filesystem is mounted will be disregarded by e2fsck(8) and the kernel.
execute "Run tune2fs for #{lv_name}" do
command "tune2fs -i 0 -c 0 /dev/#{val_hash['vg_name']}/#{lv_name}"
action :nothing
end