1209513Simp#!/bin/sh
2209513Simp#-
3209552Simp# Copyright (c) 2010 iXsystems, Inc.  All rights reserved.
4209513Simp#
5209513Simp# Redistribution and use in source and binary forms, with or without
6209513Simp# modification, are permitted provided that the following conditions
7209513Simp# are met:
8209513Simp# 1. Redistributions of source code must retain the above copyright
9209513Simp#    notice, this list of conditions and the following disclaimer.
10209513Simp# 2. Redistributions in binary form must reproduce the above copyright
11209513Simp#    notice, this list of conditions and the following disclaimer in the
12209513Simp#    documentation and/or other materials provided with the distribution.
13209513Simp#
14209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17209513Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24209513Simp# SUCH DAMAGE.
25209513Simp#
26209513Simp# $FreeBSD$
27209513Simp
28209513Simp# Query a disk for partitions and display them
29209513Simp#############################
30209513Simp
31209513Simp. ${PROGDIR}/backend/functions.sh
32209513Simp. ${PROGDIR}/backend/functions-disk.sh
33209513Simp
34209513Simpif [ -z "${1}" ]
35209513Simpthen
36209513Simp  echo "Error: No disk specified!"
37209513Simp  exit 1
38209513Simpfi
39209513Simp
40209513Simpif [ ! -e "/dev/${1}" ]
41209513Simpthen
42209513Simp  echo "Error: Disk /dev/${1} does not exist!"
43209513Simp  exit 1
44209513Simpfi
45209513Simp
46209513SimpDISK="${1}"
47209513Simp
48209513Simp# Now get the disks size in MB
49209513SimpKB="`diskinfo -v ${1} | grep 'bytes' | cut -d '#' -f 1 | tr -s '\t' ' ' | tr -d ' '`"
50209513SimpMB=$(convert_byte_to_megabyte ${KB})
51209513SimpTOTALSIZE="$MB"
52209513SimpTOTALB="`diskinfo -v ${1} | grep 'in sectors' | tr -s '\t' ' ' | cut -d ' ' -f 2`"
53209513Simp
54209513Simpgpart show ${1} >/dev/null 2>/dev/null
55209513Simpif [ "$?" != "0" ] ; then
56209513Simp  # No partitions on this disk, display entire disk size and exit
57209513Simp  echo "${1}-freemb: ${TOTALSIZE}"
58209513Simp  echo "${1}-freeblocks: ${TOTALB}"
59209513Simp  exit
60209513Simpfi
61209513Simp
62209513Simp# Display if this is GPT or MBR formatted
63217164SjpaetzelTYPE=`gpart show ${1} | awk '/^=>/ { printf("%s",$5); }'`
64217164Sjpaetzelecho "${1}-format: $TYPE"
65209513Simp
66209513Simp# Set some search flags
67209513SimpPART="0"
68209513SimpEXTENDED="0"
69209513SimpSTART="0"
70209513SimpSIZEB="0"
71209513Simp
72209513Simp# Get a listing of partitions on this disk
73209513Simpget_disk_partitions "${DISK}"
74209513SimpPARTS="${VAL}"
75209513Simpfor curpart in $PARTS
76209513Simpdo
77209513Simp
78209513Simp  # First get the sysid / label for this partition
79209513Simp  if [ "$TYPE" = "MBR" ] ; then
80211730Simp    get_partition_sysid_mbr "${DISK}" "${curpart}"
81211730Simp    echo "${curpart}-sysid: ${VAL}"
82211730Simp    get_partition_label_mbr "${DISK}" "${curpart}"
83211730Simp    echo "${curpart}-label: ${VAL}"
84209513Simp  else
85211730Simp    get_partition_label_gpt "${DISK}" "${curpart}"
86211730Simp    echo "${curpart}-sysid: ${VAL}"
87211730Simp    echo "${curpart}-label: ${VAL}"
88209513Simp  fi
89209513Simp
90209513Simp  # Now get the startblock, blocksize and MB size of this partition
91209513Simp
92209513Simp  get_partition_startblock "${DISK}" "${curpart}"
93209513Simp  START="${VAL}"
94209513Simp  echo "${curpart}-blockstart: ${START}"
95209513Simp
96209513Simp  get_partition_blocksize "${DISK}" "${curpart}"
97209513Simp  SIZEB="${VAL}"
98209513Simp  echo "${curpart}-blocksize: ${SIZEB}"
99209513Simp
100209513Simp  SIZEMB=$(convert_blocks_to_megabyte ${SIZEB})
101209513Simp  echo "${curpart}-sizemb: ${SIZEMB}"
102209513Simp
103209513Simpdone
104209513Simp
105209513Simp
106209513Simp# Now calculate any free space
107209513SimpLASTB="`expr $SIZEB + $START`"
108209513SimpFREEB="`expr $TOTALB - $LASTB`"
109209513SimpFREEMB="`expr ${FREEB} / 2048`"
110209513Simpecho "${1}-freemb: $FREEMB"
111209513Simpecho "${1}-freeblocks: $FREEB"
112