This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCarveEBS.sh
executable file
·187 lines (171 loc) · 5.11 KB
/
CarveEBS.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
#
# Configure attached EBS into target partitioning-state. Script may
# be used to carve disk into a simple "boot-n-root", 2-slice disk
# or into a "boot+LVM2" 2-slice disk. LVM2 configuration-options are
# currently limited.
#
# Usage:
# CarveEBS -b <bootlabel> -r <rootlabel>|-v <vgname> -d <devnode>
# -b|--bootlabel:
# FS-label applied to '/boot' filesystem
# -d|--disk:
# dev-path to disk to be partitioned
# -r|--rootlabel:
# FS-label to apply to '/' filesystem (no LVM in use)
# -v|--vgname:
# LVM2 Volume-Group name for root volumes
#
# Note: the "-r" and "-v" options are mutually exclusive
#
####################################################################
PROGNAME=$(basename "$0")
BOOTDEVSZ="500m"
# Check for arguments
if [[ $# -lt 1 ]]
then
(
printf "Missing parameter(s). Valid flags/parameters are:\n"
printf "\t-b|--bootlabel: FS-label applied to '/boot' filesystem\n"
printf "\t-d|--disk: dev-path to disk to be partitioned\n"
printf "\t-r|--rootlabel: FS-label to apply to '/' filesystem (no LVM in use)\n"
printf "\t-v|--vgname: LVM2 Volume-Group name for root volumes\n"
printf "Aborting...\n"
) > /dev/stderr
exit 1
fi
function LogBrk() {
echo $2 > /dev/stderr
exit $1
}
# Partition as LVM
function CarveLVM() {
local ROOTVOL=(rootVol 4g)
local SWAPVOL=(swapVol 2g)
local HOMEVOL=(homeVol 1g)
local VARVOL=(varVol 2g)
local LOGVOL=(logVol 2g)
local AUDVOL=(auditVol 100%FREE)
# Clear the MBR and partition table
dd if=/dev/zero of=${CHROOTDEV} bs=512 count=1000 > /dev/null 2>&1
# Lay down the base partitions
parted -s ${CHROOTDEV} -- mklabel msdos mkpart primary ext4 2048s ${BOOTDEVSZ} \
mkpart primary ext4 ${BOOTDEVSZ} 100% set 2 lvm
# Create LVM objects
vgcreate -y ${VGNAME} ${CHROOTDEV}2 || LogBrk 5 "VG creation failed. Aborting!"
lvcreate --yes -W y -L ${ROOTVOL[1]} -n ${ROOTVOL[0]} ${VGNAME} || LVCSTAT=1
lvcreate --yes -W y -L ${SWAPVOL[1]} -n ${SWAPVOL[0]} ${VGNAME} || LVCSTAT=1
lvcreate --yes -W y -L ${HOMEVOL[1]} -n ${HOMEVOL[0]} ${VGNAME} || LVCSTAT=1
lvcreate --yes -W y -L ${VARVOL[1]} -n ${VARVOL[0]} ${VGNAME} || LVCSTAT=1
lvcreate --yes -W y -L ${LOGVOL[1]} -n ${LOGVOL[0]} ${VGNAME} || LVCSTAT=1
lvcreate --yes -W y -l ${AUDVOL[1]} -n ${AUDVOL[0]} ${VGNAME} || LVCSTAT=1
# Create filesystems
mkfs -t ext4 -L "${BOOTLABEL}" ${CHROOTDEV}1
mkfs -t ext4 /dev/${VGNAME}/${ROOTVOL[0]}
mkfs -t ext4 /dev/${VGNAME}/${HOMEVOL[0]}
mkfs -t ext4 /dev/${VGNAME}/${VARVOL[0]}
mkfs -t ext4 /dev/${VGNAME}/${LOGVOL[0]}
mkfs -t ext4 /dev/${VGNAME}/${AUDVOL[0]}
mkswap /dev/${VGNAME}/${SWAPVOL[0]}
}
# Partition with no LVM
function CarveBare() {
# Clear the MBR and partition table
dd if=/dev/zero of=${CHROOTDEV} bs=512 count=1000 > /dev/null 2>&1
# Lay down the base partitions
parted -s ${CHROOTDEV} -- mklabel msdos mkpart primary ext4 2048s ${BOOTDEVSZ} \
mkpart primary ext4 ${BOOTDEVSZ} 100%
# Create FS on partitions
mkfs -t ext4 -L "${BOOTLABEL}" ${CHROOTDEV}1
mkfs -t ext4 -L "${ROOTLABEL}" ${CHROOTDEV}2
}
######################
## Main program-flow
######################
OPTIONBUFR=`getopt -o b:d:r:v: --long bootlabel:,disk:,rootlabel:,vgname: -n ${PROGNAME} -- "$@"`
eval set -- "${OPTIONBUFR}"
###################################
# Parse contents of ${OPTIONBUFR}
###################################
while [ true ]
do
case "$1" in
-b|--bootlabel)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
*)
BOOTLABEL=${2}
shift 2;
;;
esac
;;
-d|--disk)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
*)
CHROOTDEV=${2}
shift 2;
;;
esac
;;
-r|--rootlabel)
case "$2" in
"")
LogBrk 1"Error: option required but not specified"
shift 2;
exit 1
;;
*)
ROOTLABEL=${2}
shift 2;
;;
esac
;;
-v|--vgname)
case "$2" in
"")
LogBrk 1 "Error: option required but not specified"
shift 2;
exit 1
;;
*)
VGNAME=${2}
shift 2;
;;
esac
;;
--)
shift
break
;;
*)
LogBrk 1 "Internal error!"
exit 1
;;
esac
done
# Abort if no bootlabel was set
if [[ -z ${BOOTLABEL+xxx} ]]
then
LogBrk 1 "Cannot continue without 'bootlabel' being specified. Aborting..."
# Abort if mutually-exclusive options were declared
elif [[ ! -z ${ROOTLABEL+xxx} ]] && [[ ! -z ${VGNAME+xxx} ]]
then
LogBrk 1 "The 'rootlabel' and 'vgname' arguments are mutually exclusive. Exiting."
# Partion for LVM
elif [[ -z ${ROOTLABEL+xxx} ]] && [[ ! -z ${VGNAME+xxx} ]]
then
CarveLVM
# Partition for bare slices
elif [[ ! -z ${ROOTLABEL+xxx} ]] && [[ -z ${VGNAME+xxx} ]]
then
CarveBare
fi