1# vim: filetype=sh 2 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2024 Axcient 6# All rights reserved. 7# 8# Redistribution and use in source and binary forms, with or without 9# modification, are permitted provided that the following conditions 10# are met: 11# 1. Redistributions of source code must retain the above copyright 12# notice, this list of conditions and the following disclaimer. 13# 2. Redistributions in binary form must reproduce the above copyright 14# notice, this list of conditions and the following disclaimer in the 15# documentation and/or other materials provided with the distribution. 16# 17# THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28find_device() { 29 LUN=$1 30 31 # Rescan camsim 32 # XXX camsim doesn't update when creating a new device. Worse, a 33 # rescan won't look for new devices. So we must disable/re-enable it. 34 # Worse still, enabling it isn't synchronous, so we need a retry loop 35 # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281000 36 retries=5 37 ctladm port -o off -p 0 >/dev/null 38 ctladm port -o on -p 0 >/dev/null 39 HEXLUN=`printf %x $LUN` 40 while true; do 41 dev=`camcontrol devlist | awk -v lun=$HEXLUN ' 42 /FREEBSD CTL.*,pass/ && $9==lun { 43 split($10, fields, /[,]/); print fields[1]; 44 } 45 /FREEBSD CTL.*\(pass/ && $9==lun { 46 split($10, fields, /[,]/); print fields[2]; 47 } 48 ' | sed 's:[()]::'` 49 if [ -z "$dev" -o ! -c /dev/$dev ]; then 50 retries=$(( $retries - 1 )) 51 if [ $retries -eq 0 ]; then 52 cat lun-create.txt 53 camcontrol devlist 54 atf_fail "Could not find GEOM device" 55 fi 56 sleep 0.1 57 continue 58 fi 59 break 60 done 61 # Ensure that it's actually ready. camcontrol may report the disk's 62 # ident before it's actually ready to receive commands. Maybe that's 63 # because all of the GEOM providers must probe it? 64 while true; do 65 dd if=/dev/$dev bs=4096 count=1 of=/dev/null >/dev/null 2>/dev/null && break 66 retries=$(( $retries - 1 )) 67 if [ $retries -eq 0 ]; then 68 atf_fail "Device never became ready" 69 fi 70 sleep 0.1 71 done 72} 73 74# Create a CTL LUN backed by a file 75create_block() { 76 EXTRA_ARGS=$* 77 78 atf_check -o save:lun-create.txt ctladm create -b block $EXTRA_ARGS 79 atf_check egrep -q "LUN created successfully" lun-create.txt 80 LUN=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 81 if [ -z "$LUN" ]; then 82 atf_fail "Could not find LUN id" 83 fi 84 find_device $LUN 85} 86 87# Create a CTL LUN backed by RAM 88create_ramdisk() { 89 EXTRA_ARGS=$* 90 91 atf_check -o save:lun-create.txt ctladm create -b ramdisk -s 1048576 $EXTRA_ARGS 92 atf_check egrep -q "LUN created successfully" lun-create.txt 93 LUN=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 94 if [ -z "$LUN" ]; then 95 atf_fail "Could not find LUN id" 96 fi 97 find_device $LUN 98} 99 100cleanup() { 101 if [ -e "lun-create.txt" ]; then 102 backend=`awk '/backend:/ {print $NF}' lun-create.txt` 103 lun_id=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 104 ctladm remove -b $backend -l $lun_id > /dev/null 105 fi 106} 107