Skip to content

Commit

Permalink
Use persistent paths for mkswap (1%)
Browse files Browse the repository at this point in the history
Differential Revision: D64398147

fbshipit-source-id: f49896d0a6234544807c1c94306e7ce3d1c4d082
  • Loading branch information
Cedric Van Goethem authored and facebook-github-bot committed Oct 15, 2024
1 parent 064a102 commit 3739667
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
5 changes: 5 additions & 0 deletions cookbooks/fb_swap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Attributes
* node['fb_swap']['filesystem']
* node['fb_swap']['strict']
* node['fb_swap']['allow_unmanaged']
* node['fb_swap']['use_persistent_paths']

Usage
-----
Expand Down Expand Up @@ -97,3 +98,7 @@ You can disable the sanity check which looks for unmanaged swap devices via:
```ruby
node.default['fb_swap']['allow_unmanaged'] = true
```

### use_persistent_paths
When set to true, we will persist drive identifiers that can survive a reboot
based on `/dev/disk/by-id`
1 change: 1 addition & 0 deletions cookbooks/fb_swap/attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
'swapoff_allowed_because' => nil,
'strict' => true,
'allow_unmanaged' => false,
'use_persistent_paths' => false,
}
32 changes: 32 additions & 0 deletions cookbooks/fb_swap/libraries/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

module FB
module FbSwap
DEV_ID_DIR = '/dev/disk/by-id'.freeze

def self._validate(node)
device = self._device(node)
file = self._file(node)
Expand Down Expand Up @@ -264,6 +266,36 @@ def self._device(node)
end
end

def self._device_id_map
# Create an ID map from device names to identifiers
# Sort resolvers to ensure idempotency, as there can be
# multiple symlinks pointing to the same raw device
id_map = {}

Dir.open(DEV_ID_DIR).sort.each do |entry|
next if %w{. ..}.include?(entry)

p = "#{DEV_ID_DIR}/#{entry}"
id_map[File.basename(File.readlink(p))] = entry
end
return id_map
end

# Not using persistent paths is sensitive to
# changes in device enumeration. The kernel
# provides no guarantees here
def self._get_persistent_device_path(node)
device = self._device(node)
if node['fb_swap']['use_persistent_paths']
swap_dev = device.delete_prefix('/dev/')
device_id_map = self._device_id_map
device_id = device_id_map[swap_dev]
return "#{DEV_ID_DIR}/#{device_id}"
else
return device
end
end

def self._file(node)
"#{self._filesystem(node)}swapvol/swapfile"
end
Expand Down
25 changes: 15 additions & 10 deletions cookbooks/fb_swap/templates/default/manage-swap-device.service.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
[Unit]
Description=Format size of swap device
DefaultDependencies=no
Requires=-.mount
After=-.mount

[Service]
Type=oneshot
RemainAfterExit=yes
<%
require 'shellwords'

device = FB::FbSwap._device(node)
device_path = FB::FbSwap._get_persistent_device_path(node)
# if there's an existing device, we should round-trip the existing label and
# uuid. This way if this code stops being used, and we fall back on the entry
# in fstab, we'll still find the device using the same predicate as before.
Expand All @@ -20,9 +13,21 @@ uuid = info['uuid']
# still very close to correct.
options = label ? "--label #{Shellwords.escape(label)} " : ''
options = "#{options}--uuid #{Shellwords.escape(uuid)} " if uuid
requires = FB::Systemd.path_to_unit(device_path, 'device')
-%>

[Unit]
Description=Format size of swap device
DefaultDependencies=no
Requires=<%= requires %>
After=<%= requires %>

[Service]
Type=oneshot
RemainAfterExit=yes

<% if node['fb_swap']['enabled'] -%>
ExecStart=/usr/sbin/mkswap <%= options %><%= device %> <%= node['fb_swap']['_calculated']['device_size_bytes'] / 1024 %>
ExecStart=/usr/sbin/mkswap <%= options %><%= device_path %> <%= node['fb_swap']['_calculated']['device_size_bytes'] / 1024 %>
<% else -%>
ExecStart=/usr/bin/echo "Not formatting swap when disabled"
<% end -%>

0 comments on commit 3739667

Please sign in to comment.