-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit_image.sh
More file actions
executable file
·196 lines (160 loc) · 3.93 KB
/
init_image.sh
File metadata and controls
executable file
·196 lines (160 loc) · 3.93 KB
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
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
# Initialize an .img file with a single partition and formated with a
# filesystem.
# Defaults #
# Filesystem to use. At this time, only ext4 is supported
FS_TYPE="ext4"
# Image Size, in Megabytes
IMG_SIZE=20480 # 20GB
# bytestream to use for initial creation. Defaults to all zeros
FILL_SRC=/dev/zero
#moved to main loop, this needs ROOT
#LOOP_DEV=$(losetup -f)
LOOP_DEV="INVALID"
PART_N=1
MOUNT_POINT="$(mktemp -d)"
ROOT_METHOD="sudo"
# /Defaults #
help_and_exit() {
cat 1>&2 << EOF
init_image.sh:
Create an disk .img file, with a single parition and a file system. Creates a
file of an arbitrary size, formats it with the ext4 filesystem. Takes one
parameter, filename.
Default file size is 20GB.
USAGE:
init_image [-s <size>] <filename.img>
OPTIONS:
-s,--size Size of image, in Megabytes, defaults to 20GB
EOF
exit 4
}
message() {
echo "init_image.sh: ${@}"
}
submsg() {
echo "==> ${@}"
}
exit_with_error() {
echo 1>&2 "init_image.sh: ERROR: ${2}"
exit ${1}
}
warn() {
echo 1>&2 "init_image.sh: WARN: ${@}"
}
as_root() {
# execute a command as root.
case $ROOT_METHOD in
sudo)
sudo ${@}
;;
pkexec)
pkexec ${@}
;;
uid)
${@}
;;
esac
}
switch_checker() {
while [ ! -z "$1" ];do
case "$1" in
--help|-\?)
help_and_exit
;;
-s|--size)
IMG_SIZE="${2}"
shift
;;
*)
PARMS+="${1}"
;;
esac
shift
done
}
abort_cleanup() {
# If this fails or is aborted, cleanup before we exit
rm "${OUTFILE}"
as_root losetup -d ${LOOP_DEV} &> /dev/null
rmdir "${MOUNT_POINT}"
exit_with_error 2 "INTERRUPT: ABORT!"
}
_setup-loop() {
local -i local_exit=0
submsg "Setting up loop: ${OUT_FILE} on ${LOOP_DEV}"
as_root losetup -P ${LOOP_DEV} "${OUT_FILE}" || local_exit+=1
return ${local_exit}
}
_destroy-loop() {
local -i local_exit=0
submsg "Destroying ${LOOP_DEV}"
as_root losetup -d ${LOOP_DEV} &> /dev/null || local_exit+=1
return ${local_exit}
}
_create_blank_file() {
# Generate a blank file of arbitrary size
local -i local_exit=0
local blocksize="1024k" # 1 Megabyte
submsg "Generating Blank file ${IMG_SIZE}M long"
dd if="${FILL_SRC}" of="${OUT_FILE}" bs=${blocksize} count=${IMG_SIZE} status=progress || local_exit+=1
sync
return ${local_exit}
}
_partition() {
# Create partition
local -i local_exit=0
#local label="msdos"
#local -i offset=2048 #first 2048 sectors
submsg "Partitioning image"
# parted
#as_root parted --script ${LOOP_DEV} mklabel ${label} || local_exit+=1
#as_root parted --script ${LOOP_DEV} mkpart primary ${FS_TYPE} ${offset}S -- -1 || local_exit+=1
# sfdisk
as_root sfdisk ${LOOP_DEV} > /dev/null << EOF
;
EOF
[ $? -ne 0 ] && local_exit+=1
return ${local_exit}
}
_format() {
# format with filesystem
local -i local_exit=0
submsg "Foramting with ${FS_TYPE}"
as_root mkfs -t ${FS_TYPE} ${LOOP_DEV}p${PART_N} &> /dev/null|| local_exit+=1
return ${local_exit}
}
main() {
OUT_FILE="${@}"
local -i errors=0
[ -z ${OUT_FILE} ] && help_and_exit
trap "abort_cleanup" 1 2 3 9 15
message "Making ${IMG_SIZE}M image file ${OUT_FILE}"
as_root true # get root
LOOP_DEV=$(as_root losetup -f)
_create_blank_file || exit_with_error 1 "Could Not Generate Blank File, Exiting"
_setup-loop || exit_with_error 1 "Could Not Set Up Loop Device, Exiting"
_partition || exit_with_error 1 "Could Not Generate Paritions in ${OUT_FILE}, Exiting"
_format || exit_with_error 1 "Formatting Failed, Exiting"
_destroy-loop
if [ ${?} -ne 0 ];then
"Couldn't remove loop device. Clean up mantually"
errors+=1
fi
# Clean up last random mount point
rmdir "${MOUNT_POINT}"
if [ ${?} -ne 0 ];then
warn "couldn't delete ${MOUNT_POINT}, clean up manually"
errors+=1
fi
if [ $errors -gt 0 ];then
message "Done, but with ${errors} errors"
exit 1
else
message "Done"
exit 0
fi
}
PARMS=""
switch_checker "${@}"
main "${PARMS}"