Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active June 8, 2024 15:10
Show Gist options
  • Save miraculixx/bdd54449fc1095589ce6cbc9f169d442 to your computer and use it in GitHub Desktop.
Save miraculixx/bdd54449fc1095589ce6cbc9f169d442 to your computer and use it in GitHub Desktop.
mount multiple longhorn replicas from a single command

longhorn replicas

Mount all replicas found at /mnt/snapshot

$ sudo ./mount-longhorn-replica.sh start

Show status

$ sudo ./mount-longhorn-replica.sh status

Unmount all replicas

$ sudo ./mount-longhorn-replica.sh stop

Example

$ sudo ./mount-longhorn-replica.sh start
found replica pvc-e3399fd0-4a9f-4aa0-8b77-3e23d616955e-6210230e, size 8589934592 in /mnt/snapshot/var/lib/longhorn/replicas/pvc-e3399fd0-4a9f-4aa0-8b77-3e23d616955e-6210230e
0a444e34d9591c792e469315ab0cdcaa15b883ef83400606809f8166687eb8b8
waiting for /dev/longhorn/pvc-e3399fd0-4a9f-4aa0-8b77-3e23d616955e-6210230e
.........Mounted /dev/longhorn/pvc-e3399fd0-4a9f-4aa0-8b77-3e23d616955e-6210230e at /mnt/longhorn/pvc-e3399fd0-4a9f-4aa0-8b77-3e23d616955e-6210230e
found replica pvc-cb830c27-bff7-448f-b688-2acf9f6739f8-1d14c2ee, size 5368709120 in /mnt/snapshot/var/lib/longhorn/replicas/pvc-cb830c27-bff7-448f-b688-2acf9f6739f8-1d14c2ee
7b0ba35ce97e068f0dfa01380705f8788d4cd926b7543636791f3422c716e110
waiting for /dev/longhorn/pvc-cb830c27-bff7-448f-b688-2acf9f6739f8-1d14c2ee
.........Mounted /dev/longhorn/pvc-cb830c27-bff7-448f-b688-2acf9f6739f8-1d14c2ee at /mnt/longhorn/pvc-cb830c27-bff7-448f-b688-2acf9f6739f8-1d14c2ee
found replica pvc-5ab34c92-1943-4f93-a80e-1ecde29090e2-bd7a6e6c, size 8589934592 in /mnt/snapshot/var/lib/longhorn/replicas/pvc-5ab34c92-1943-4f93-a80e-1ecde29090e2-bd7a6e6c
aac29f6d86817ccd23d8398744a541a941dd07bd0041ef5176f95b3759fad4bf
waiting for /dev/longhorn/pvc-5ab34c92-1943-4f93-a80e-1ecde29090e2-bd7a6e6c
.........Mounted /dev/longhorn/pvc-5ab34c92-1943-4f93-a80e-1ecde29090e2-bd7a6e6c at /mnt/longhorn/pvc-5ab34c92-1943-4f93-a80e-1ecde29090e2-bd7a6e6c
found replica pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17, size 8589934592 in /mnt/snapshot/var/lib/longhorn/replicas/pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17
585388c57e4c881c8ccda9a00f5aa5d990c1e57dbb9526d0aa68c67adc317b64
waiting for /dev/longhorn/pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17
.........Mounted /dev/longhorn/pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17 at /mnt/longhorn/pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17
found replica pvc-3efc6d7c-b412-45e3-a866-1828e5ae8d3e-65ecee92, size 8589934592 in /mnt/snapshot/var/lib/longhorn/replicas/pvc-3efc6d7c-b412-45e3-a866-1828e5ae8d3e-65ecee92
e8e5afe2c50bdcb39b59dd6dfe6511c80c3aba064a05dae269853c51909c45a6
waiting for /dev/longhorn/pvc-3efc6d7c-b412-45e3-a866-1828e5ae8d3e-65ecee92
.........Mounted /dev/longhorn/pvc-3efc6d7c-b412-45e3-a866-1828e5ae8d3e-65ecee92 at /mnt/longhorn/pvc-3efc6d7c-b412-45e3-a866-1828e5ae8d3e-65ecee92
found replica pvc-dcb2b5f1-972c-453d-8042-5956595f84cb-73812c0a, size 21474836480 in /mnt/snapshot/var/lib/longhorn/replicas/pvc-dcb2b5f1-972c-453d-8042-5956595f84cb-73812c0a
f1d99434fdcfd3d864439c2c5393d7115d56ec43e1feb27fcb9cf764c957bcb4
waiting for /dev/longhorn/pvc-dcb2b5f1-972c-453d-8042-5956595f84cb-73812c0a
.........Mounted /dev/longhorn/pvc-dcb2b5f1-972c-453d-8042-5956595f84cb-73812c0a at /mnt/longhorn/pvc-dcb2b5f1-972c-453d-8042-5956595f84cb-73812c0a

