linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Elsayed <eternaleye@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: Re: lvm volume like support
Date: Wed, 27 Feb 2013 01:57:55 -0800	[thread overview]
Message-ID: <kgklb0$g6a$1@ger.gmane.org> (raw)
In-Reply-To: kgkhcp$dfg$1@ger.gmane.org

Alex Elsayed wrote:

> Roman Mamedov wrote:
> 
>> On Wed, 27 Feb 2013 13:23:23 +1100
>> "Fajar A. Nugraha" <list@fajar.net> wrote:

<snip>

> This could be pretty easily put into a shell script that uses du -b and
> manually pokes configfs instead of calling tcm_node, and it'd be able to
> run without any nonstandard userspace dependencies.

Just for fun, I decided to put my money where my mouth is and implement
a quick scsi-target-losetup that actually worked, both for creation and
deletion. Here it is:

---cut---

#!/bin/bash

gen_naa() {
    local UUID="$( uuidgen -r )"
    UUID="${UUID//-/}"
    UUID="${UUID:0:9}"
    echo "naa.6001405${UUID}"
}

setup() {
    local FILE
    local INPUT_NAME
    local NAME
    local BACKEND_IDX
    local TRANSPORT_IDX
    declare -a NAA
    FILE="${1}"
    INPUT_NAME="${2}"
    NAME="${INPUT_NAME//\//_}"
    BACKEND_IDX='-1'
    TRANSPORT_IDX='-1'

    if [[ $UID -ne 0 ]]; then
        echo "You must be root in order to set up a lioloop device" >&2
        exit 1
    fi

    if [[ "${NAME}" != "${INPUT_NAME}" ]]; then
        echo "The chosen name '${INPUT_NAME}' contained slashes, using '${NAME}' instead" >&2
    fi

    declare SIZE="$(du -b "${FILE}")"
    SIZE="${SIZE/[^0123456789]*}"

    # Load the scsi target core and backends
    modprobe target_core_mod >/dev/null 2>&1

    while BACKEND_IDX=$((BACKEND_IDX + 1)); do
        if [[ -d "/sys/kernel/config/target/core/fileio_${BACKEND_IDX}/${NAME}" ]]; then
            echo "A backstore with the name '${NAME}' already exists" >&2
            exit 1
        elif ! [[ -d /sys/kernel/config/target/core/fileio_${BACKEND_IDX} ]]; then
    #        mkdir -p "/sys/kernel/config/target/core/fileio_${BACKEND_IDX}/${NAME}"
    # Tell it where the file is and how big it is
            tcm_node --establishdev "fileio_${BACKEND_IDX}/${NAME}" \
                "fd_dev_name=${FILE},fd_dev_size=${SIZE}"
    # Give it a unique serial
            tcm_node --setunitserialwithmd "fileio_${BACKEND_IDX}/${NAME}" "$(uuidgen -r)"
            break
        fi
    done

    # Load the local scsi frontend transport
    modprobe tcm_loop >/dev/null 2>&1
    mkdir -p /sys/kernel/config/target/loopback

    NAA=( "$(gen_naa)" "$(gen_naa)" )
    # Some setup so you have a place to put LUNs
    mkdir -p "/sys/kernel/config/target/loopback/${NAA[0]}/tpgt_1"
    echo "${NAA[1]}" > "/sys/kernel/config/target/loopback/${NAA[0]}/tpgt_1/nexus"

    # Create a fresh LUN...
    while TRANSPORT_IDX=$((TRANSPORT_IDX + 1)); do
        if ! [[ -d "/sys/kernel/config/target/loopback/${NAA[0]}/tpgt_1/lun/lun_${TRANSPORT_IDX}" ]]; then
            mkdir -p "/sys/kernel/config/target/loopback/${NAA[0]}/tpgt_1/lun/lun_${TRANSPORT_IDX}"
    # ...and map the file to it.
            ln -s "/sys/kernel/config/target/core/fileio_${BACKEND_IDX}/${NAME}" \
                "/sys/kernel/config/target/loopback/${NAA[0]}/tpgt_1/lun/lun_${TRANSPORT_IDX}"
            break
        fi
    done
}

teardown() {
    local INPUT_NAME="${1}"
    local NAME="${INPUT_NAME//\//_}"

    if [[ $UID -ne 0 ]]; then
        echo "You must be root in order to tear down a lioloop device" >&2
        exit 1
    fi

    if [[ "${NAME}" != "${INPUT_NAME}" ]]; then
        echo "The chosen name '${INPUT_NAME}' contained slashes, using '${NAME}' instead" >&2
    fi

    local FOUND=''
    for LUN in /sys/kernel/config/target/loopback/*/tpgt_1/lun/lun_*; do
        if [[ -L "${LUN}/${NAME}" ]]; then
            rm -f "${LUN}/${NAME}"
            FOUND=1
        fi
    done

    if [[ -z "${FOUND}" ]]; then
        echo "No lioloop with the name '${NAME}' was found" >&2
        return
    fi

    for BACKSTORE in /sys/kernel/config/target/core/fileio_*; do
        if [[ -d "${BACKSTORE}/${NAME}" ]]; then
            rmdir "${BACKSTORE}/${NAME}"
        fi
    done
}

if [[ "$1" == '-d' ]]; then
    shift;
    for name in "$@"; do
        teardown "${name}"
    done
else
    setup "$@"
fi




  reply	other threads:[~2013-02-27  9:58 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-25 23:35 lvm volume like support Suman C
2013-02-26  0:59 ` Mike Fleetwood
2013-02-26  1:46   ` Fajar A. Nugraha
2013-02-26  5:35     ` Suman C
2013-02-26  5:48       ` Remco Hosman - Yerf-IT
2013-02-26  5:57       ` Roman Mamedov
2013-02-26  6:25         ` Suman C
2013-02-26  6:28           ` Remco Hosman - Yerf IT
2013-02-26  6:37             ` Alex Elsayed
2013-02-26  7:08             ` Dave Chinner
2013-04-19 18:13           ` Andy Grover
2013-02-26 10:30     ` Martin Steigerwald
2013-02-27  2:23       ` Fajar A. Nugraha
2013-02-27  4:05         ` Roman Mamedov
2013-02-27  8:42           ` Martin Steigerwald
2013-02-27  9:17             ` Roman Mamedov
2013-02-27  8:50           ` Alex Elsayed
2013-02-27  9:57             ` Alex Elsayed [this message]
2013-02-27 10:12               ` Alex Elsayed
2013-04-08 12:01                 ` David Sterba
2013-04-11 12:29                   ` David Sterba
2013-03-02  6:24               ` Marcus Sorensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='kgklb0$g6a$1@ger.gmane.org' \
    --to=eternaleye@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).