aboutsummaryrefslogtreecommitdiffstats
path: root/create_arm_image.sh
blob: 5a46a69794bdf5cecdd8b983c8658aec96661884 (plain)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/bash

# Copyright (C) 2017-2018 Daniel Tartavel-jeannot   <contact@librepc.com>
#                         Jean-Baptiste Biernacki   <j.biernacki@free.fr>
# Copyright (C) 2020      Neal Gompa                <ngompa13@gmail.com>
#
# 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,dnf 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, see <http://www.gnu.org/licenses/>.

#Change the local to the most global
export LC_ALL=C

source "$(dirname $(realpath "$0"))/functions.sh"

function help() {
    echo -e "${CMDNAME} [option]"
    echo -e "Options:"
    echo -e "--all                        create ready to burn image of Mageia ${MAGEIA_VERSION}"
    echo -e "--build-path                 Path to the build directory of the image of Mageia ${MAGEIA_VERSION}"
    echo -e "--clean                      Clean all (suppress all) to make a new image"
    echo -e "--log [file]                 logs the output into mga4arm-YYYY-MM-DD-HH-MM-\${config}.log or specific file."
    echo -e "--config [name | path]       A name of a default config or a path to a config files"
    echo -e "--target                     target system (for now rpi, odroid)"
    echo -e "--target-version             version of the target (0, 1, 2, 3 for rpi, 3 or 4 for odroid)"
#     echo -e "--bootfs                     filesystem of boot partition (ext4 or vfat) default: ext4"
    echo -e "--mga-version [1-7,cauldron] define which version of mageia to build"
    echo -e "--nonfree                    activate nonfree repos"
    echo -e "--size                       size of image default: 7Go"
    echo -e "--tainted                    activate tainted repos"
    echo -e "--desktop [xfce|...]         add a desktop environment task"
    echo -e "--package \"pkg1 pkg2\"        One space separated list of package to install"
    
    echo -e "\nBuild levels:"             
    echo -e "--create-chroot              Create the chroot directory"
    echo -e "--install-basesystem         install base system"
    echo -e "--add-urpmimedia             add mirrors for urpmi"
    echo -e "--chroot                     chroot to arm directory and launch packages installation"
    echo -e "--create-image               Create the image of Mageia ${MAGEIA_VERSION}"

    echo -e "\nFor image size, make sure it fit on physical support. (Default size is 7 Go)"
}


CMD_LINE="${@}"
if [ 0 -ne $(echo "${CMD_LINE}" | grep -c '\-\-log') ]; then
    if [ 0 -ne $(echo "${CMD_LINE}" | grep -c '\-\-config') ]; then
        CONFIG_NAME="$(echo "${CMD_LINE}" | sed -e 's/^.*--config //' | sed -e 's/[[:space:]]*--.*$//' | sed -e 's/.*\/\([-_+[:alnum:]]*\)$/\1/' | tr ' ' '_')"
        if [ "x" = "${CONFIG_NAME}x" ]; then
            CONFIG_NAME="noconf"
        fi
    else
        CONFIG_NAME="noconf"
    fi
    LOGFILE="$(echo "${CMD_LINE}" | sed -e 's/^.*--log //' | sed -e 's/[[:space:]]*--.*$//' )"
    if [ "x" = "${LOGFILE}x" ]; then
        mkdir -p ./log
        LOGFILE="./log/mga4arm-$(date "+%Y-%m-%d-%H:%M:%S")-${CONFIG_NAME}.log"
        LOGCMD="--log "
    else
        LOGCMD="--log ${lOGFILE}"
    fi
    CMD_LINE="$(echo ${CMD_LINE} | sed -e "s/${LOGCMD}//" )"
    touch ${LOGFILE}
    /usr/bin/chown ${USER}:${USER} ${LOGFILE}
    exec ${0} ${CMD_LINE} 2>&1 | tee ${LOGFILE}
    exit
fi

print_info ${0}

#Check dependencies :
#if [ -e /usr/bin/qemu-arm-static ]; then
#   error "qemu-user-static package is needed."
#   exit ${ERR_DEPENDENCY_MISSING}
#fi
if [ 0 -ne $(id -u) ]; then
   error "Script need to be run as root."
   exit ${ERR_NOT_ROOT}
fi

SOURCE_PATH="$(/usr/bin/dirname  "$(readlink -f "${0}")")"
INSTALL_PATH="$(pwd)/build"
PLATFORMS_PATH="${SOURCE_PATH}/platforms"
INSTALL_METHOD="urpmi"

CMDNAME=$(/usr/bin/basename "${0}")


