Skip to content

Commit add6bda

Browse files
committed
cookbook renamed to fs_resize
1 parent 01527a2 commit add6bda

24 files changed

+300
-306
lines changed

.kitchen.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ platforms:
1515
suites:
1616
- name: default
1717
run_list:
18-
- recipe[partition_resize-test::default]
18+
- recipe[fs_resize-test::default]
1919
attributes:

Berksfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
site :opscode
22

33
metadata
4-
cookbook 'partition_resize-test', path: './test/kitchen/cookbooks/partition_resize-test'
4+
cookbook 'fs_resize-test', path: './test/kitchen/cookbooks/fs_resize-test'

CHANGELOG.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
partition_resize CHANGELOG
1+
fs_resize CHANGELOG
22
==========================
33

4-
This file is used to list changes made in each version of the partition_resize cookbook.
4+
This file is used to list changes made in each version of the fs_resize cookbook.
55

66
0.1.0
77
-----
8-
- [your_name] - Initial release of partition_resize
8+
- [your_name] - Initial release of fs_resize
99

1010
- - -
1111
Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax) for help with Markdown.

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Contributing
1010
7. Push to the branch (`$ git push origin my-new-feature`).
1111
8. [Submit a Pull Request using Github](https://help.github.com/articles/creating-a-pull-request).
1212

13-
Please do not hesitate to [open an issue](https://github.com/onddo/partition_resize-cookbook/issues/new) with any questions or problems.
13+
Please do not hesitate to [open an issue](https://github.com/onddo/fs_resize-cookbook/issues/new) with any questions or problems.

README.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
Description
22
===========
3-
[![Cookbook Version](https://img.shields.io/cookbook/v/partition_resize.svg?style=flat)](https://supermarket.getchef.com/cookbooks/partition_resize)
4-
[![Dependency Status](http://img.shields.io/gemnasium/onddo/partition_resize-cookbook.svg?style=flat)](https://gemnasium.com/onddo/partition_resize-cookbook)
5-
[![Code Climate](http://img.shields.io/codeclimate/github/onddo/partition_resize-cookbook.svg?style=flat)](https://codeclimate.com/github/onddo/partition_resize-cookbook)
6-
[![Build Status](http://img.shields.io/travis/onddo/partition_resize-cookbook.svg?style=flat)](https://travis-ci.org/onddo/partition_resize-cookbook)
3+
[![Cookbook Version](https://img.shields.io/cookbook/v/fs_resize.svg?style=flat)](https://supermarket.getchef.com/cookbooks/fs_resize)
4+
[![Dependency Status](http://img.shields.io/gemnasium/onddo/fs_resize-cookbook.svg?style=flat)](https://gemnasium.com/onddo/fs_resize-cookbook)
5+
[![Code Climate](http://img.shields.io/codeclimate/github/onddo/fs_resize-cookbook.svg?style=flat)](https://codeclimate.com/github/onddo/fs_resize-cookbook)
6+
[![Build Status](http://img.shields.io/travis/onddo/fs_resize-cookbook.svg?style=flat)](https://travis-ci.org/onddo/fs_resize-cookbook)
77

8-
This Chef cookbook resizes partitions automatically when the underlying disk increases its size.
8+
This Chef cookbook Resizes the file system automatically when the underlying partition or disk increases its size.
9+
10+
It is mainly oriented to work with disks in the cloud.
911

1012
Requirements
1113
============
@@ -21,7 +23,7 @@ This cookbook has been tested on the following platforms:
2123
* RedHat
2224
* Ubuntu (>= 12.04)
2325

24-
Please, [let us know](https://github.com/onddo/partition_resize-cookbook/issues/new?title=I%20have%20used%20it%20successfully%20on%20...) if you use it successfully on any other platform.
26+
Please, [let us know](https://github.com/onddo/fs_resize-cookbook/issues/new?title=I%20have%20used%20it%20successfully%20on%20...) if you use it successfully on any other platform.
2527

2628
## Applications:
2729

@@ -31,7 +33,7 @@ The other required applications usually come with the operating system:
3133

3234
* `lsblk`, `findmnt` and `losetup`: included inside **[util-linux](http://en.wikipedia.org/wiki/Util-linux) (≥ 2.19)** package.
3335
* `pgrep`: included inside [procps-ng](http://sourceforge.net/projects/procps-ng/) package.
34-
* `e2fsck` and `dumpe2fs` for *ext3* and *ext4*: included inside [e2fsprogs](http://e2fsprogs.sourceforge.net/) package.
36+
* `e2fsck`, `dumpe2fs` and `resize2fs` for *ext3* and *ext4*: included inside [e2fsprogs](http://e2fsprogs.sourceforge.net/) package.
3537
* `xfs_info` and `xfs_growfs` for *XFS*: included inside [xfsprogs](http://oss.sgi.com/projects/xfs/) package.
3638

3739
Attributes
@@ -44,18 +46,18 @@ Attributes
4446
<th>Default</th>
4547
</tr>
4648
<tr>
47-
<td><code>node['partition_resize']['compiletime']</code></td>
48-
<td>Resize the partitions at compile time.</td>
49+
<td><code>node['fs_resize']['compiletime']</code></td>
50+
<td>Resize the file systems at compile time.</td>
4951
<td><code>false</code></td>
5052
</tr>
5153
</table>
5254

5355
Recipes
5456
=======
5557

56-
## partition_resize::default
58+
## fs_resize::default
5759

58-
Resize mounted partitions.
60+
Resize mounted file systems.
5961

6062
Usage
6163
=====
@@ -66,14 +68,14 @@ You can simply include it in a recipe:
6668

6769
```ruby
6870
# in your recipe
69-
include_recipe "partition_resize::default"
71+
include_recipe "fs_resize::default"
7072
```
7173

72-
Don't forget to include the `partition_resize` cookbook as a dependency in the metadata:
74+
Don't forget to include the `fs_resize` cookbook as a dependency in the metadata:
7375

7476
```ruby
7577
# metadata.rb
76-
depends "partition_resize"
78+
depends "fs_resize"
7779
```
7880

7981
## Including in the Run List
@@ -86,27 +88,27 @@ Another alternative is to include it in your Run List:
8688
[...]
8789
"run_list": [
8890
[...]
89-
"recipe[partition_resize]"
91+
"recipe[fs_resize]"
9092
]
9193
}
9294
```
9395

9496
Testing
9597
=======
9698

97-
See [TESTING.md](https://github.com/onddo/partition_resize-cookbook/blob/master/TESTING.md).
99+
See [TESTING.md](https://github.com/onddo/fs_resize-cookbook/blob/master/TESTING.md).
98100

99101
Contributing
100102
============
101103

102-
Please do not hesitate to [open an issue](https://github.com/onddo/partition_resize-cookbook/issues/new) with any questions or problems.
104+
Please do not hesitate to [open an issue](https://github.com/onddo/fs_resize-cookbook/issues/new) with any questions or problems.
103105

104-
See [CONTRIBUTING.md](https://github.com/onddo/partition_resize-cookbook/blob/master/CONTRIBUTING.md).
106+
See [CONTRIBUTING.md](https://github.com/onddo/fs_resize-cookbook/blob/master/CONTRIBUTING.md).
105107

106108
TODO
107109
====
108110

109-
See [TODO.md](https://github.com/onddo/partition_resize-cookbook/blob/master/TODO.md).
111+
See [TODO.md](https://github.com/onddo/fs_resize-cookbook/blob/master/TODO.md).
110112

111113
License and Author
112114
==================

TODO.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
TODO
22
====
33

4-
* Support for disks with multiple partitions.
54
* LVM support.
6-
* Support for resize unmounted partitions.
5+
* Support for resize unmounted file systems.
76
* Add RSpec unit tests for libraries.

attributes/default.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# encoding: UTF-8
22

3-
default['partition_resize']['compiletime'] = false
3+
default['fs_resize']['compiletime'] = false

libraries/disk_device_base.rb

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# encoding: UTF-8
2+
3+
require 'chef/mixin/shell_out'
4+
5+
require 'forwardable'
6+
7+
module FilesystemResize
8+
# Disk device type abstract base class
9+
class DiskDeviceBase
10+
extend Forwardable
11+
extend Chef::Mixin::ShellOut
12+
13+
attr_reader :device
14+
attr_reader :loop_device
15+
16+
def initialize(dev)
17+
from_loop = Filesystems.from_loop(dev)
18+
if !from_loop.nil?
19+
@device = from_loop
20+
@loop_device = dev
21+
else
22+
@device = dev
23+
@loop_device = Filesystems.to_loop(dev)
24+
end
25+
end
26+
27+
def self.shell_out(*args)
28+
cmd = super(args)
29+
Chef::Log.debug("#shell_out: #{args[0]}")
30+
unless cmd.status.success?
31+
Chef::Log.info("#shell_out #{args[0]} (STDOUT): #{cmd.stdout}")
32+
Chef::Log.info("#shell_out #{args[0]} (STDERR): #{cmd.stderr}")
33+
end
34+
cmd
35+
end
36+
def_delegator self, :shell_out
37+
38+
def block_device
39+
@loop_device || @device
40+
end
41+
42+
def loop?
43+
!@loop_device.nil?
44+
end
45+
46+
def to_s
47+
loop? ? "#{device} (#{loop_device})" : device
48+
end
49+
end
50+
end

libraries/filesystem.rb

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# encoding: UTF-8
2+
3+
module FilesystemResize
4+
# This class represent the logical file system as seen from the
5+
# operating system
6+
class Filesystem < DiskDeviceBase
7+
def initialize(dev)
8+
super
9+
@block_count = @block_size = nil
10+
end
11+
12+
def command_running?(cmd)
13+
shell_out("pgrep '#{cmd.gsub(/'/, '')}'").status.success?
14+
end
15+
16+
def ext_size
17+
cmd = shell_out("dumpe2fs -h '#{device.gsub(/'/, '')}'")
18+
return unless cmd.status.success?
19+
cmd.stdout.split("\n").each do |line|
20+
case line
21+
when /^Block\s+count:\s+([0-9]+)$/
22+
@block_count = Regexp.last_match[1].to_i
23+
when /^Block\s+size:\s+([0-9]+)$/
24+
@block_size = Regexp.last_match[1].to_i
25+
end
26+
end
27+
end
28+
29+
def ext_resize
30+
if command_running?('resize2fs')
31+
Chef::Log.warn("#{self.class}: resize2fs already running, skipping")
32+
return false
33+
end
34+
shell_out("e2fsck -f -y '#{device.gsub(/'/, '')}'")
35+
shell_out("resize2fs '#{device.gsub(/'/, '')}'").status.success?
36+
end
37+
38+
# must be mounted
39+
def xfs_size
40+
cmd = shell_out("xfs_info '#{mount_point.gsub(/'/, '')}'")
41+
return unless cmd.status.success?
42+
cmd.stdout.split("\n").each do |line|
43+
next unless line =~ /^data\s+=\s+bsize=([0-9]+)\s+blocks=([0-9]+),/
44+
@block_size = Regexp.last_match[1].to_i
45+
@block_count = Regexp.last_match[2].to_i
46+
end
47+
end
48+
49+
def xfs_resize
50+
if command_running?('xfs_growfs')
51+
Chef::Log.warn("#{self.class}: xfs_growfs already running, skipping")
52+
return false
53+
elsif mount_point.nil?
54+
Chef::Log.warn(
55+
"#{self.class}: mount point not found for #{self}, skipping")
56+
return false
57+
end
58+
shell_out("xfs_growfs -d '#{mount_point.gsub(/'/, '')}'")
59+
.status.success?
60+
end
61+
62+
def file_type
63+
cmd = shell_out(
64+
"file --special-files --dereference '#{device.gsub(/'/, '')}'"
65+
)
66+
return nil unless cmd.status.success?
67+
cmd.stdout.split("\n")[0]
68+
end
69+
70+
# Returns the fs logical size in bytes, nil if not found or error
71+
def size
72+
@size ||= begin
73+
case type
74+
when /^ext[0-9]+$/ then ext_size
75+
when 'xfs' then xfs_size
76+
end
77+
@block_count * @block_size unless @block_count.nil? || @block_size.nil?
78+
end
79+
end
80+
81+
def type
82+
@type ||= begin
83+
if file_type =~ / ([^ ]+) filesystem /
84+
Regexp.last_match[1].downcase
85+
else
86+
nil
87+
end
88+
end
89+
end
90+
91+
def mount_point
92+
@mount_point ||= begin
93+
cmd = shell_out(
94+
'findmnt --list --first-only --canonicalize --evaluate '\
95+
'--noheadings --output TARGET '\
96+
"'#{block_device.gsub(/'/, '')}'"
97+
)
98+
cmd.status.success? ? cmd.stdout.split("\n")[0] : nil
99+
end
100+
end
101+
102+
def resize
103+
Chef::Log.debug("#{self} type: #{type}")
104+
case type
105+
when /^ext[0-9]+$/ then ext_resize
106+
when 'xfs' then xfs_resize
107+
else
108+
Chef::Log.warn("#{self.class}: unknown fs type: #{type}")
109+
return false
110+
end
111+
true # ignore console errors
112+
end
113+
end
114+
end

libraries/filesystem_disk.rb

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# encoding: UTF-8
2+
3+
module FilesystemResize
4+
# This class represent the underlying partition from the physical point of
5+
# view. If the physical partition is resized will be reflected here.
6+
class FilesystemDisk < DiskDeviceBase
7+
def self.lsblk_list
8+
cmd = shell_out('lsblk --raw --noheadings --output NAME')
9+
cmd.status.success? ? cmd.stdout : ''
10+
end
11+
12+
def lsblk_block
13+
path = block_device.gsub(/'/, '')
14+
cmd = shell_out(
15+
"lsblk --bytes --raw --noheadings --output NAME,SIZE '#{path}'"
16+
)
17+
cmd.status.success? ? cmd.stdout : ''
18+
end
19+
20+
def size
21+
@size ||= begin
22+
line = lsblk_block.split("\n")[0]
23+
dev = ::File.basename(block_device)
24+
if line =~ /^#{Regexp.escape(dev)}\s([0-9]+)/
25+
Regexp.last_match[1].to_i
26+
else
27+
nil
28+
end
29+
end
30+
end
31+
32+
def self.list
33+
lsblk_list.split("\n").each_with_object([]) do |name, devs|
34+
dev = name =~ /^\// ? name : "/dev/#{name}"
35+
devs << dev
36+
devs.delete(Regexp.last_match[1]) if dev =~ /^([^0-9]+)[0-9]+$/
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)