3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 # Query a disk for partitions and display them
29 #############################
31 . ${PROGDIR}/backend/functions.sh
32 . ${PROGDIR}/backend/functions-disk.sh
36 echo "Error: No disk specified!"
40 if [ ! -e "/dev/${1}" ]
42 echo "Error: Disk /dev/${1} does not exist!"
48 # Now get the disks size in MB
49 KB="`diskinfo -v ${1} | grep 'bytes' | cut -d '#' -f 1 | tr -s '\t' ' ' | tr -d ' '`"
50 MB=$(convert_byte_to_megabyte ${KB})
52 TOTALB="`diskinfo -v ${1} | grep 'in sectors' | tr -s '\t' ' ' | cut -d ' ' -f 2`"
54 gpart show ${1} >/dev/null 2>/dev/null
55 if [ "$?" != "0" ] ; then
56 # No partitions on this disk, display entire disk size and exit
57 echo "${1}-freemb: ${TOTALSIZE}"
58 echo "${1}-freeblocks: ${TOTALB}"
62 # Display if this is GPT or MBR formatted
63 TYPE=`gpart show ${1} | awk '/^=>/ { printf("%s",$5); }'`
64 echo "${1}-format: $TYPE"
66 # Set some search flags
72 # Get a listing of partitions on this disk
73 get_disk_partitions "${DISK}"
78 # First get the sysid / label for this partition
79 if [ "$TYPE" = "MBR" ] ; then
80 get_partition_sysid_mbr "${DISK}" "${curpart}"
81 echo "${curpart}-sysid: ${VAL}"
82 get_partition_label_mbr "${DISK}" "${curpart}"
83 echo "${curpart}-label: ${VAL}"
85 get_partition_label_gpt "${DISK}" "${curpart}"
86 echo "${curpart}-sysid: ${VAL}"
87 echo "${curpart}-label: ${VAL}"
90 # Now get the startblock, blocksize and MB size of this partition
92 get_partition_startblock "${DISK}" "${curpart}"
94 echo "${curpart}-blockstart: ${START}"
96 get_partition_blocksize "${DISK}" "${curpart}"
98 echo "${curpart}-blocksize: ${SIZEB}"
100 SIZEMB=$(convert_blocks_to_megabyte ${SIZEB})
101 echo "${curpart}-sizemb: ${SIZEMB}"
106 # Now calculate any free space
107 LASTB="`expr $SIZEB + $START`"
108 FREEB="`expr $TOTALB - $LASTB`"
109 FREEMB="`expr ${FREEB} / 2048`"
110 echo "${1}-freemb: $FREEMB"
111 echo "${1}-freeblocks: $FREEB"