scripts/zpool.creation.sh

75 lines
2.0 KiB
Bash
Raw Normal View History

#!/bin/sh
POOL_NAME="r2-d2"
GROUP_NUMBER="4"
DISK_BY_GROUP="11"
ZPOOL_CREATION_CMD="sudo zpool create -f ${POOL_NAME} -m /mnt/${POOL_NAME} "
ZPOOL_SPARE_CMD="sudo zpool add ${POOL_NAME} spare"
DISK_TEMP_LIST="/tmp/zfs.disks"
# Store disks list in a temp file
find /dev/disk/by-vdev -maxdepth 1 -type l -printf "%f\\n" > "${DISK_TEMP_LIST}"
2018-06-25 17:07:19 +02:00
# Tests {{{
## zfs module {{{
2018-06-25 17:01:07 +02:00
if ! lsmod | grep -q "^zfs"
then
printf "%b\\n" "Try to load ZFS module"
modprobe zfs
fi
2018-06-25 17:07:19 +02:00
## }}}
## If a previous ZFS partitioning already exists {{{
if find /dev/disk/by-vdev -maxdepth 1 -type l -iname "*part[0-9]*" > /dev/null 2>&1
then
printf "%b\\n" "Some disks are already partitioned, please verify that you don't have a working ZFS configuration!"
exit 1
fi
## }}}
2018-06-25 17:01:07 +02:00
# }}}
# Pool creation {{{
## Initialize loop vars
group=1
start_disk_group=1
end_disk_group=11
while [ "${group}" -le "${GROUP_NUMBER}" ]
do
## Get the next $DISK_BY_GROUP number of disks for this group
GROUP_DISK="$(sed -n ${start_disk_group},${end_disk_group}p -- ${DISK_TEMP_LIST} | tr '\n' ' ')"
## Display information
#printf "%b" "Group ${group} (disk ${start_disk_group} to ${end_disk_group}): ${GROUP_DISK}\\n"
## Update the creation command
ZPOOL_CREATION_CMD="${ZPOOL_CREATION_CMD}\\
raidz3 ${GROUP_DISK}"
## Increment variables
group=$((group+1))
start_disk_group=$((end_disk_group+1))
end_disk_group=$((end_disk_group+DISK_BY_GROUP))
done
printf "%b\\n" "++++++ You can use this command to create your ZSH pool +++++++++"
printf "%b\\n" "${ZPOOL_CREATION_CMD}"
printf "%b\\n" "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
# }}}
# Pool spare {{{
## Get the last disks for the spare command
SPARE_DISK="$(tail -n +${start_disk_group} -- ${DISK_TEMP_LIST} | tr '\n' ' ')"
ZPOOL_SPARE_CMD="${ZPOOL_SPARE_CMD} ${SPARE_DISK}"
printf "%b\\n" "++++ You can use this command to add spare disks to the pool ++++"
printf "%b\\n" "${ZPOOL_SPARE_CMD}"
printf "%b\\n" "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
# }}}
# Delete the temp list file
rm -f -- "${DISK_TEMP_LIST}"
exit 0