aboutsummaryrefslogtreecommitdiff
path: root/custom/generate-unique-machine-values.sh
blob: 2eece98bf4baef3104779c0db93fe55255abd955 (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
#     ____             __             ____  ______  __
#    / __ \____  _____/ /_____  _____/ __ \/ ___/ |/ /
#   / / / / __ \/ ___/ //_/ _ \/ ___/ / / /\__ \|   /
#  / /_/ / /_/ / /__/ ,< /  __/ /  / /_/ /___/ /   |
# /_____/\____/\___/_/|_|\___/_/   \____//____/_/|_| SERIALIZER
#
# Repo:             https://github.com/sickcodes/Docker-OSX/
# Title:            Mac on Docker (Docker-OSX)
# Author:           Sick.Codes https://sick.codes/
# Version:          3.1
# License:          GPLv3+

help_text="Usage: generate-unique-machine-values.sh

General options:
    --count, -n, -c <count>         Number of serials to generate
    --model, -m <model>             Device model, e.g. 'iMacPro1,1'
    --csv <filename>                Optionally change the CSV output filename.
    --tsv <filename>                Optionally change the TSV output filename.
    --output-bootdisk <filename>    Optionally change the bootdisk qcow output filename. Useless when count > 1.
    --output-env <filename>         Optionally change the bootdisk env filename. Useless when count > 1.
    --output-dir <directory>        Optionally change the script output location.
    --width <string>                Resolution x axis length in pixels (default 1920)
    --height <string>               Resolution y axis length in pixels (default 1080

    --master-plist-url <url>        Specify an alternative master plist, via URL.
    --master-plist | --custom-plist <filename>
                                    Optionally change the input plist. Placeholders:
                                        {{DEVICE_MODEL}}, {{SERIAL}}, {{BOARD_SERIAL}},
                                        {{UUID}}, {{ROM}}, {{WIDTH}}, {{HEIGHT}}

    --help, -h, help                Display this help and exit
    --plists                        Create corresponding config.plists for each serial set.
    --bootdisks                     [SLOW] Create corresponding boot disk images for each serial set.

Example:
    ./generate-unique-machine-values.sh --count 1 --model='iMacPro1,1' --plists --bootdisks

        The above example will generate a
            - serial
            - board serial
            - uuid
            - MAC address
            - ROM value based on lowercase MAC address
            - Boot disk qcow image.
            - config.plist

Notes:
    - Default is 1 serial for 'iMacPro1,1' in the current working directory.
    - Default output is CSV, whereas setting the TSV option will output as tab-separated.
    - CSV is double quoted.
    - If you do not set a CSV filename, the output will be sent to the output-dir.
    - If you do not set an output-dir, the current directory will be the output directory.
    - Sourcable environment variable shell files will be written to a folder, 'envs'.
    - config.plist files will be written to a folder, 'plists'.

Author:  Sick.Codes https://sick.codes/
Project: https://github.com/sickcodes/Docker-OSX/
"

MACINFOPKG_VERSION=2.1.2

# gather arguments
while (( "$#" )); do
    case "${1}"  in

    --help | -h | h | help ) 
                echo "${help_text}" && exit 0
            ;;

    --count=* | -c=* | -n=* )
                export SERIAL_SET_COUNT="${1#*=}"
                shift
            ;;
    --count* | -c* | -n* )
                export SERIAL_SET_COUNT="${2}"
                shift
                shift
            ;;

    --csv=* )
                export CSV_OUTPUT_FILENAME="${1#*=}"
                shift
            ;;
    --csv* )
                export CSV_OUTPUT_FILENAME="${2}"
                shift
                shift
            ;;

    --tsv=* )
                export TSV_OUTPUT_FILENAME="${1#*=}"
                shift
            ;;
    --tsv* )
                export TSV_OUTPUT_FILENAME="${2}"
                shift
                shift
            ;;

    --output-dir=* )
                export OUTPUT_DIRECTORY="${1#*=}"
                shift
            ;;
    --output-dir* )
                export OUTPUT_DIRECTORY="${2}"
                shift
                shift
            ;;

    --output-bootdisk=* )
                export OUTPUT_QCOW="${1#*=}"
                shift
            ;;
    --output-bootdisk* )
                export OUTPUT_QCOW="${2}"
                shift
                shift
            ;;

    --output-env=* )
                export OUTPUT_ENV="${1#*=}"
                shift
            ;;
    --output-env* )
                export OUTPUT_ENV="${2}"
                shift
                shift
            ;;

    --model=* | -m=* )
                export DEVICE_MODEL="${1#*=}"
                shift
            ;;
    --model* | -m* ) 
                export DEVICE_MODEL="${2}"
                shift
                shift
            ;;

    --width=* )
                export WIDTH="${1#*=}"
                shift
            ;;

    --width* )
                export WIDTH="${2}"
                shift
                shift
            ;;

    --height=* )
                export HEIGHT="${1#*=}"
                shift
            ;;
    --height* )
                export HEIGHT="${2}"
                shift
                shift
            ;;

    --master-plist-url=* )
                export MASTER_PLIST_URL="${1#*=}"
                shift
            ;;
            
    --master-plist-url* )
                export MASTER_PLIST_URL="${2}"
                shift
                shift
            ;;

    --master-plist=* )
                export MASTER_PLIST="${1#*=}"
                shift
            ;;

    --master-plist* )
                export MASTER_PLIST="${2}"
                shift
                shift
            ;;

    --custom-plist=* )
                export MASTER_PLIST="${1#*=}"
                shift
            ;;

    --custom-plist* )
                export MASTER_PLIST="${2}"
                shift
                shift
            ;;

    --plists )
                export CREATE_PLISTS=1
                shift
            ;;
    --bootdisks ) 
                export CREATE_QCOWS=1
                shift
            ;;

    *)
                echo "Invalid option. Running with default values..."
                shift
            ;;
    esac