if [ ${#} == 0 ]; then
    help
    exit
fi


# parsing commandline
TEMP=$(getopt -o h,a --long add-urpmimedia,add-checksum,all,help,clean,create-chroot,prepare-chroot,jump-chroot,create-image,config:,compress,mga-version:,target:,target-version:,chroot,bootfs:,install-basesystem,update-mirror,build-path:,size:,sign,nonfree,tainted -n ${CMDNAME}  -- "${@}")
if [ ${?} -ne 0 ] ; then error "line ${LINENO} Failed parsing options." >&2 ; exit ${ERR_1} ; fi
eval set -- "${TEMP}"

echo "${TEMP}"
while true; do
    case "${1}" in
    --add-urpmimedia)
        if [ -z ${OPT} ]; then
            OPT="addurpmimedia"
        fi
        shift
        ;;
    --add-checksum)
        GEN_CHECKSUM="true"
        shift
        ;;
    -a|--all)
        OPT="all"
        shift 1
        ;;
    --bootfs)
        BOOTFS_P=${2}
        shift 2
        ;;
    --build-path)
        mkdir -p ${2}
        INSTALL_PATH_P=$(readlink -f "${2}")
        shift 2
        ;;
    --chroot)
        if [ -z ${OPT} ]; then
            OPT="chroot"
        fi
        shift
        ;;
    --clean)
        OPT="clean"
        shift
        ;;
    --compress)
        GEN_GZ="true"
        shift
        ;;
    --config)
        PATH_NOT_NAME=$(echo "${2}" | grep '/' 2>&1 1>/dev/null && echo "true" || echo "false" )
        if [ ${PATH_NOT_NAME} == true ] ; then
            CONFIG_PATH="${2}"
        else
            CONFIG_PATH="${PLATFORMS_PATH}/${2}"
        fi
        shift 2
        ;;
    --create-chroot)
        if [ -z ${OPT} ]; then
            OPT="createchroot"
        fi
        shift
        ;;
    --create-image)
        if [ -z ${OPT} ]; then
            OPT="createimage"
        fi
        shift
        ;;
    --desktop)
        DESKTOP="${2}"
        DESKTOP_TASK="task-${DESKTOP}"
        shift 2
        ;;
    -h|--help)
        help
        exit 0
        ;;
    --install-basesystem)
        if [ -z ${OPT} ]; then
            OPT="installbasesystem"
        fi
        shift
        ;;
    --jump-chroot)
        if [ -z ${OPT} ]; then
            OPT="jumpchroot"
        fi
        shift
        ;;
    --mga-version)
        if [ "xcauldron" = "x${2}" ]; then
            MAGEIA_VERSION="${2}"
        elif [ "${2}" -gt 0 ]; then
            MAGEIA_VERSION="${2}"
        else
            exit ${ERR_1}
        fi
        shift 2
        ;;
    --mirror)
        MIRROR="${2}"
        shift 2
        ;;
    --nonfree)
        NONFREE_P=1
        shift
        ;;
    --package)
        PACKAGE="${2}"
        shift 2
        ;;
    --prepare-chroot)
        if [ -z ${OPT} ]; then
            OPT="preparechroot"
        fi
        shift
        ;;
    --sign)
        GEN_SIGN="true"
        shift
        ;;
    --size)
        IMAGE_SIZE_P=${2}
        shift 2
        ;;
    --tainted)
        TAINTED_P=1
        shift
        ;;
    --target)
        TARGET_P=${2}
        shift 2
        ;;
    --target-version)
        TARGET_VERSION_P=${2}
        shift 2
        ;;
    --update-mirror)
        if [ -z ${OPT} ]; then
            OPT="updatemirror"
        fi
        shift
        ;;
    --)
      shift
      break;;
    *)
        error "Parameter ${1} does not exists "
        exit ${ERR_1};;
    esac
done


# path of config file
if ! [ -d "${CONFIG_PATH}" ]; then
    print_info " Config path does not exists, defaulting to ./platforms/${TARGET}"
    CONFIG_PATH="${PLATFORMS_PATH}/${TARGET}"
    warning "Do you want to create it and to copy the template file in? [Y|n] "
    read yn
    if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then
        /usr/bin/mkdir "${CONFIG_PATH}/"
        /usr/bin/cp --preserve=mode "${SOURCE_PATH}/mageia4arm.cfg.template" "${CONFIG_PATH}/mageia4arm.cfg"
        warning "You need now to modify the config file (${CONFIG_PATH}/mageia4arm.cfg) and relaunch the script"
        exit ${ERR_DEFAULT_CONFIG}
    else
        error "Error: Can't continue without config file, exiting"
        exit ${ERR_NO_CONFIG_FILE}
    fi
fi

if [ -e "${CONFIG_PATH}/mageia4arm.cfg" ]; then
    print_info "using ${CONFIG_PATH}/mageia4arm.cfg as config"
    source "${CONFIG_PATH}/mageia4arm.cfg"                  #NOTE1 : Here is sourced a file with variables.
else
    warning "Config file does not exists, do you want to copy template ? [Y|n] "
    read yn
    if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then
        /usr/bin/cp --preserve=mode "${SOURCE_PATH}/mageia4arm.cfg.template" "${CONFIG_PATH}/mageia4arm.cfg"
        warning "You need now to modify the config file (${CONFIG_PATH}/mageia4arm.cfg) and relaunch the script"
        exit ${ERR_DEFAULT_CONFIG}
    fi
