<commit_before>#!/bin/bash

#------------------------------------------------------------------------------
# Copyright (c) 2016, <NAME>
# All rights reserved.
# This code is licensed under standard 3-clause BSD license.
# See file LICENSE supplied with this package for the full license text.
#------------------------------------------------------------------------------

SCRDIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
. "${SCRDIR}"/pVM.cfg

while [ $# -gt 0 ]
do
    key="$1"
    case $key in
        -d)
            DRIVES=(${2//,/ })
            shift
            ;;
        -i)
            INDRIVES=(${2//,/ })
            shift
            ;;
        -o)
            OUTDIRS=(${2//,/ })
            shift
            ;;
        -m)
            MEM=(${2//,/ })
            shift
            ;;
        -c)
            CORES=(${2//,/ })
            shift
            ;;
        -a)
            ADDITIONAL=(${2//,/ })
            shift
            ;;
        --install)
            INSTALLING=true
            ;;
        -h|--help)
            cat <<EOF
Usage: $0 [-d DRIVE] [-i SQSH|DIR] [-o DIR] [-m MEM] [-c CORES] [-a OPTS] \
[--install]
EOF
            exit 0
            ;;
    esac
    shift
done

case $NET_TYPE in
    tap)
        case $USER in
        root)
            echo "Running with tap network."
            ;;
        *)
            echo "Error: tap network can be run with root privileges only!"
            exit 1
        esac
        ;;
    user)
        echo "Running with user network."
        ;;
    *)
        echo "Error: NET_TYPE should be defined as \"tap\" or \"user\" only!"
        exit 1
esac

CURR_USER=${SUDO_USER:-${USER}}
SSH_LOCAL=$(awk '{ print $3 ":" $4 }' <<< ${SSH_CONNECTION})

numbers_dont_match() {
    echo "Check config arrays!"
    exit 1
}

getmac() {
    local MAC_I="56:3c:4c:"
    local MAC_F=":cc"
    local MAC_M=$(printf "%04d" $1 | sed 's/.\{2\}/&:/' | cut -c 1-5)
    printf "%s%s%s" ${MAC_I} ${MAC_M} ${MAC_F}
}

zerolead() {
    printf "%02d" $1
}

calcvec() {
    printf "$(( $1 * 2 + 2 ))"
}

name_from_drive() {
    local DRIVE=$(basename $1)
    printf "%s_%s" ${DRIVE%.*} $2
}

start_dnsmasq() {
    dnsmasq -i ${VM_BRIDGE} -z -F "${LEASE_RANGE}" -x "${SCRDIR}"/dnsmasq.pid
    chown ${CURR_USER}:$(id -g -n ${CURR_USER}) "${SCRDIR}"/dnsmasq.pid
}

stop_dnsmasq() {
    kill -9 $(cat "${SCRDIR}"/dnsmasq.pid)
    rm -r "${SCRDIR}"/dnsmasq.pid
}

create_bridge() {
    ovs-vsctl add-br ${VM_BRIDGE}
    ip link set ${VM_BRIDGE} up
    ip addr add ${BR_ADDR} dev ${VM_BRIDGE}
}

remove_bridge() {
    ip link set ${VM_BRIDGE} down
    ovs-vsctl del-br ${VM_BRIDGE}
}

create_bridge_scripts() {
cat > "${SCRDIR}"/upscript.sh <<EOF
#!/bin/sh
ip link set \$1 up
ovs-vsctl add-port ${VM_BRIDGE} \$1
EOF
chmod 755 "${SCRDIR}"/upscript.sh
chown ${CURR_USER}:$(id -g -n ${CURR_USER}) "${SCRDIR}"/upscript.sh
}

remove_bridge_scripts() {
    rm -f "${SCRDIR}"/upscript.sh
}

kill_machines() {
    for j in $@
    do
        kill -9 $j
    done
}

squashdrive_cmd() {
    if [ -f "$2" ]
    then
        printf -- "-drive file=%s,if=none,id=rdrive%s,readonly,format=raw
                   -device scsi-hd,drive=rdrive%s" "$2" "$1" "$1"
    fi
}

