Skip to content

Commit 8936ed1

Browse files
committed
Import configure-swap role from openstack-zuul-jobs
1 parent f3799a6 commit 8936ed1

5 files changed

Lines changed: 272 additions & 0 deletions

File tree

configure-swap/README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Configure a swap partition
2+
3+
Creates a swap partition on the ephemeral block device (the rest of which
4+
will be mounted on /opt).
5+
6+
**Role Variables**
7+
8+
.. zuul:rolevar:: configure_swap_size
9+
:default: 1024
10+
11+
The size of the swap partition, in MiB.

configure-swap/defaults/main.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Default swap partition/file size, in MiB
2+
configure_swap_size: 1024
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
3+
# Configure attached ephemeral devices for storage and swap
4+
5+
- name: Assert that ephemeral_device is defined
6+
assert:
7+
that:
8+
- "ephemeral_device is defined"
9+
10+
- name: Set partition names
11+
set_fact:
12+
swap_partition: "{{ ephemeral_device }}1"
13+
opt_partition: "{{ ephemeral_device }}2"
14+
15+
- name: Ensure ephemeral device is unmounted
16+
become: yes
17+
ansible.posix.mount:
18+
name: "{{ ephemeral_device }}"
19+
state: "{{ item }}"
20+
with_items:
21+
- unmounted
22+
- absent
23+
# ^ https://github.com/ansible/ansible/issues/48313
24+
25+
- name: Get existing partitions
26+
become: yes
27+
community.general.parted:
28+
device: "{{ ephemeral_device }}"
29+
unit: MiB
30+
register: ephemeral_partitions
31+
32+
- name: Remove any existing partitions
33+
become: yes
34+
community.general.parted:
35+
device: "{{ ephemeral_device }}"
36+
number: "{{ item.num }}"
37+
state: absent
38+
with_items:
39+
- "{{ ephemeral_partitions.partitions }}"
40+
41+
- name: Create new disk label
42+
become: yes
43+
community.general.parted:
44+
label: msdos
45+
device: "{{ ephemeral_device }}"
46+
47+
- name: Create swap partition
48+
become: yes
49+
community.general.parted:
50+
device: "{{ ephemeral_device }}"
51+
number: 1
52+
state: present
53+
part_start: '0%'
54+
part_end: "{{ configure_swap_size }}MiB"
55+
56+
- name: Create opt partition
57+
become: yes
58+
community.general.parted:
59+
device: "{{ ephemeral_device }}"
60+
number: 2
61+
state: present
62+
part_start: "{{ configure_swap_size }}MiB"
63+
part_end: "100%"
64+
65+
- name: Make swap on partition
66+
become: yes
67+
command: "mkswap {{ swap_partition }}"
68+
69+
- name: Write swap to fstab
70+
become: yes
71+
ansible.posix.mount:
72+
path: none
73+
src: "{{ swap_partition }}"
74+
fstype: swap
75+
opts: sw
76+
passno: 0
77+
dump: 0
78+
state: present
79+
80+
# XXX: does "parted" plugin ensure the partition is available
81+
# before moving on? No udev settles here ...
82+
83+
- name: Add all swap
84+
become: yes
85+
command: swapon -a
86+
87+
- name: Create /opt filesystem
88+
become: yes
89+
community.general.filesystem:
90+
fstype: ext4
91+
# The default ratio is 16384 bytes per inode or so. Reduce that to 8192
92+
# bytes per inode so that we get roughly twice the number of inodes as
93+
# by default. This should still be well above the block size of 4096.
94+
# We do this because we have found in at least a couple locations that
95+
# more inodes is useful and is painful to fix after the fact.
96+
opts: -i 8192
97+
dev: "{{ opt_partition }}"
98+
99+
# Rackspace at least does not have enough room for two devstack
100+
# installs on the primary partition. We copy in the existing /opt to
101+
# the new partition on the ephemeral device, and then overmount /opt
102+
# to there for the test runs.
103+
#
104+
# NOTE(ianw): the existing "mount" touches fstab. There is currently (Sep2017)
105+
# work in [1] to split mount & fstab into separate parts, but for now we bundle
106+
# it into an atomic shell command
107+
# [1] https://github.com/ansible/ansible/pull/27174
108+
- name: Copy old /opt
109+
become: yes
110+
register: moving_opt
111+
shell: |
112+
mount {{ opt_partition }} /mnt
113+
find /opt/ -mindepth 1 -maxdepth 1 -print -exec mv {} /mnt/ \;
114+
umount /mnt
115+
df -h
116+
tags:
117+
- skip_ansible_lint
118+
119+
- name: Output data from old /opt
120+
debug:
121+
var: moving_opt
122+
123+
# This overmounts any existing /opt
124+
- name: Add opt to fstab and mount
125+
become: yes
126+
ansible.posix.mount:
127+
path: /opt
128+
src: "{{ opt_partition }}"
129+
fstype: ext4
130+
opts: noatime
131+
state: mounted