fi

# VARIABLE_A=${VARIABLE_B_IF_EXISTS:=${VARIABLE_C_IF_B_DOESNT_EXISTS}}
IMAGE_SIZE=2 #Set a default size in case it is missing.
#Redefined priority : commandline, then specific board config file.
IMAGE_SIZE="${IMAGE_SIZE_P:=${IMAGE_SIZE}}"
INSTALL_PATH="${INSTALL_PATH_P:=${INSTALL_PATH}}"
TARGET="${TARGET_P:=${TARGET}}"
TARGET_VERSION="${TARGET_VERSION_P:=${TARGET_VERSION}}"
BOOTFS="${BOOTFS_P:=${BOOTFS}}"
NONFREE="${NONFREE_P:=0}"
TAINTED="${TAINTED_P:=0}"

#name of the image
# TODO Add nonfree if image contains nonfree drivers for example.
DESKTOP_NAME="${DESKTOP:="noDE"}"
IMAGE_BASE="Mageia-${MAGEIA_VERSION}-${TARGET}"
IMAGE="${IMAGE_BASE}-${DESKTOP_NAME}.img"

print_info "Option: "${OPT}

BUILD_PATH="${INSTALL_PATH}/build-${TARGET}${TARGET_VERSION}"

DNF_CONFIGPATH="$(dirname $(realpath "$0"))/mageia4arm-dnf.conf"

# Assigne a script for creating a particular target image (rpi, odroid, ...)
if [ -e "${CONFIG_PATH}/specialFunctions.sh" ]; then
    source "${CONFIG_PATH}/specialFunctions.sh"
fi

print_info "target : ${TARGET}"
print_info "target_version : ${TARGET_VERSION}"
print_info "source path: ${SOURCE_PATH}"
print_info "Install path: ${INSTALL_PATH}"
print_info "Build path: ${BUILD_PATH}"
print_info "Commande : ${CMDNAME}"
print_info "Firmware path : ${FIRMWARE_PATH}"
print_info "Firmware dir : ${FIRMWARE_DIR}"
print_info "Installation method : ${INSTALL_METHOD}"

# change dir to install path and create it if not existing
if [ "${OPT}" != "clean" ]; then
    print_info "cd ${INSTALL_PATH}"
    if ! [ -e "${INSTALL_PATH}" ]; then
        /usr/bin/mkdir -p "${INSTALL_PATH}"
        if [ -z ${?} ]; then
            error "line ${LINENO} can't make directory ${INSTALL_PATH} , exiting"
            exit ${ERR_1}
        fi
    fi
    cd "${INSTALL_PATH}"
    if [ -z ${?} ]; then
        error "line ${LINENO} can't change to directory ${INSTALL_PATH} , exiting"
        exit ${ERR_1}
    fi
fi

#if no parameters then display help message
OPT=${OPT:="--help"}

print_info "Image size is: ${IMAGE_SIZE} GB"


case ${OPT} in
    "clean")
        clean
        ;;
    "all")
        verify_disk_space
        if [ ${?} -ne 0 ]; then
            echo -e "Not enough space on disk\nDo you want to continue anyway ? [Y,n]"
            read yn
            if [ ${yn} = "n" ]; then
                exit ${ERR_NO_SPACE}
            fi
        fi
        check_extrapackage
        createImageWrap #Create the empty .img
        mountPartitions
        createchroot #Check qemu and activate it
        if [ ${INSTALL_METHOD} = "urpmi" ]; then
            addurpmimedia
        else
            genusemirroroptions #Generate mirror options, if needed
        fi
        installbasesystem #Generate the rootfs, rootfiles, ...
        mkfstab
        preparechroot
        jumpchroot
        extrapackage
        copyingsystem
        bunrningBootloader
        unmountingPartitions
        unloopingImage
        
        warning "You can now burn the image ( ${INSTALL_PATH}/${IMAGE} ) on SD card"
        print_info "pv \"${INSTALL_PATH}/${IMAGE}\" | dd of=/dev/mmcblk#"
        generateExtra
        ;;
    "createchroot")
        createchroot
        ;;
    "preparechroot")
        preparechroot
        ;;
    "jumpchroot")
        jumpchroot
        ;;
    "chroot")
        jumpchroot
        ;;
    "addurpmimedia")
        addurpmimedia
        ;;
    "createimage")
        verify_disk_space
        if [ ${?} -ne 0 ]; then
            warning "Not enough space on disk"
            exit ${ERR_NO_SPACE}
        fi
        createImageWrap
        ;;
    "installbasesystem")
        installbasesystem
        ;;
esac


if [ ${ERRORN} -ne 0 ]; then
    warning "Some errors occurs : ${ERRORN} errors"
fi

unset exit
exit ${ERRORN}