drivedir_cmd() {
    # Arguments: name number ro|rw /path/on/host
    if [ -d "$4" ]
    then
        local ID="$1dir$(zerolead $2)"
        local TAG="$1"
        case $3 in
        ro)
            local LOCRO=",readonly"
        esac
        printf -- "-virtfs local,id=%s,path=%s,security_model=none,mount_tag=%s%s" \
                  $ID $4 $TAG $LOCRO
    fi
}

#set_qcow2_l2_cache() {
#    local L2_DEFAULT_CLUSTERS=8
#    local L2_DEFAULT_SIZE=1048576
#    # Format, virtual size, cluster size:
#    set $(${QEMU_IMG_BIN} info $1 |
#          egrep 'file format|virtual size|cluster_size' |
#          sed 's/(//;s/ bytes)//' |
#          awk '{print $NF}')
#    if [ "$1" = qcow2 ]
#    then
#        # size*cache_clusters/cluster_size, but divisible by cluster_size:
#        local L2SIZE=$(( $2 * $L2_DEFAULT_CLUSTERS / $3**2 * $3 ))
#        # Apply only if not smaller than the default
#        [ $L2SIZE -gt $L2_DEFAULT_SIZE ] && printf ",l2-cache-size=%s" $L2SIZE
#    fi
#}
set_qcow2_l2_cache() {
    local L2_DEFAULT_CLUSTERS=8
    local L2_DEFAULT_SIZE=1048576
    # Info: http://git.qemu.org/?p=qemu.git;a=blob;f=docs/specs/qcow2.txt
    set $(od -N 32 --endian=big -An -x $1 2> /dev/null | tr -d " ")
    local MAG_VER=$(cut -c 1-16 <<< $1)
    case $MAG_VER in
    514649fb0000000[2-3])
        # File is qcow2
        local CLUSTERBITS=$(cut -c 9-16 <<< $2)<commit_msg>Fix a changed link to QEMU docs<commit_after>#!/bin/bash

#------------------------------------------------------------------------------
# Copyright (c) 2016, <NAME>
# All rights reserved.
# This code is licensed under standard 3-clause BSD license.
# See file LICENSE supplied with this package for the full license text.
#------------------------------------------------------------------------------

SCRDIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
. "${SCRDIR}"/pVM.cfg

while [ $# -gt 0 ]
do
    key="$1"
    case $key in
        -d)
            DRIVES=(${2//,/ })
            shift
            ;;
        -i)
            INDRIVES=(${2//,/ })
            shift
            ;;
        -o)
            OUTDIRS=(${2//,/ })
            shift
            ;;
        -m)
            MEM=(${2//,/ })
            shift
            ;;
        -c)
            CORES=(${2//,/ })
            shift
            ;;
        -a)
            ADDITIONAL=(${2//,/ })
            shift
            ;;
        --install)
            INSTALLING=true
            ;;
        -h|--help)
            cat <<EOF
Usage: $0 [-d DRIVE] [-i SQSH|DIR] [-o DIR] [-m MEM] [-c CORES] [-a OPTS] \
[--install]
EOF
            exit 0
            ;;
    esac
    shift
done

case $NET_TYPE in
    tap)
        case $USER in
        root)
            echo "Running with tap network."
            ;;
        *)
            echo "Error: tap network can be run with root privileges only!"
            exit 1
        esac
        ;;
    user)
        echo "Running with user network."
        ;;
    *)
        echo "Error: NET_TYPE should be defined as \"tap\" or \"user\" only!"
        exit 1
esac

CURR_USER=${SUDO_USER:-${USER}}
SSH_LOCAL=$(awk '{ print $3 ":" $4 }' <<< ${SSH_CONNECTION})

numbers_dont_match() {
    echo "Check config arrays!"
    exit 1
}

getmac() {
    local MAC_I="56:3c:4c:"
    local MAC_F=":cc"
    local MAC_M=$(printf "%04d" $1 | sed 's/.\{2\}/&:/' | cut -c 1-5)
    printf "%s%s%s" ${MAC_I} ${MAC_M} ${MAC_F}
}

