-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_filesystem
executable file
·150 lines (140 loc) · 2.67 KB
/
create_filesystem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
#
# Copyright (C) 2022 David Valin [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
device_to_use=""
mount_pnt=""
fs_type=""
valid_fs_type="xfs ext4 ext3 gfs2 gfs"
usage()
{
echo "Usage: $1"
echo " --fs_type: filesystem to create. ${valid_fs_type}"
echo " --mount_dir: directory to mount to"
echo " --device: device to use."
exit 1
}
#
# Verify parameters passed in.
#
validate_data()
{
if [[ $device_to_use = "" ]]; then
echo You need to designate a device to use.
exit 1
fi
if [[ ! -e $device_to_use ]]; then
echo Designated device, $device_to_use, does not exist
exit 1
fi
if [[ mount_pnt = "" ]]; then
echo You need to designate a mount point.
exit 1
fi
if [[ fs_type = "" ]]; then
echo You need to designate a filesys type.
exit 1
fi
}
#
# Define options
#
ARGUMENT_LIST=(
"device"
"fs_type"
"mount_dir"
)
NO_ARGUMENTS=(
"usage"
)
# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[@]}")" \
--longoptions "$(printf "%s," "${NO_ARGUMENTS[@]}")" \
--name "$(basename "$0")" \
--options "h" \
-- "$@"
)
if [ $? -ne 0 ]; then
exit
fi
eval set --$opts
while [[ $# -gt 0 ]]; do
case "$1" in
--device)
device_to_use=${2}
shift 2
;;
--fs_type)
fs_type=${2}
shift 2
;;
--mount_dir)
mount_pnt=${2}
mkdir -p $mount_pnt
shift 2
;;
--usage)
usage $0
;;
-h)
usage $0
;;
--)
break;
;;
*)
echo option not found $1
usage $0
;;
esac
done
validate_data
#
# Create the file system.
#
case $fs_type in
ext3)
MKFS_OPS="-F"
;;
ext4|ext4dev)
MKFS_OPS="-F"
;;
gfs)
MKFS_OPS="-O -p lock_nolock -j 1"
;;
gfs2)
MKFS_OPS="-O -p lock_nolock -j 1"
;;
xfs)
MKFS_OPS="-f"
;;
*)
echo Error, $1 unknown filesystem type
exit 1
;;
esac
echo "mkfs -t ${fs_type} ${MKFS_OPS} ${device_to_use}"
mkfs -t ${fs_type} ${MKFS_OPS} ${device_to_use}
if [ $? -ne 0 ]; then
exit 1
fi
mount ${device_to_use} ${mount_pnt}
if [ $? -ne 0 ]; then
exit 1
fi
exit 0