Skip to content

Commit 3e066c6

Browse files
authored
Packer: Hardening, Resource limits, Debug (#82)
* packer: -static 'ubuntu' user, use 'packer' (and remove) * packer: +'sos' and 'gpu_perf_config' (limits.d) * packer-rocm/re-entry: remove 'custom-packages.tar.gz' * packer-rocm/build: get plugins, run 'packer init'
1 parent ff2b4d6 commit 3e066c6

File tree

10 files changed

+151
-12
lines changed

10 files changed

+151
-12
lines changed

packer-rocm/playbooks/build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@
131131
ansible.builtin.set_fact:
132132
packer_vars: "{{ packer_rocm_hcl_awk.stdout_lines | replace('\"', '') }}"
133133

134+
- name: "Run 'packer init', ensure plugin coverage"
135+
ansible.builtin.command:
136+
cmd: 'packer init .'
137+
chdir: "{{ (workdir, packer_dist) | path_join }}"
138+
environment:
139+
PACKER_LOG: '1' # wanted as str
140+
changed_when: false # command w/ assumed changes: report 'ok' or failure
141+
134142
- name: "Run 'packer build', create '{{ _creates }}'"
135143
ansible.builtin.command:
136144
cmd: >

packer-rocm/playbooks/limits.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# yamllint disable rule:line-length
3+
# vim: ft=yaml.ansible
4+
- name: "Set limits.conf w/ 'gpu_perf_config' Role"
5+
hosts: all
6+
environment: # may be superfluous for your environment; mapped through Packer HCL with 'ansible_env_vars'
7+
http_proxy: "{{ lookup('ansible.builtin.env', 'http_proxy') | default(omit) }}"
8+
https_proxy: "{{ lookup('ansible.builtin.env', 'https_proxy') | default(omit) }}"
9+
no_proxy: "{{ lookup('ansible.builtin.env', 'no_proxy') | default(omit) }}"
10+
roles:
11+
- { role: gpu_perf_config }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* soft memlock unlimited
2+
* hard memlock unlimited
3+
* soft nofile 1048576
4+
* hard nofile 1048576
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- name: Push configuration files
3+
become: true
4+
ansible.builtin.copy:
5+
src: "{{ item.src }}"
6+
dest: "{{ item.dest }}"
7+
mode: "{{ item.mode }}"
8+
loop:
9+
- { src: "10-sre-limits.conf", mode: "0644", dest: "/etc/security/limits.d/10-sre-limits.conf" }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# sos
2+
3+
This role installs [sosreport](https://github.com/sosreport/sos),
4+
[xsos](https://github.com/ryran/xsos),
5+
and `extras.d` entries. Report _generation/collection_ is left as an admin activity.
6+
7+
## Variables
8+
9+
1. `sos_extras`: custom commands or files in `sos` reports.
10+
Default: see the [example playbook](#example)
11+
2. `sos_xsos`: controls [xsos](https://github.com/ryran/xsos) installation.
12+
Default: `true`
13+
3. `sos_xsos_url`: `xsos` installation URL.
14+
[Default](https://github.com/ryran/xsos/raw/master/xsos)
15+
16+
## Example
17+
18+
```yaml
19+
---
20+
- name: "'sos' role"
21+
hosts: all
22+
roles:
23+
- name: Configure 'sos', extras, and 'xsos'
24+
role: sos
25+
vars:
26+
sos_xsos_url: 'https://raw.githubusercontent.com/ryran/xsos/v0.7.33/xsos'
27+
sos_extras:
28+
amdgpu:
29+
- 'rocm-smi -a'
30+
yours:
31+
- ':/some/file/to/read'
32+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
# defaults file for sos
3+
#
4+
# dictionary of sos/related packages to install, mapped by Ansible fact (ansible_os_family)
5+
sos_pkgs:
6+
Debian: # includes derivatives (eg: Ubuntu, Mint, etc)
7+
- sosreport
8+
RedHat: # includes derivatives (eg: Fedora, Alma, etc)
9+
- sos
10+
11+
# provides '/etc/sos/extras.d' entries. expected to be provided by the user, see README for current example
12+
sos_extras: {}
13+
# sosreport examiner
14+
sos_xsos: true
15+
sos_xsos_url: "https://github.com/ryran/xsos/raw/master/xsos"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
# tasks file for sos
3+
4+
- name: Install packages
5+
become: true
6+
tags: ["pkgs", "packages"]
7+
ansible.builtin.package:
8+
name: "{{ sos_pkgs[ansible_os_family] }}"
9+
update_cache: "{{ true if ansible_os_family in ['RedHat', 'Debian'] else omit }}" # these modules behind 'package' will accept this
10+
11+
- name: Install 'xsos'
12+
become: true
13+
tags: ["xsos"]
14+
when: sos_xsos is truthy(convert_bool=True)
15+
ansible.builtin.get_url:
16+
url: "{{ sos_xsos_url }}"
17+
dest: /usr/local/bin/xsos
18+
mode: "+x"
19+
20+
- name: Extras
21+
become: true
22+
tags: ["extras", "extras.d"]
23+
loop: "{{ sos_extras | dict2items }}"
24+
loop_control: { loop_var: entry }
25+
ansible.builtin.copy:
26+
dest: "/etc/sos/extras.d/{{ entry.key }}"
27+
content: "{{ entry.value | join('\n') }}"
28+
mode: "0644" # lint; suggested in case umask is restricted

packer-rocm/playbooks/sos.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# yamllint disable rule:line-length
3+
# vim: ft=yaml.ansible
4+
- name: "SOS"
5+
hosts: all
6+
environment: # may be superfluous for your environment; mapped through Packer HCL with 'ansible_env_vars'
7+
http_proxy: "{{ lookup('ansible.builtin.env', 'http_proxy') | default(omit) }}"
8+
https_proxy: "{{ lookup('ansible.builtin.env', 'https_proxy') | default(omit) }}"
9+
no_proxy: "{{ lookup('ansible.builtin.env', 'no_proxy') | default(omit) }}"
10+
roles:
11+
- { role: sos }

packer-rocm/ubuntu/ubuntu-rocm.pkr.hcl

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ source "qemu" "rocm" {
2222
efi_drop_efivars = true # don't place efivars.fd in output artifact
2323
format = "raw" # qcow2 may not be converted. if written to drives, can't be read back/won't find 'curtin'
2424
headless = var.hidden
25-
shutdown_command = "sudo -S shutdown -P now"
25+
shutdown_command = "sudo -S bash -c \"rm -fv /etc/sudoers.d/packer /etc/sudoers.d/90-cloud-init-users; userdel --remove --force packer; shutdown -P now\""
2626
ssh_handshake_attempts = 500
27-
ssh_username = "ubuntu"
27+
ssh_username = "packer"
2828
ssh_password = "ubuntu"
2929
ssh_wait_timeout = "1h"
3030
ssh_timeout = "1h"
@@ -36,12 +36,13 @@ source "qemu" "rocm" {
3636
build {
3737
sources = ["source.qemu.rocm"]
3838

39-
# generate/copy tarball of custom packages; 'packer-maas' will process
39+
# regenerate/copy tarball of custom packages; 'packer-maas' will process
4040
provisioner "shell-local" {
4141
inline = [
42-
"tar cvzf ${path.root}/custom-packages.tar.gz -C ${path.root}/packages --overwrite .",
42+
"rm -f ${path.root}/custom-packages.tar.gz",
43+
"tar cvzf ${path.root}/custom-packages.tar.gz -C ${path.root}/packages ."
4344
]
44-
inline_shebang = "/bin/bash -e"
45+
inline_shebang = "/bin/bash"
4546
}
4647
provisioner "file" {
4748
destination = "/tmp/"
@@ -76,9 +77,29 @@ build {
7677
scripts = ["${path.root}/../packer-maas/ubuntu/scripts/curtin.sh", "${path.root}/../packer-maas/ubuntu/scripts/networking.sh", "${path.root}/../packer-maas/ubuntu/scripts/cloudimg/install-custom-kernel.sh"]
7778
}
7879

80+
provisioner "ansible" {
81+
playbook_file = "${path.root}/../playbooks/sos.yml"
82+
user = "packer"
83+
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
84+
extra_arguments = [
85+
"-e", "ansible_python_interpreter=/usr/bin/python3",
86+
"--scp-extra-args", "'-O'"
87+
]
88+
}
89+
90+
provisioner "ansible" {
91+
playbook_file = "${path.root}/../playbooks/limits.yml"
92+
user = "packer"
93+
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
94+
extra_arguments = [
95+
"-e", "ansible_python_interpreter=/usr/bin/python3",
96+
"--scp-extra-args", "'-O'"
97+
]
98+
}
99+
79100
provisioner "ansible" {
80101
playbook_file = "${path.root}/../playbooks/os_prep.yml"
81-
user = "ubuntu"
102+
user = "packer"
82103
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
83104
extra_arguments = [
84105
"-e", "ansible_python_interpreter=/usr/bin/python3", # work around Packer/SSH proxy+client limitations
@@ -89,7 +110,7 @@ build {
89110

90111
provisioner "ansible" {
91112
playbook_file = "${path.root}/../playbooks/amdgpu_install.yml"
92-
user = "ubuntu"
113+
user = "packer"
93114
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
94115
extra_arguments = [
95116
"-e", "ansible_python_interpreter=/usr/bin/python3",
@@ -106,7 +127,7 @@ build {
106127

107128
provisioner "ansible" {
108129
playbook_file = "${path.root}/../playbooks/tuned.yml"
109-
user = "ubuntu"
130+
user = "packer"
110131
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
111132
extra_arguments = [
112133
"-e", "ansible_python_interpreter=/usr/bin/python3",
@@ -116,7 +137,7 @@ build {
116137

117138
provisioner "ansible" {
118139
playbook_file = "${path.root}/../playbooks/niccli.yml"
119-
user = "ubuntu"
140+
user = "packer"
120141
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
121142
extra_arguments = [
122143
"-e", "ansible_python_interpreter=/usr/bin/python3",
@@ -130,7 +151,7 @@ build {
130151

131152
provisioner "ansible" {
132153
playbook_file = "${path.root}/../playbooks/tuned.yml"
133-
user = "ubuntu"
154+
user = "packer"
134155
ansible_env_vars = ["http_proxy=${var.http_proxy}", "https_proxy=${var.https_proxy}", "no_proxy=${var.no_proxy}"]
135156
extra_arguments = [
136157
"-e", "ansible_python_interpreter=/usr/bin/python3",

packer-rocm/ubuntu/user-data-rocm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ autoinstall:
33
version: 1
44
identity:
55
hostname: ubuntu
6-
username: ubuntu
6+
username: packer
77
password: "$6$5OcQlL5Sfjzsczoq$i8LnaFro3xOZ8mZ258DYtGMeWymBdLHts37F7LS.eV4SGe8cGWGWbcHQ/423aijQgMCGIKLMvEQCHpD2pSPKV0"
88
packages:
99
# support potential use of the ansible-local provisioner
@@ -55,6 +55,6 @@ autoinstall:
5555
# reset-partition: true # likely impractical with ROCm/amdgpu/etc, multiplies usage
5656
late-commands:
5757
# 'execute_command' in 'ubuntu-rocm.pkr.hcl' depends on this sudo rule
58-
- echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/ubuntu
58+
- echo 'packer ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/packer
5959
package_update: true
6060
package_upgrade: true

0 commit comments

Comments
 (0)