aboutsummaryrefslogtreecommitdiffstats
path: root/create_arm_image.sh
blob: 2649bc01949d226bf8506a3e337e162de1227602 (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
#!/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] [size of image in Go]"
    echo -e "Options:"
    echo -e "--all                create ready to burn image of Mageia ${MAGEIA_VERSION}"
    echo -e "--clean              Clean all (suppress all) to make a new image"
    echo -e "--size               size of image default: 7Go"
    echo -e "--build-path         Path to the build directory of the image of Mageia ${MAGEIA_VERSION}"
    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 "--config             Path to config files"
    echo -e "--bootfs             filesystem of boot partition (ext4 or vfat) default: ext4"
    echo -e "--nonfree            activate nonfree repos"
    echo -e "--tainted            activate tainted repos"

    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 "--config             Path to config files (rpi2  rpi3  rpi3+  xu4  bananaPro)"

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


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,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)
        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
        ;;
    -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
        ;;
    --nonfree)
        NONFREE_P=1
        shift
        ;;
    --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
    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
    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=${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}


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

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

# Defining default image size to 7 go
if [[ "${IMAGE_SIZE}" -lt 2 ]] || [[ "${IMAGE_SIZE}" -gt 128 ]]; then
    IMAGE_SIZE=7
fi

# change dir to install path and create it if not existing
if [ "${OPT}" != "clean" ]; then
    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"}

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
        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
        copyingsystem
        bunrningBootloader
        unmountingPartitions
        unloopingImage
        ;;
    "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

exit ${ERRORN}