There's no particular reason why the directory used as the temporary mount point for the data volume needs to be random. Using a static name, on the other hand, makes it easier for the SELinux policy to apply the correct type transition and ensure the directory is labelled correctly.
57 lines
1.2 KiB
Bash
Executable File
57 lines
1.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# vim: set sw=4 ts=4 sts=4 et :
|
|
|
|
cleanup() {
|
|
if [ -n "${tmpdir}" ] && [ "${tmpdir}" != / ]; then
|
|
if mountpoint -q "${tmpdir}"; then
|
|
umount "${tmpdir}"
|
|
fi
|
|
rm -rf "${tmpdir}"
|
|
unset tmpdir
|
|
fi
|
|
}
|
|
|
|
copy_var() {
|
|
dev="$1"
|
|
|
|
echo 'Copying /var contents to data volume'
|
|
mount -o subvol=var "${dev}" "${tmpdir}" || exit
|
|
cp -auv /var/. "${tmpdir}" || exit
|
|
umount "${tmpdir}"
|
|
}
|
|
|
|
format_dev() {
|
|
dev="$1"
|
|
printf 'Creating BTRFS filesystem on %s\n' "${dev}"
|
|
mkfs.btrfs "${dev}" || exit
|
|
|
|
mount "${dev}" "${tmpdir}" || exit
|
|
btrfs subvolume create "${tmpdir}"/var || exit
|
|
btrfs subvolume create "${tmpdir}"/var/log || exit
|
|
umount "${dev}" || exit
|
|
}
|
|
|
|
has_fs() {
|
|
dev="$1"
|
|
fstype=$(blkid -o value -s TYPE "${dev}")
|
|
[ -n "${fstype}" ]
|
|
}
|
|
|
|
datapart=$(findfs PARTLABEL=dch-data)
|
|
if [ -b "${datapart}" ]; then
|
|
printf 'Found data partition: %s\n' "${datapart}"
|
|
else
|
|
echo 'Could not identify data partition' >&2
|
|
exit 1
|
|
fi
|
|
|
|
trap cleanup INT TERM QUIT EXIT
|
|
tmpdir=/run/storinit
|
|
mkdir -p "${tmpdir}"
|
|
|
|
if ! has_fs "${datapart}"; then
|
|
format_dev "${datapart}"
|
|
fi
|
|
|
|
copy_var "${datapart}"
|