configure-swap/tasks/main.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
3+
# On RAX hosts, we have a small root partition and a large,
4+
# unallocated ephemeral device attached at /dev/xvde
5+
- name: Set ephemeral device if /dev/xvde exists
6+
when: ansible_devices["xvde"] is defined
7+
set_fact:
8+
ephemeral_device: "/dev/xvde"
9+
10+
# On other providers, we have a device called "ephemeral0".
11+
#
12+
# NOTE(ianw): Once [1] is in our ansible (2.4 era?), we can figure
13+
# this out more directly by walking the device labels in the facts
14+
#
15+
# [1] https://github.com/ansible/ansible/commit/d46dd99f47c0ee5081d15bc5b741e9096d8bfd3e
16+
- name: Set ephemeral device by label
17+
when: ephemeral_device is undefined
18+
block:
19+
- name: Get ephemeral0 device node
20+
command: /sbin/blkid -L ephemeral0
21+
register: ephemeral0
22+
# rc !=0 is expected
23+
failed_when: False
24+
changed_when: False
25+
26+
- name: Set ephemeral device if LABEL exists
27+
when: "ephemeral0.rc == 0"
28+
set_fact:
29+
ephemeral_device: "{{ ephemeral0.stdout }}"
30+
31+
# If we have ephemeral storage and we don't appear to have setup swap,
32+
# we will create a swap and move /opt to a large data partition there.
33+
- name: Setup swap on ephemeral storage
34+
include_tasks: ephemeral.yaml
35+
when:
36+
- ephemeral_device is defined
37+
- ansible_memory_mb['swap']['total'] | int + 10 <= configure_swap_size
38+
39+
# If no ephemeral device and no swap, then we will setup some swap
40+
# space on the root device to ensure all hosts a consistent memory
41+
# environment.
42+
- name: Setup swap file on root device
43+
include_tasks: root.yaml
44+
when:
45+
- ephemeral_device is undefined
46+
- ansible_memory_mb['swap']['total'] | int + 10 <= configure_swap_size
47+
48+
# ensure a standard level of swappiness. Some platforms
49+
# (rax+centos7) come with swappiness of 0 (presumably because the
50+
# vm doesn't come with swap setup ... but we just did that above),
51+
# which depending on the kernel version can lead to the OOM killer
52+
# kicking in on some processes despite swap being available;
53+
# particularly things like mysql which have very high ratio of
54+
# anonymous-memory to file-backed mappings.
55+
#
56+
# This sets swappiness low; we really don't want to be relying on
57+
# cloud I/O based swap during our runs if we can help it
58+
- name: Set swappiness
59+
become: yes
60+
ansible.posix.sysctl:
61+
name: vm.swappiness
62+
value: 30
63+
state: present
64+
65+
- name: Debug the ephemeral_device variable
66+
debug:
67+
var: ephemeral_device

configure-swap/tasks/root.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
3+
# If no ephemeral devices are available, use root filesystem
4+
5+
- name: Calculate required swap
6+
set_fact:
7+
swap_required: "{{ configure_swap_size - ansible_memory_mb['swap']['total'] | int }}"
8+
9+
- name: Get root filesystem
10+
block:
11+
- name: Get root filesystem
12+
shell: df --output='fstype' /root | tail -1
13+
register: root_fs
14+
15+
- name: Save root filesystem
16+
set_fact:
17+
root_filesystem: "{{ root_fs.stdout }}"
18+
19+
- name: Debug the root_filesystem variable
20+
debug:
21+
var: root_filesystem
22+
23+
# Note, we don't use a sparse device to avoid wedging when disk space
24+
# and memory are both unavailable.
25+
26+
- name: Create swap backing file
27+
become: yes
28+
command: dd if=/dev/zero of=/root/swapfile bs=1M count={{ swap_required }}
29+
args:
30+
creates: /root/swapfile
31+
32+
- name: Ensure swapfile perms
33+
become: yes
34+
file:
35+
path: /root/swapfile
36+
owner: root
37+
group: root
38+
mode: '0600'
39+
40+
- name: Make swapfile
41+
become: yes
42+
command: mkswap /root/swapfile
43+
44+
- name: Write swap to fstab
45+
become: yes
46+
ansible.posix.mount:
47+
path: none
48+
src: /root/swapfile
49+
fstype: swap
50+
opts: sw
51+
passno: 0
52+
dump: 0
53+
state: present
54+
55+
- name: Add all swap
56+
become: yes
57+
command: swapon -a
58+
59+
- name: Debug the swap_required variable
60+
debug:
61+
var: swap_required

0 commit comments

Comments
 (0)