Skip to content

Instantly share code, notes, and snippets.

@olafdietsche
Last active December 29, 2025 13:32
Show Gist options
  • Select an option

  • Save olafdietsche/2a77ab264d6b669c9cdbbd117e48f3be to your computer and use it in GitHub Desktop.

Select an option

Save olafdietsche/2a77ab264d6b669c9cdbbd117e48f3be to your computer and use it in GitHub Desktop.
Setup encrypted RAID 1
#! /bin/sh
# usage: $0 <device1> <device2>
# $0 /dev/sda1 /dev/sdb1
if test $# -ne 2; then
echo "usage: $0 <device1> <device2>" >&2
exit 2
fi
device1="$1"
device2="$2"
# create raid1
md_device=/dev/md0
mdadm --create "${md_device}" --level=1 --raid-devices=2 "${device1}" "${device2}"
# encrypt
cryptsetup luksFormat "${md_device}"
md_crypt=md0_crypt
cryptsetup open "${md_device}" "${md_crypt}"
# create volume group
physical_volume="/dev/mapper/${md_crypt}"
pvcreate "${physical_volume}"
volume_group=vg0
vgcreate "${volume_group}" "${physical_volume}"
# create logical volume
volume_size=10g
logical_volume=data
lvcreate --size "${volume_size}" --name "${logical_volume}" "${volume_group}"
data_device="/dev/${volume_group}/${logical_volume}"
mkfs -t ext2 -T big "${data_device}"
tune2fs -m 0 "${data_device}"
exit
# this section is for reference only
# cleanup
vgchange --activate n
cryptsetup close "${md_crypt}"
mdadm --stop "${md_device}"
mdadm --stop --scan
# reassemble
mdadm --assemble "${md_device}" "${device1}" "${device2}"
mdadm --assemble --run "${md_device}" "${device1}"
mdadm --assemble --scan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment