Skip to content

Instantly share code, notes, and snippets.

@xserveraws
Created January 5, 2018 02:32
Show Gist options
  • Save xserveraws/e96e3e346ca6f7f1c867fa7936306302 to your computer and use it in GitHub Desktop.
Save xserveraws/e96e3e346ca6f7f1c867fa7936306302 to your computer and use it in GitHub Desktop.
OpenMediaVault USB Write Minimisation

OpenMediaVault: USB Write Minimalisation Support

Generating an Alternate Preseed Configuration

A template of an alternate preseed file is provided which has been modified to include the following:

  • Selection of locale, keymap and timezone to avoid being prompted.
  • Option to choose own hostname and domain.
  • Ability to enter SHA-512, MD5 or other supported hash for the root password.
  • Custom partitioning recipe to skip swap partition installation.
  • Silencing of final reboot notice post-installation.
  • Amended late preseed command to perform the following additional steps: - Install additional packages and tweaks for write minimisation.

Modify the preseed.cfg.tpl template and save it on an accessible web server.

If you have python installed on a machine on your local network, you can simply run the following in the directory containing all of the required files:

python -m SimpleHTTPServer 8000

Specifying an Alternate Preseed Configuration

When the boot options menu appears, press ESC and enter the following at the boot: prompt that is displayed, replacing the URL with a valid URL pointing to your preseed configuration:

auto url=http://example.net/preseed.cfg

Note that the following additional files must be present alongside the preseed configuration for everything to be installed correctly:

  • omv-post-install-usb.sh

Monitoring I/O

Install the monitoring application apt-get install iotop.

Monitor with iotop --accumulated --only --processes.

Sources