done


build_mac_serial () {
    export MACINFOPKG_VERSION="${MACINFOPKG_VERSION:=2.1.2}"
    wget -O "${TARBALL:=./MacInfoPkg.tar.gz}" \
        "https://github.com/acidanthera/MacInfoPkg/archive/${MACINFOPKG_VERSION}.tar.gz"
    tar -xzvf "${TARBALL}"
    cd "./MacInfoPkg-${MACINFOPKG_VERSION}/macserial" \
        && ./build.tool \
        && cd -
    mv "./MacInfoPkg-${MACINFOPKG_VERSION}/macserial/bin/macserial" .
    rm -f "${TARBALL}"
    rm -rf "./MacInfoPkg-${MACINFOPKG_VERSION}/"
    chmod +x ./macserial
    stat ./macserial
}

download_vendor_mac_addresses () {
    # download the MAC Address vendor list
    [[ -e "${MAC_ADDRESSES_FILE:=vendor_macs.tsv}" ]] || wget -O "${MAC_ADDRESSES_FILE}" https://gitlab.com/wireshark/wireshark/-/raw/master/manuf
}

download_qcow_efi_folder () {
    git clone --depth 1 https://github.com/kholia/OSX-KVM.git
    cp -ra ./OSX-KVM/OpenCore-Catalina/EFI .
    mkdir -p ./EFI/OC/Resources
    # clone some Apple drivers
    git clone --depth 1 https://github.com/acidanthera/OcBinaryData.git
    # copy said drivers into EFI/OC/Resources
    cp -a ./OcBinaryData/Resources/* ./EFI/OC/Resources
    # EFI Shell commands
    touch startup.nsh && echo 'fs0:\EFI\BOOT\BOOTx64.efi' > startup.nsh
}


generate_serial_sets () {

    if [[ "${MASTER_PLIST}" ]]; then
        [[ -e "${MASTER_PLIST}" ]] || echo "Could not find: ${MASTER_PLIST}"
    elif [[ "${MASTER_PLIST}" ]] && [[ "${MASTER_PLIST_URL}" ]]; then
        echo 'You specified both a custom plist file AND a custom plist url. Use one or the other.'
    elif [[ "${MASTER_PLIST_URL}" ]]; then
        wget -O "./${MASTER_PLIST:=/config-custom.plist}" "${MASTER_PLIST_URL}" \
            || echo "Could not download ${MASTER_PLIST_URL}" && exit 1
    else
        MASTER_PLIST_URL='https://raw.githubusercontent.com/sickcodes/Docker-OSX/master/custom/config-nopicker-custom.plist'
        wget -O "./${MASTER_PLIST:=/config-nopicker-custom.plist}" "${MASTER_PLIST_URL}" \
            || echo "Could not download ${MASTER_PLIST_URL}" && exit 1
    fi

    [[ -e ./opencore-image-ng.sh ]] || wget https://raw.githubusercontent.com/sickcodes/Docker-OSX/master/custom/opencore-image-ng.sh && chmod +x opencore-image-ng.sh

    mkdir -p "${OUTPUT_DIRECTORY}/envs"
    export DATE_NOW="$(date +%F-%T)"
    export DEVICE_MODEL="${DEVICE_MODEL:=iMacPro1,1}"
    export VENDOR_REGEX="${VENDOR_REGEX:=Apple, Inc.}"
    
    if [[ "${CSV_OUTPUT_FILENAME}" ]] || [[ "${TSV_OUTPUT_FILENAME}" ]]; then
        [[ ${CSV_OUTPUT_FILENAME} ]] && export CSV_SERIAL_SETS_FILE="${CSV_OUTPUT_FILENAME}"
        [[ ${TSV_OUTPUT_FILENAME} ]] && export TSV_SERIAL_SETS_FILE="${TSV_OUTPUT_FILENAME}"
    else
        export SERIAL_SETS_FILE="${OUTPUT_DIRECTORY}/serial_sets-${DATE_NOW}.csv"
    fi
    
    touch "${SERIAL_SETS_FILE}"
    echo "Writing serial sets to ${SERIAL_SETS_FILE}"

    ./macserial \
        --num "${SERIAL_SET_COUNT:=1}" \
        --model "${DEVICE_MODEL}" \
        | while IFS='\ \|\ ' read -r SERIAL BOARD_SERIAL; do
            # make a uuid...
            UUID="$(uuidgen)"
            UUID="${UUID^^}"

            # get a random vendor specific MAC address.
            RANDOM_MAC_PREFIX="$(grep -e "${VENDOR_REGEX}" < "${MAC_ADDRESSES_FILE:=vendor_macs.tsv}" | sort --random-sort | head -n1)"
            RANDOM_MAC_PREFIX="$(cut -d$'\t' -f1 <<< "${RANDOM_MAC_PREFIX}")"
            MAC_ADDRESS="$(printf "${RANDOM_MAC_PREFIX}:%02X:%02X:%02X" $[RANDOM%256] $[RANDOM%256] $[RANDOM%256])"

            # append to csv file
            if [[ "${CSV_SERIAL_SETS_FILE}" ]]; then
                echo "\"${DEVICE_MODEL}\",\"${SERIAL}\",\"${BOARD_SERIAL}\",\"${UUID}\",\"${MAC_ADDRESS}\"" >> "${CSV_SERIAL_SETS_FILE}"
            fi

            # append to tsv file
            if [[ "${TSV_SERIAL_SETS_FILE}" ]]; then
                printf "${DEVICE_MODEL}\t${SERIAL}\t${BOARD_SERIAL}\t${UUID}\t${MAC_ADDRESS}\n" >> "${TSV_SERIAL_SETS_FILE}"
            fi 

            OUTPUT_ENV_FILE="${OUTPUT_ENV:-"${OUTPUT_DIRECTORY}/envs/${SERIAL}.env.sh"}"
            touch "${OUTPUT_ENV_FILE}"
            cat <<EOF > "${OUTPUT_ENV_FILE}"
export DEVICE_MODEL="${DEVICE_MODEL}"
export SERIAL="${SERIAL}"
export BOARD_SERIAL="${BOARD_SERIAL}"
export UUID="${UUID}"
export MAC_ADDRESS="${MAC_ADDRESS}"
export WIDTH="${WIDTH:=1920}"
export HEIGHT="${HEIGHT:=1080}"
EOF

            # plist required for bootdisks, so create anyway.
            if [[ "${CREATE_PLISTS}" ]] || [[ "${CREATE_QCOWS}" ]]; then
                mkdir -p "${OUTPUT_DIRECTORY}/plists"
                source "${OUTPUT_ENV_FILE}"
                ROM_VALUE="${MAC_ADDRESS//\:/}"
                ROM_VALUE="${ROM_VALUE,,}"
                sed -e s/{{DEVICE_MODEL}}/"${DEVICE_MODEL}"/g \
                    -e s/{{SERIAL}}/"${SERIAL}"/g \
                    -e s/{{BOARD_SERIAL}}/"${BOARD_SERIAL}"/g \
                    -e s/{{UUID}}/"${UUID}"/g \
                    -e s/{{ROM}}/"${ROM}"/g \
                    -e s/{{WIDTH}}/"${WIDTH}"/g \
                    -e s/{{HEIGHT}}/"${HEIGHT}"/g \
                    "${MASTER_PLIST}" > "${OUTPUT_DIRECTORY}/plists/${SERIAL}.config.plist" || exit 1
            fi

            if [[ "${CREATE_QCOWS}" ]]; then
                mkdir -p "${OUTPUT_DIRECTORY}/qcows"
                ./opencore-image-ng.sh \
                    --cfg "${OUTPUT_DIRECTORY}/plists/${SERIAL}.config.plist" \
                    --img "${OUTPUT_QCOW:-${OUTPUT_DIRECTORY}/qcows/${SERIAL}.OpenCore-nopicker.qcow2}" || exit 1
            fi

        done

        [[ -e "${CSV_SERIAL_SETS_FILE}" ]] && \
            cat <(echo "DEVICE_MODEL,SERIAL,BOARD_SERIAL,UUID,MAC_ADDRESS") "${CSV_SERIAL_SETS_FILE}"


        [[ -e "${TSV_SERIAL_SETS_FILE}" ]] && \
            cat <(printf "DEVICE_MODEL\tSERIAL\tBOARD_SERIAL\tUUID\tMAC_ADDRESS\n") "${TSV_SERIAL_SETS_FILE}"
    
}

main () {
    # setting default variables if there are no options
    export DEVICE_MODEL="${DEVICE_MODEL:=iMacPro1,1}"
    export SERIAL_SET_COUNT="${SERIAL_SET_COUNT:=1}"
    export OUTPUT_DIRECTORY="${OUTPUT_DIRECTORY:=.}"
    cat <<EOF
DEVICE_MODEL:       ${DEVICE_MODEL}
SERIAL_SET_COUNT:   ${SERIAL_SET_COUNT}
OUTPUT_DIRECTORY:   ${OUTPUT_DIRECTORY}
EOF
    [[ -d "${OUTPUT_DIRECTORY}" ]] || mkdir -p "${OUTPUT_DIRECTORY}"
    [[ -e ./macserial ]] || build_mac_serial
    download_vendor_mac_addresses
    download_qcow_efi_folder
    generate_serial_sets
    echo "${SERIAL_SETS_FILE}"    
}

main