1
|
#!/bin/sh
|
2
|
#-
|
3
|
# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
|
4
|
#
|
5
|
# Redistribution and use in source and binary forms, with or without
|
6
|
# modification, are permitted provided that the following conditions
|
7
|
# are met:
|
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.
|
13
|
#
|
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
|
24
|
# SUCH DAMAGE.
|
25
|
#
|
26
|
# $FreeBSD: src/usr.sbin/pc-sysinstall/backend-query/disk-part.sh,v 1.2 2010/06/27 16:46:11 imp Exp $
|
27
|
|
28
|
# Query a disk for partitions and display them
|
29
|
#############################
|
30
|
|
31
|
. ${PROGDIR}/backend/functions.sh
|
32
|
. ${PROGDIR}/backend/functions-disk.sh
|
33
|
|
34
|
if [ -z "${1}" ]
|
35
|
then
|
36
|
echo "Error: No disk specified!"
|
37
|
exit 1
|
38
|
fi
|
39
|
|
40
|
if [ ! -e "/dev/${1}" ]
|
41
|
then
|
42
|
echo "Error: Disk /dev/${1} does not exist!"
|
43
|
exit 1
|
44
|
fi
|
45
|
|
46
|
DISK="${1}"
|
47
|
|
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})
|
51
|
TOTALSIZE="$MB"
|
52
|
TOTALB="`diskinfo -v ${1} | grep 'in sectors' | tr -s '\t' ' ' | cut -d ' ' -f 2`"
|
53
|
|
54
|
|
55
|
|
56
|
gpart show ${1} >/dev/null 2>/dev/null
|
57
|
if [ "$?" != "0" ] ; then
|
58
|
# No partitions on this disk, display entire disk size and exit
|
59
|
echo "${1}-freemb: ${TOTALSIZE}"
|
60
|
echo "${1}-freeblocks: ${TOTALB}"
|
61
|
exit
|
62
|
fi
|
63
|
|
64
|
# Display if this is GPT or MBR formatted
|
65
|
gpart show ${1} | grep "GPT" >/dev/null 2>/dev/null
|
66
|
if [ "$?" = "0" ] ; then
|
67
|
echo "${1}-format: GPT"
|
68
|
TYPE="GPT"
|
69
|
else
|
70
|
echo "${1}-format: MBR"
|
71
|
TYPE="MBR"
|
72
|
fi
|
73
|
|
74
|
# Set some search flags
|
75
|
PART="0"
|
76
|
EXTENDED="0"
|
77
|
START="0"
|
78
|
SIZEB="0"
|
79
|
|
80
|
# Get a listing of partitions on this disk
|
81
|
get_disk_partitions "${DISK}"
|
82
|
PARTS="${VAL}"
|
83
|
for curpart in $PARTS
|
84
|
do
|
85
|
|
86
|
# First get the sysid / label for this partition
|
87
|
if [ "$TYPE" = "MBR" ] ; then
|
88
|
get_partition_sysid_mbr "${DISK}" "${curpart}"
|
89
|
echo "${curpart}-sysid: ${VAL}"
|
90
|
get_partition_label_mbr "${DISK}" "${curpart}"
|
91
|
echo "${curpart}-label: ${VAL}"
|
92
|
else
|
93
|
get_partition_label_gpt "${DISK}" "${curpart}"
|
94
|
echo "${curpart}-sysid: ${VAL}"
|
95
|
echo "${curpart}-label: ${VAL}"
|
96
|
fi
|
97
|
|
98
|
# Now get the startblock, blocksize and MB size of this partition
|
99
|
|
100
|
get_partition_startblock "${DISK}" "${curpart}"
|
101
|
START="${VAL}"
|
102
|
echo "${curpart}-blockstart: ${START}"
|
103
|
|
104
|
get_partition_blocksize "${DISK}" "${curpart}"
|
105
|
SIZEB="${VAL}"
|
106
|
echo "${curpart}-blocksize: ${SIZEB}"
|
107
|
|
108
|
SIZEMB=$(convert_blocks_to_megabyte ${SIZEB})
|
109
|
echo "${curpart}-sizemb: ${SIZEMB}"
|
110
|
|
111
|
done
|
112
|
|
113
|
|
114
|
# Now calculate any free space
|
115
|
LASTB="`expr $SIZEB + $START`"
|
116
|
FREEB="`expr $TOTALB - $LASTB`"
|
117
|
FREEMB="`expr ${FREEB} / 2048`"
|
118
|
echo "${1}-freemb: $FREEMB"
|
119
|
echo "${1}-freeblocks: $FREEB"
|