#!/bin/sh
##############################################################################
# OpenMediaVault: USB Write Minimalisation Support (Post Install Command)
##############################################################################
##############################################################################
# Functions
omv_sed() {
sed -i -r "$@"
}
omv_apt_get() {
apt-get --allow-unauthenticated --assume-yes --no-install-recommends $*
}
omv_preseed() {
seed=`mktemp --suffix=.seed /tmp/XXXXXXXX`
echo "${*}" > "${seed}"
debconf-set-selections "${seed}"
rm -f "${seed}"
}
##############################################################################
# Tweaks
# Set some kernel parameters to reduce writes to the drive:
cat >> /etc/sysctl.d/99-openmediavault.conf <<-'EOF'
# Additional optimisations to reduce I/O on flash media:
vm.dirty_writeback_centisecs = 12000
vm.dirty_expire_centisecs = 12000
vm.dirty_ratio = 10
vm.dirty_background_ratio = 1
vm.laptop_mode = 0
vm.swappiness = 0
vm.vfs_cache_pressure = 50
EOF
# Use the 'noop' scheduler for flash drive:
omv_sed '/exit 0/d' /etc/rc.local
cat >> /etc/rc.local <<-'EOF'
# Use a better scheduler for our root flash drive...
device=`readlink -n /dev/root | sed 's|[0-9]||g'`
echo noop > "/sys/block/${device}/queue/scheduler"
exit 0
EOF
# Force delayed write for all log files written by syslog daemon:
omv_sed 's|\t(/var/log)|\t-\1|' /etc/rsyslog.conf
# Fix for collectd 'value too old' rootfs issue spamming logs:
output='/etc/collectd/collectd.conf'
omv_sed '/^<Plugin df>/{n;d}' ${output}
omv_sed '/^<Plugin df>/s/$/\n IgnoreSelected true/' ${output}
omv_sed '/^<Plugin df>/s/$/\n FSType rootfs/' ${output}
# Fix for broken mdadm --monitor --scan service on boot:
cat >> /etc/mdadm/mdadm.conf <<-'EOF'
# Configure a default mail address:
MAILADDR root
EOF
# Disable the bash history:
cat >> /root/.bashrc <<-'EOF'
# Some options to reduce writes to flash drive:
unset HISTFILE HISTFILESIZE HISTSIZE
EOF
# Fix issue starting the web administration interface with flashybrid:
output='/etc/apache2/mods-enabled/fcgid.conf'
omv_sed '/^<\/IfModule>/s/^/ FcgidIPCDir \/var\/run\/apache2\/fcgidsock\n/' ${output}
omv_sed '/^<\/IfModule>/s/^/ FcgidProcessTableFile \/var\/run\/apache2\/fcgid_shm\n/' ${output}
##############################################################################
# Flashybrid
# Install and enable flashybrid:
omv_preseed 'flashybrid flashybrid/remove note'
omv_apt_get install flashybrid
# Enable the flashybrid service:
echo 'ENABLED=yes' > /etc/default/flashybrid
# Create directory for mounting temporary filesystems:
mkdir /ram
# Increase the amount of memory to use for temporary filesystems:
omv_sed '/^FLASH_MAX/s/=.*/=512m/' /etc/flashybrid/config
# Fix errors in flashybrid configuration files:
omv_sed '/^\/tmp$/d' /etc/flashybrid/ramtmp
omv_sed '/^\/var\/lib\/alsa$/d' /etc/flashybrid/ramtmp
omv_sed '/^\/var\/lib\/apache$/s/$/2/' /etc/flashybrid/ramtmp
# Fix some dependency issues in the flashybrid initscript:
output='/etc/init.d/flashybrid'
omv_sed '/^# Required-(Start|Stop):/s/\$[_a-z]+/$local_fs/' ${output}
omv_sed '/^# Default-Start:/s/(:\s+).*/\1S/' ${output}
omv_sed '/^# Default-Stop:/s/(:\s+).*/\10 6/' ${output}
omv_sed '/^# Short-Description:/s/^/# X-Start-Before: $network $syslog resolvconf\n/' ${output}
omv_sed '/^# Short-Description:/s/^/# X-Stop-After: $network $syslog resolvconf\n/' ${output}
insserv --remove flashybrid
insserv flashybrid
# Add some additional flashybrid rules for OpenMediaVault:
cat >> /etc/flashybrid/ramstore <<-'EOF'
# Additional paths for OpenMediaVault:
/var/backups
/var/cache
/var/lib/apt
/var/lib/collectd
/var/lib/cron-apt
/var/lib/defoma
/var/lib/dpkg
/var/lib/initramfs-tools
/var/lib/initscripts
/var/lib/innserv
/var/lib/libuuid
/var/lib/logrotate
/var/lib/mdadm
/var/lib/misc
/var/lib/monit
/var/lib/nfs
/var/lib/openmediavault
/var/lib/pam
/var/lib/postfix
/var/lib/quota
/var/lib/samba
/var/lib/smartmontools
/var/lib/snmp
/var/lib/sudo
/var/lib/ucf
/var/lib/update-rc.d
/var/lib/usbutils
/var/www
EOF
# Add an hourly cron task to sync from /ram:
cat > /etc/cron.hourly/flashybrid-sync <<-'EOF'
#!/bin/sh
# Force synchronisation of paths managed by flashybrid:
/sbin/fh-sync > /dev/null 2>&1
# Force the changes to be written to disk:
sync
EOF
##############################################################################
# vim:et:ft=sh:nowrap:sts=4:sw=4:ts=4
##############################################################################
# OpenMediaVault: USB Write Minimalisation Support (Installer Preseed)
##############################################################################
# Localization/Keyboard:
d-i debian-installer/locale select <locale>
d-i debian-installer/keymap string <keymap>
d-i time/zone string <timezone>
# Networking:
d-i ethdetect/prompt_missing_firmware boolean false
d-i hw-detect/load_firmware boolean true
d-i netcfg/get_hostname string <hostname>
d-i netcfg/get_domain string <domain>
# Account Setup:
d-i passwd/make-user boolean false
d-i passwd/root-password-crypted password <hash>
# Partitioning:
partman-basicfilesystems partman-basicfilesystems/no_swap boolean false
d-i partman-auto/disk string /dev/sda
d-i partman-auto/method string regular
d-i partman-auto/init_automatically_partition select custom
d-i partman-auto/expert_recipe string \
omv-usb :: \
1024 1 -1 $default_filesystem \
$primary{ } $bootable{ } method{ format } format{ } \
use_filesystem{ } $default_filesystem{ } mountpoint{ / } \
options/noatime{ noatime } .
d-i partman-auto/choose_recipe select omv-usb
d-i partman/default_filesystem string ext2
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
# Boot loader installation:
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean false
# Execute post-install script:
d-i preseed/late_command string \
cp /omv-post-install.sh /target/tmp; \
in-target sh /tmp/omv-post-install.sh; \
in-target rm /tmp/omv-post-install.sh; \
cp /etc/resolv.conf /target/etc/resolvconf/run/; \
base_url=`debconf-get preseed/url | sed 's|/[^/]*$||'`; \
wget -q "${base_url}/omv-post-install-usb.sh"; \
cp /omv-post-install-usb.sh /target/tmp; \
in-target sh /tmp/omv-post-install-usb.sh; \
in-target rm /tmp/omv-post-install-usb.sh
# Skip rebooting notice:
d-i finish-install/reboot_in_progress note
##############################################################################
# vim:et:ft=conf:nowrap:sts=4:sw=4:ts=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment