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 28load_modules() { 29 if ! kldstat -q -m ctl; then 30 kldload ctl || atf_skip "could not load ctl kernel mod" 31 fi 32 if ! ctladm port -o on -p 0; then 33 atf_skip "could not enable the camsim frontend" 34 fi 35} 36 37find_device() { 38 LUN=$1 39 40 # Rescan camsim 41 # XXX camsim doesn't update when creating a new device. Worse, a 42 # rescan won't look for new devices. So we must disable/re-enable it. 43 # Worse still, enabling it isn't synchronous, so we need a retry loop 44 # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281000 45 retries=5 46 ctladm port -o off -p 0 >/dev/null 47 ctladm port -o on -p 0 >/dev/null 48 HEXLUN=`printf %x $LUN` 49 while true; do 50 dev=`camcontrol devlist | awk -v lun=$HEXLUN ' 51 /FREEBSD CTL.*,pass/ && $9==lun { 52 split($10, fields, /[,]/); print fields[1]; 53 } 54 /FREEBSD CTL.*\(pass/ && $9==lun { 55 split($10, fields, /[,]/); print fields[2]; 56 } 57 ' | sed 's:[()]::'` 58 if [ -z "$dev" -o ! -c /dev/$dev ]; then 59 retries=$(( $retries - 1 )) 60 if [ $retries -eq 0 ]; then 61 cat lun-create.txt 62 camcontrol devlist 63 atf_fail "Could not find GEOM device" 64 fi 65 sleep 0.1 66 continue 67 fi 68 break 69 done 70 # Ensure that it's actually ready. camcontrol may report the disk's 71 # ident before it's actually ready to receive commands. Maybe that's 72 # because all of the GEOM providers must probe it? 73 while true; do 74 dd if=/dev/$dev bs=4096 count=1 of=/dev/null >/dev/null 2>/dev/null && break 75 retries=$(( $retries - 1 )) 76 if [ $retries -eq 0 ]; then 77 atf_fail "Device never became ready" 78 fi 79 sleep 0.1 80 done 81} 82 83# Create a CTL LUN 84create_ramdisk() { 85 EXTRA_ARGS=$* 86 87 atf_check -o save:lun-create.txt ctladm create -b ramdisk -s 1048576 $EXTRA_ARGS 88 atf_check egrep -q "LUN created successfully" lun-create.txt 89 LUN=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 90 if [ -z "$LUN" ]; then 91 atf_fail "Could not find LUN id" 92 fi 93 find_device $LUN 94} 95 96cleanup() { 97 if [ -e "lun-create.txt" ]; then 98 lun_id=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 99 ctladm remove -b ramdisk -l $lun_id > /dev/null 100 fi 101} 102