How it works

Source: https://longhorn.io/docs/1.5.3/advanced-resources/data-recovery/export-from-replica/

Find the longhorn files as

$ find /mnt/snapshot -type d | grep replicas
/mnt/snapshot/var/lib/longhorn/replicas
/mnt/snapshot/var/lib/longhorn/replicas/pvc-e3399fd0-4a9f-4aa0-8b77-3e23d616955e-6210230e
/mnt/snapshot/var/lib/longhorn/replicas/pvc-cb830c27-bff7-448f-b688-2acf9f6739f8-1d14c2ee
/mnt/snapshot/var/lib/longhorn/replicas/pvc-5ab34c92-1943-4f93-a80e-1ecde29090e2-bd7a6e6c
/mnt/snapshot/var/lib/longhorn/replicas/pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17
/mnt/snapshot/var/lib/longhorn/replicas/pvc-3efc6d7c-b412-45e3-a866-1828e5ae8d3e-65ecee92
/mnt/snapshot/var/lib/longhorn/replicas/pvc-dcb2b5f1-972c-453d-8042-5956595f84cb-73812c0a

Each of this should be mountable like this

$ cat pvc-06b4a8a8-b51d-42c6-a8cc-d8c8d6bc65bc-d890efb2/volume.meta 
{"Size":1073741824,"Head":"volume-head-000.img","Dirty":true,"Rebuilding":false,"Parent":"","SectorSize":512,"BackingFileName":""}

must copy Size value to the following command

mount using longhorn docker run -v /dev:/host/dev -v /proc:/host/proc -v <data_path>:/volume --privileged longhornio/longhorn-engine:v1.5.3 launch-simple-longhorn <volume_name> <volume_size>


example: 

docker run -v /dev:/host/dev -v /proc:/host/proc -v /var/lib/longhorn/replicas/pvc-06b4a8a8-b51d-42c6-a8cc-d8c8d6bc65bc-d890efb2:/volume --privileged longhornio/longhorn-engine:v1.5.3 launch-simple-longhorn pvc-06b4a8a8-b51d-42c6-a8cc-d8c8d6bc65bc 1073741824


=> creates block device at /dev/longhorn/pvc-*

$ sudo mount -o ro /dev/longhorn/pvc-e2b1512d-f612-4d61-943f-16a0ad90daaa-f7ffcc17 /mnt/pvc


=> now data is in /mnt/pvc

MIT NON-AI License
Copyright (c) 2024, miraculixx
Permission is hereby granted, free of charge, to any person obtaining a copy of the software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
In addition, the following restrictions apply:
1. The Software and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms,
including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives,
modifications, or updates based on the Software code. Any usage of the Software in an AI-training dataset is considered a breach of this License.
2. The Software may not be included in any dataset used for training or improving machine learning algorithms,
including but not limited to artificial intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions will be subject to legal action and may be held liable
for any damages resulting from such use.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/bin/bash
# implements longhorn replica mounting from an image
# -- https://longhorn.io/docs/1.5.3/advanced-resources/data-recovery/export-from-replica/
function mountreplica()
{
replicadir=$1
name=$(basename $replicadir)
mountdir="/mnt/longhorn/$name"
devdir="/dev/longhorn/$name"
mkdir -p $mountdir
size=$(cat $replicadir/volume.meta | jq .Size)
echo "found replica $name, size $size in $replicadir"
docker run -d -v /dev:/host/dev -v /proc:/host/proc -v $replicadir:/volume --privileged longhornio/longhorn-engine:v1.5.3 launch-simple-longhorn $name $size
echo "waiting for $devdir"
while [ ! -b $devdir ]; do
echo -n .
sleep 1
done
mount -o ro $devdir $mountdir
echo "Mounted $devdir at $mountdir"
}
function startlonghorn()
{
for replicadir in $(find /mnt/snapshot -type d | grep replicas/pvc | xargs); do
mountreplica $replicadir
done
}
function stoplonghorn()
{
docker ps | grep longhorn | cut -f1 -d' ' | xargs docker kill
ls /dev/longhorn/pvc* | xargs rm
}
function status()
{
docker ps | grep longhorn
echo "devices:"
echo "$(ls /dev/longhorn)"
}
case "$1" in
start) # start
startlonghorn
;;
stop) # stop
stoplonghorn
;;
status) # status
status
;;
*) # help
echo "mount longhorn images from replicas"
echo "usage: $(basename $0) start|stop"
exit 0
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment