@@ -42,3 +42,63 @@ PUMP() {
42
42
43
43
umount_image " ${loop} "
44
44
}
45
+
46
+ # ADDPART adds a partition of a given size, partition type and file system
47
+ #
48
+ # Usage: ADDPART SIZE PTYPE FS
49
+ ADDPART () {
50
+ if [ $# -ne 3 ]; then
51
+ echo -e " \033[0;31m### Error: usage ADDPART SIZE PTYPE FS\033[0m"
52
+ return 1
53
+ fi
54
+
55
+ echo -e " \033[0;32m### ADDPART ${1} ${2} ${3} \033[0m"
56
+
57
+ if [[ -b " ${DEST_IMG} " ]]; then
58
+ echo -e " \033[0;31m### Error: Block device ${DEST_IMG} cannot be altered.\033[0m"
59
+ return 1
60
+ fi
61
+
62
+ BS=" 1M"
63
+
64
+ # units does not print to stderr, thus test call before using output
65
+ echo -n " addpart conversion to ${BS} * "
66
+ units -t " ${1} B" " ${BS} B"
67
+
68
+ COUNT=$( units -t ${1} B ${BS} B)
69
+
70
+ # Ceil the number if a decimal is given.
71
+ if [[ " ${COUNT} " == * .* ]]; then
72
+ COUNT=$(( $(echo "${COUNT} " | cut - d. - f1 ) + 1 ))
73
+ fi
74
+
75
+ echo " addpart ceil: ${BS} * ${COUNT} "
76
+
77
+ # Error on unset PTYPE
78
+ if [[ -z ${2+x} ]]; then
79
+ echo -e " \033[0;31m### Error: Partition type unspecified, possible options:\033[0m"
80
+ sfdisk -T
81
+ return 1
82
+ fi
83
+
84
+ echo " checking mkfs.${3} "
85
+
86
+ if ! command -v mkfs.${3} ; then
87
+ echo -e " \033[0;31m### Error: file system ${3} is not available.\033[0m"
88
+ return 1
89
+ fi
90
+
91
+ dd if=/dev/zero bs=" ${BS} " count=" ${COUNT} " >> " ${DEST_IMG} "
92
+
93
+ local data_part_start
94
+ data_part_start=$(( $(sfdisk - l "${DEST_IMG} " - o END - n | tail - n1 ) + 1 ))
95
+
96
+ echo " $data_part_start ,+,${2} " | sfdisk -a " ${DEST_IMG} "
97
+
98
+ local loop
99
+ loop=$( mount_image " ${DEST_IMG} " )
100
+
101
+ mkfs.${3} " /dev/mapper/${loop} p$(( IMG_ROOT + 1 )) "
102
+
103
+ umount_image " ${loop} "
104
+ }
0 commit comments