Initial commit
This commit is contained in:
47
roles/extroot/README.md
Normal file
47
roles/extroot/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# `flyoverhead.openwrt.extroot`
|
||||
|
||||
OpenWRT `extroot` configuration
|
||||
- configure extroot
|
||||
|
||||
## Role Variables
|
||||
|
||||
| Variable | Descritpion | Status | Type | Default/Example |
|
||||
| :--- | :--- | :--- | :--- | :--- |
|
||||
| `extroot_enabled` | Enable extroot configuration | `required` | `boolean` | `false` |
|
||||
| `extroot_pkgs` | List of packages required for extroot configuration | `required` | `list` | `["kmod-usb-core", "kmod-usb-storage", "kmod-usb2", "kmod-usb3", "block-mount", "kmod-fs-ext4", "e2fsprogs", "parted"]` |
|
||||
| `extroot_device` | External USB device name | `required` | `string` | `sda` |
|
||||
|
||||
## Dependencies
|
||||
|
||||
| Name | Description |
|
||||
| :--- | :--- |
|
||||
| `Ansible Role: openwrt` | [Ansible role by gekmihesg](https://github.com/gekmihesg/ansible-openwrt) for managing OpenWRT and derivatives |
|
||||
|
||||
## Example Playbook
|
||||
|
||||
```yaml
|
||||
- hosts: openwrt
|
||||
roles:
|
||||
- role: flyoverhead.openwrt.extroot
|
||||
```
|
||||
|
||||
## Example Vars
|
||||
|
||||
```yaml
|
||||
extroot_enabled: true
|
||||
extroot_device: "sda"
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
## Author Information
|
||||
|
||||
fly0v3rH34D
|
||||
|
||||
## References
|
||||
|
||||
- https://openwrt.org/docs/guide-user/additional-software/extroot_configuration
|
||||
- https://openwrt.org/docs/guide-user/advanced/hotplug_extras
|
||||
- https://openwrt.org/docs/guide-user/advanced/opkg_extras
|
||||
19
roles/extroot/defaults/main.yml
Normal file
19
roles/extroot/defaults/main.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
---
|
||||
# Deafult extroot status
|
||||
extroot_enabled: false
|
||||
|
||||
# extroot required packages
|
||||
extroot_pkgs:
|
||||
[
|
||||
"kmod-usb-core",
|
||||
"kmod-usb-storage",
|
||||
"kmod-usb2",
|
||||
"kmod-usb3",
|
||||
"block-mount",
|
||||
"kmod-fs-ext4",
|
||||
"e2fsprogs",
|
||||
"parted",
|
||||
]
|
||||
|
||||
# extroot device
|
||||
extroot_device: "sda"
|
||||
19
roles/extroot/files/90-extroot-restore
Normal file
19
roles/extroot/files/90-extroot-restore
Normal file
@@ -0,0 +1,19 @@
|
||||
if uci -q get fstab.overlay > /dev/null \
|
||||
&& [ ! -e /etc/extroot-restore ] \
|
||||
&& lock -n /var/lock/extroot-restore \
|
||||
&& [ -e /etc/opkg-restore-init ]
|
||||
then
|
||||
UUID="$(uci -q get fstab.overlay.uuid)"
|
||||
OVRL="$(block info | sed -n -e "/${UUID}/s/:.*$//p")"
|
||||
mount "${OVRL}" /mnt
|
||||
BAK="$(mktemp -d -p /mnt -t bak.XXXXXX)"
|
||||
mv -f /mnt/etc /mnt/upper "${BAK}"
|
||||
touch /etc/extroot-restore
|
||||
if grep -q -e "\s/overlay\s" /etc/mtab
|
||||
then cp -f -a /overlay/. /mnt
|
||||
fi
|
||||
umount "${OVRL}"
|
||||
lock -u /var/lock/extroot-restore
|
||||
reboot
|
||||
fi
|
||||
exit 1
|
||||
13
roles/extroot/handlers/main.yml
Normal file
13
roles/extroot/handlers/main.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
# Reboot device
|
||||
- name: Reboot device
|
||||
ansible.builtin.command:
|
||||
cmd: "reboot"
|
||||
register: reboot_device_status
|
||||
changed_when: reboot_device_status != 0
|
||||
|
||||
# Wait for device to boot up
|
||||
- name: Wait for device to boot up
|
||||
ansible.builtin.wait_for_connection:
|
||||
delay: 30
|
||||
timeout: 300
|
||||
12
roles/extroot/meta/main.yml
Normal file
12
roles/extroot/meta/main.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
---
|
||||
galaxy_info:
|
||||
author: flyoverhead
|
||||
description: Configure external root
|
||||
license: GPL-3.0
|
||||
min_ansible_version: "2.13"
|
||||
platforms:
|
||||
- name: OpenWrt
|
||||
versions: ["22.03"]
|
||||
galaxy_tags: ["openwrt", "extroot"]
|
||||
dependencies:
|
||||
- role: gekmihesg.openwrt
|
||||
24
roles/extroot/tasks/check.yml
Normal file
24
roles/extroot/tasks/check.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
---
|
||||
# Set extroot status to false
|
||||
# to prevent permanent configuration
|
||||
- name: Set default extroot status fact to false
|
||||
ansible.builtin.set_fact:
|
||||
extroot_configure: false
|
||||
|
||||
# Check current extroot status
|
||||
- name: Check current extroot status
|
||||
uci:
|
||||
command: "get"
|
||||
config: "fstab"
|
||||
section: "@mount[0].target"
|
||||
type: "mount"
|
||||
register: extroot_status
|
||||
failed_when: >
|
||||
extroot_status.result is undefined and
|
||||
'Entry not found' not in extroot_status.result
|
||||
|
||||
# Set extroot status fact
|
||||
- name: Set extroot status fact
|
||||
ansible.builtin.set_fact:
|
||||
extroot_configure: true
|
||||
when: extroot_device not in extroot_status.result
|
||||
68
roles/extroot/tasks/fstab.yml
Normal file
68
roles/extroot/tasks/fstab.yml
Normal file
@@ -0,0 +1,68 @@
|
||||
---
|
||||
# Get rootfs block device name
|
||||
- name: Get rootfs block device name
|
||||
ansible.builtin.command:
|
||||
cmd: 'sed -n -e "/\s\/overlay\s.*$/s///p" /etc/mtab'
|
||||
register: rootfs_block_device_name
|
||||
changed_when: rootfs_block_device_name.rc != 0
|
||||
|
||||
# Set rootfs block device name fact
|
||||
- name: Set rootfs block device name fact
|
||||
ansible.builtin.set_fact:
|
||||
block_device_name: "{{ rootfs_block_device_name.stdout }}"
|
||||
|
||||
# Mount rootfs data to another directory
|
||||
- name: Mount rootfs data to another directory
|
||||
block:
|
||||
# Add new mount point for root
|
||||
- name: Add new mount point for root
|
||||
uci:
|
||||
command: "add"
|
||||
config: "fstab"
|
||||
section: "rwm"
|
||||
type: "mount"
|
||||
|
||||
# Configure new mount point for root
|
||||
- name: Configure new mount point for root
|
||||
uci:
|
||||
command: "set"
|
||||
config: "fstab"
|
||||
section: "rwm"
|
||||
type: "mount"
|
||||
value:
|
||||
device: "{{ block_device_name }}"
|
||||
target: "/rwm"
|
||||
|
||||
# Get external USB device UUID
|
||||
- name: Get external usb device uuid
|
||||
ansible.builtin.command:
|
||||
cmd: 'block info /dev/{{ extroot_device }}1 | grep -o -e "UUID=\S*"'
|
||||
register: external_usb_device_uuid
|
||||
changed_when: external_usb_device_uuid.rc != 0
|
||||
|
||||
# Set external usb device uuid fact
|
||||
- name: Set external usb device uuid fact
|
||||
ansible.builtin.set_fact:
|
||||
usb_device_uuid: '{{ external_usb_device_uuid.stdout | regex_search(''(?<=")(.*?)(?=")'') }}'
|
||||
|
||||
# Mount external USB as overlayfs
|
||||
- name: Mount external usb as overlayfs
|
||||
block:
|
||||
# Add overlay mount point
|
||||
- name: Add overlay mount point
|
||||
uci:
|
||||
command: "add"
|
||||
config: "fstab"
|
||||
section: "overlay"
|
||||
type: "mount"
|
||||
|
||||
# Configure overlay mount point
|
||||
- name: Configure overlay mount point
|
||||
uci:
|
||||
command: "set"
|
||||
config: "fstab"
|
||||
section: "overlay"
|
||||
type: "mount"
|
||||
value:
|
||||
uuid: "{{ usb_device_uuid }}"
|
||||
target: "/overlay"
|
||||
39
roles/extroot/tasks/main.yml
Normal file
39
roles/extroot/tasks/main.yml
Normal file
@@ -0,0 +1,39 @@
|
||||
---
|
||||
# Check extroot status
|
||||
- name: Check extroot status
|
||||
ansible.builtin.include_tasks: check.yml
|
||||
when: extroot_enabled
|
||||
|
||||
# Install required packages
|
||||
- name: Install required packages
|
||||
ansible.builtin.include_tasks: prepare.yml
|
||||
when: extroot_configure
|
||||
|
||||
# Configure exteral USB drive
|
||||
- name: Configure external usb
|
||||
ansible.builtin.include_tasks: usb.yml
|
||||
when: extroot_configure
|
||||
|
||||
# Configure fstab
|
||||
- name: Configure fstab
|
||||
ansible.builtin.include_tasks: fstab.yml
|
||||
when: extroot_configure
|
||||
|
||||
# Configure system
|
||||
- name: Configure system
|
||||
ansible.builtin.include_tasks: system.yml
|
||||
when: extroot_configure
|
||||
|
||||
# Apply changes and reboot device
|
||||
- name: Apply changes and reboot device
|
||||
when: extroot_configure
|
||||
block:
|
||||
# Commit changes
|
||||
- name: Commit changes
|
||||
uci:
|
||||
command: commit
|
||||
notify: ["Reboot device", "Wait for device to boot up"]
|
||||
|
||||
# Reboot device
|
||||
- name: Reboot device
|
||||
ansible.builtin.meta: flush_handlers
|
||||
13
roles/extroot/tasks/prepare.yml
Normal file
13
roles/extroot/tasks/prepare.yml
Normal file
@@ -0,0 +1,13 @@
|
||||
---
|
||||
# Update opkg cache
|
||||
- name: Update opkg cache
|
||||
ansible.builtin.command:
|
||||
cmd: "opkg update"
|
||||
changed_when: false
|
||||
|
||||
# Install required packages
|
||||
- name: Install required packages
|
||||
opkg:
|
||||
name: "{{ item }}"
|
||||
state: "present"
|
||||
loop: "{{ extroot_pkgs }}"
|
||||
55
roles/extroot/tasks/system.yml
Normal file
55
roles/extroot/tasks/system.yml
Normal file
@@ -0,0 +1,55 @@
|
||||
---
|
||||
# Copy overlay partition content to external USB device
|
||||
- name: Copy overlay content to external usb device
|
||||
ansible.builtin.command:
|
||||
cmd: "{{ item }}"
|
||||
uses_shell: true
|
||||
loop:
|
||||
[
|
||||
"mount /dev/{{ extroot_device }}1 /mnt",
|
||||
"tar -C /overlay -cvf - . | tar -C /mnt -xf -",
|
||||
"umount /mnt",
|
||||
]
|
||||
loop_control:
|
||||
label: "{{ item }}"
|
||||
|
||||
# Configure hotplug extras
|
||||
- name: Configure hotplug extras
|
||||
ansible.builtin.command:
|
||||
cmd: "{{ item }}"
|
||||
chdir: "/tmp"
|
||||
loop:
|
||||
[
|
||||
'uclient-fetch -O hotplug-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/hotplug_extras?codeblock=0"',
|
||||
"chmod +x ./hotplug-extras.sh",
|
||||
./hotplug-extras.sh,
|
||||
]
|
||||
|
||||
# Configure opkg extras
|
||||
- name: Configure opkg extras
|
||||
ansible.builtin.command:
|
||||
cmd: "{{ item }}"
|
||||
chdir: "/tmp"
|
||||
loop:
|
||||
[
|
||||
'uclient-fetch -O opkg-extras.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/opkg_extras?codeblock=0"',
|
||||
"chmod +x ./opkg-extras.sh",
|
||||
./opkg-extras.sh,
|
||||
]
|
||||
|
||||
# Configure extroot auto restore
|
||||
- name: Configure extroot auto restore
|
||||
ansible.builtin.copy:
|
||||
src: "90-extroot-restore"
|
||||
dest: "/etc/uci-defaults/90-extroot-restore"
|
||||
owner: "root"
|
||||
group: "root"
|
||||
mode: "0644"
|
||||
|
||||
# Add auto restore to sysupgrade configuration
|
||||
- name: Add auto restore to sysupgrade.conf
|
||||
ansible.builtin.lineinfile:
|
||||
path: "/etc/sysupgrade.conf"
|
||||
line: "/etc/uci-defaults"
|
||||
insertafter: "EOF"
|
||||
state: "present"
|
||||
10
roles/extroot/tasks/usb.yml
Normal file
10
roles/extroot/tasks/usb.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
# Create new partition in external USB disk
|
||||
- name: Create partition in external usb device
|
||||
ansible.builtin.command:
|
||||
cmd: "parted -s /dev/{{ extroot_device }} -- mklabel gpt mkpart extroot 2048s -2048s"
|
||||
|
||||
# Format partition to EXT4 file system
|
||||
- name: Format partition to ext4 fs
|
||||
ansible.builtin.command:
|
||||
cmd: "mkfs.ext4 -FL extroot /dev/{{ extroot_device }}1"
|
||||
Reference in New Issue
Block a user