Chef

Table Of Contents

mount

A resource defines the desired state for a single configuration item present on a node that is under management by Chef. A resource collection—one (or more) individual resources—defines the desired state for the entire node. During every chef-client run, the current state of each resource is tested, after which the chef-client will take any steps that are necessary to repair the node and bring it back into the desired state.

Use the mount resource to manage a mounted file system.

Syntax

The syntax for using the mount resource in a recipe is as follows:

mount "name" do
  attribute "value" # see attributes section below
  ...
  fstype "type"
  action :action # see actions section below
end

where

  • mount tells the chef-client to use the Chef::Provider::Mount provider during the chef-client run for all platforms except for Microsoft Windows, which uses the Chef::Provider::Mount::Windows provider
  • name is the name of the resource block; when the mount_point attribute is not specified as part of a recipe, name is also the directory (or path) in which a device should be mounted
  • attribute is zero (or more) of the attributes that are available for this resource
  • fstype is the file system type; this attribute is required
  • :action is the step that the resource will ask the provider to take during the chef-client run

The following is an example of how the mount resource can work when used in a recipe:

mount node['mysql']['ec2_path'] do
  device ebs_vol_dev
  fstype "xfs"
  action :mount
end

Actions

This resource has the following actions:

Action Description
:mount Default. Use to mount a device.
:umount Use to unmount a device.
:remount Use to remount a device.
:enable Use to add an entry to the file systems table (fstab).
:disable Use to remove an entry from the file systems table (fstab).

Note

Order matters when passing multiple actions. For example: action [:mount, :enable] ensures that the file system is mounted before it is enabled.

Attributes

This resource has the following attributes:

Attribute Description
device Required for :umount and :remount actions (for the purpose of checking the mount command output for presence). The special block device or remote node, a label, or a uuid to be mounted.
device_type The type of device: :device, :label, or :uuid. Default value: :device.
domain Microsoft Windows only. Use to specify the domain in which the username and password are located.
dump The dump frequency (in days) used while creating a file systems table (fstab) entry. Default value: 0.
enabled Use to specify if a mounted file system is enabled. Default value: false.
fstype Required. The file system type (fstype) of the device.
mount_point The directory (or path) in which the device should be mounted. Default value: the name of the resource block. (See “Syntax” section above for more information.)
mounted Use to specify if a file system is already mounted. Default value: false.
options An array or string that contains mount options. If this value is a string, it will be converted to an array. Default value: defaults.
pass The pass number used by the file system check (fsck) command while creating a file systems table (fstab) entry. Default value: 2.
password Microsoft Windows only. Use to specify the password for username.
provider Optional. Use to explicitly specify a provider. (See “Providers” section below for more information.)
supports A Hash of options for supported mount features. Default value: { :remount => false }.
username Microsoft Windows only. Use to specify the user name.

Providers

The chef-client will attempt to determine the correct provider during the chef-client run, and then choose the best/correct provider based on configuration data collected at the start of the chef-client run. In general, a specific provider does not need to be specified. In situations where a specific provider must be specified, there are two approaches:

  • Using a provider’s short name as the name of the resource, e.g. short_name "foo" do
  • Using the provider attribute to specify the long name as an attribute of a resource, e.g. provider Chef::Provider::Long::Name

Whenever possible, try using the short name first, and then use the long name when necessary.

This resource has the following providers:

Long name Short name Notes
Chef::Provider::Mount mount The default provider for all platforms, except for Microsoft Windows.
Chef::Provider::Mount::Windows mount The default provider for the Microsoft Windows platform.

Examples

The following examples demonstrate various approaches for using resources in recipes. If you want to see examples of how Chef uses resources in recipes, take a closer look at the cookbooks that Chef authors and maintains: https://github.com/opscode-cookbooks.

Mount a labeled file system

mount "/mnt/volume1" do
  device "volume1"
  device_type :label
  fstype "xfs"
  options "rw"
end

Mount a local block drive

mount "/mnt/local" do
  device "/dev/sdb1"
  fstype "ext3"
end

Mount a non-block file system

mount "/mount/tmp" do
  pass     0
  fstype   "tmpfs"
  device   "/dev/null"
  options  "nr_inodes=999k,mode=755,size=500m"
  action   [:mount, :enable]
end

Mount and add to the file systems table

mount "/export/www" do
  device "nas1prod:/export/web_sites"
  fstype "nfs"
  options "rw"
  action [:mount, :enable]
end

Mount a remote file system

mount "/export/www" do
  device "nas1prod:/export/web_sites"
  fstype "nfs"
  options "rw"
end

Mount a remote folder in Microsoft Windows

mount "T:" do
  action :mount
  device "\\\\hostname.example.com\\folder"
end

Unmount a remote folder in Microsoft Windows

mount "T:" do
  action :umount
  device "\\\\hostname.example.com\\D$"
end

Stop a service, do stuff, and then restart it

The following example shows how to use the execute, service, and mount resources together to ensure that a node running on Amazon EC2 is running MySQL. This example does the following:

  • Checks to see if the Amazon EC2 node has MySQL
  • If the node has MySQL, stops MySQL
  • Installs MySQL
  • Mounts the node
  • Restarts MySQL
#  the following code sample comes from the ``server_ec2`` recipe in the following cookbook: https://github.com/opscode-cookbooks/mysql

if (node.attribute?('ec2') && ! FileTest.directory?(node['mysql']['ec2_path']))

  service "mysql" do
    action :stop
  end

  execute "install-mysql" do
    command "mv #{node['mysql']['data_dir']} #{node['mysql']['ec2_path']}"
    not_if do FileTest.directory?(node['mysql']['ec2_path']) end
  end

  [node['mysql']['ec2_path'], node['mysql']['data_dir']].each do |dir|
    directory dir do
      owner 'mysql'
      group 'mysql'
    end
  end

  mount node['mysql']['data_dir'] do
    device node['mysql']['ec2_path']
    fstype "none"
    options "bind,rw"
    action [:mount, :enable]
  end

  service "mysql" do
    action :start
  end

end

where

  • the two service resources are used to stop, and then restart the MySQL service
  • the execute resource is used to install MySQL
  • the mount resource is used to mount the node and enable MySQL