zerolead() {
    printf "%02d" $1
}

calcvec() {
    printf "$(( $1 * 2 + 2 ))"
}

name_from_drive() {
    local DRIVE=$(basename $1)
    printf "%s_%s" ${DRIVE%.*} $2
}

start_dnsmasq() {
    dnsmasq -i ${VM_BRIDGE} -z -F "${LEASE_RANGE}" -x "${SCRDIR}"/dnsmasq.pid
    chown ${CURR_USER}:$(id -g -n ${CURR_USER}) "${SCRDIR}"/dnsmasq.pid
}

stop_dnsmasq() {
    kill -9 $(cat "${SCRDIR}"/dnsmasq.pid)
    rm -r "${SCRDIR}"/dnsmasq.pid
}

create_bridge() {
    ovs-vsctl add-br ${VM_BRIDGE}
    ip link set ${VM_BRIDGE} up
    ip addr add ${BR_ADDR} dev ${VM_BRIDGE}
}

remove_bridge() {
    ip link set ${VM_BRIDGE} down
    ovs-vsctl del-br ${VM_BRIDGE}
}

create_bridge_scripts() {
cat > "${SCRDIR}"/upscript.sh <<EOF
#!/bin/sh
ip link set \$1 up
ovs-vsctl add-port ${VM_BRIDGE} \$1
EOF
chmod 755 "${SCRDIR}"/upscript.sh
chown ${CURR_USER}:$(id -g -n ${CURR_USER}) "${SCRDIR}"/upscript.sh
}

remove_bridge_scripts() {
    rm -f "${SCRDIR}"/upscript.sh
}

kill_machines() {
    for j in $@
    do
        kill -9 $j
    done
}

squashdrive_cmd() {
    if [ -f "$2" ]
    then
        printf -- "-drive file=%s,if=none,id=rdrive%s,readonly,format=raw
                   -device scsi-hd,drive=rdrive%s" "$2" "$1" "$1"
    fi
}

drivedir_cmd() {
    # Arguments: name number ro|rw /path/on/host
    if [ -d "$4" ]
    then
        local ID="$1dir$(zerolead $2)"
        local TAG="$1"
        case $3 in
        ro)
            local LOCRO=",readonly"
        esac
        printf -- "-virtfs local,id=%s,path=%s,security_model=none,mount_tag=%s%s" \
                  $ID $4 $TAG $LOCRO
    fi
}

#set_qcow2_l2_cache() {
#    local L2_DEFAULT_CLUSTERS=8
#    local L2_DEFAULT_SIZE=1048576
#    # Format, virtual size, cluster size:
#    set $(${QEMU_IMG_BIN} info $1 |
#          egrep 'file format|virtual size|cluster_size' |
#          sed 's/(//;s/ bytes)//' |
#          awk '{print $NF}')
#    if [ "$1" = qcow2 ]
#    then
#        # size*cache_clusters/cluster_size, but divisible by cluster_size:
#        local L2SIZE=$(( $2 * $L2_DEFAULT_CLUSTERS / $3**2 * $3 ))
#        # Apply only if not smaller than the default
#        [ $L2SIZE -gt $L2_DEFAULT_SIZE ] && printf ",l2-cache-size=%s" $L2SIZE
#    fi
#}
set_qcow2_l2_cache() {
    local L2_DEFAULT_CLUSTERS=8
    local L2_DEFAULT_SIZE=1048576
    # Info: http://git.qemu.org/?p=qemu.git;a=blob;f=docs/interop/qcow2.txt
    set $(od -N 32 --endian=big -An -x $1 2> /dev/null | tr -d " ")
    local MAG_VER=$(cut -c 1-16 <<< $1)
    case $MAG_VER in
    514649fb0000000[2-3])
        # File is qcow2
        local CLUSTERBITS=$(cut -c 9-16 <<< $2)
{'fraction_non_alphanumeric': 0.20203771948840235, 'fraction_numerical': 0.02644699761543464, 'mean_word_length': 1.7306895531222255, 'pattern_counts': {'":': 4, '<': 31, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3323339', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3441, 'n_words': 805}