aboutsummaryrefslogtreecommitdiffstats
path: root/create_arm_img_urpmi.sh
blob: 5d9141c3ed9efe7c47113fdeb1e925fe6d1c0415 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#!/bin/bash

# Copyright (C) 2017-2018 Daniel Tartavel-jeannot   <contact@librepc.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, 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/>.


function title()
{
    echo -e "\e[1;32m${1}\e[0m"
}


function info()
{
    echo -e "\e[36m${1}\e[0m"
}


function warning()
{
    echo -e "\e[1;35m${1}\e[0m"
}


function error()
{
    echo -e "\e[1;31m${1}\e[0m"
}


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 "--addmedia           add mirrors"
    echo -e "--update-mirror      update urpmi database"
    echo -e "--install-basesystem install base system"
    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)"
}


# cleaning build space
function clean()
{
    # Unmounting /dev /proc /sys in chroot
    title "Cleaning"
    if ! [ -z "$(mountpoint -qd "${BUILD_PATH}/dev")" ]; then # mountpoint - see if a directory or file is a mountpoint ; -d Show the major/minor numbers of the device that is mounted on the given directory.
        info "Unmounting ${BUILD_PATH}/dev"
        umount "${BUILD_PATH}/dev"
        if ! [ ${?} -ne 0 ]; then
            error "line ${LINENO} can't unmount ${BUILD_PATH}/dev : exiting"
            #exit ${ERR_1}
        fi
    fi
    if ! [ -z "$(mountpoint -qd "${BUILD_PATH}/sys")" ]; then
        info "Unmounting ${BUILD_PATH}/sys"
        umount "${BUILD_PATH}/sys"
        if ! [ ${?} -ne 0 ]; then
            error "line ${LINENO} can't unmount ${BUILD_PATH}/sys : exiting"
            #exit ${ERR_1}
        fi
    fi
    if ! [ -z "$(mountpoint -qd "${BUILD_PATH}/proc")" ]; then
        info "Unmounting ${BUILD_PATH}/proc"
        umount "${BUILD_PATH}/proc"
        if ! [ ${?} -ne 0 ]; then
            error "line ${LINENO} can't unmount ${BUILD_PATH}/proc : exiting"
            #exit ${ERR_1}
        fi
    fi
    # Removing old Build directory
    if [ -d "${BUILD_PATH}" ]; then
        info "Removing ${BUILD_PATH}"
        /bin/rm -Rf "${BUILD_PATH}"
    else
        warning "${BUILD_PATH} does not exists"
    fi
    # removing old image
    if [ -e "${IMAGE}" ]; then
        info "Removing ${IMAGE}"
        /bin/rm -f "${INSTALL_PATH}/${IMAGE}"
    else
        warning "${IMAGE} does not exists"
    fi
    # removing loop devices
    for LOOP in $(ls /dev/loop*[0-9]p1); do
        X=${LOOP:0:10}
        if [ -e "${X}" ]; then
            info "removing ${X}"
            partx -d "${X}" # Tell the kernel about the presence and numbering of partitions.
        fi
    done
    # Removing boot and root directory
    if [ -d "${BOOT}" ]; then
        info "Removing ${BOOT}"
        /bin/rmdir "${BOOT}"
    else
        warning "${BOOT} does not exists"
    fi
    if [ -d "${ROOT}" ]; then
        info "Removing ${ROOT}"
        /bin/rmdir "${ROOT}"
    else
        warning "${ROOT} does not exists"
    fi
    return 0
}


# creation of install path and copy of qemu ( installing it if not yet installed )
function createchroot()
{
    title "Making chroot"
    if ! [ -f /bin/qemu-arm-static ]; then
        title "Qemu package not present : installing qemu packages"
        /sbin/urpmi --auto --no-recommends qemu-user-static
        if [ ${?} -ne 0 ]; then
            error "line ${LINENO} can't install qemu-user-static : exiting"
            exit ${ERR_1}
        fi
    fi
    # Starting qemu service if not started
    /bin/systemctl is-active systemd-binfmt.service
    if  [ ${?} -ne 0 ]; then
        title "Starting systemd-binfmt.service"
        /bin/systemctl start systemd-binfmt.service
        if [ ${?} -ne 0 ]; then
            error "line ${LINENO} can't start qemu-user-static : exiting"
            exit ${ERR_1}
        fi
    fi
    return 0
}


# adding Mageia repositories
function addmedia()
{
    title "Creating media ${MIRROR}"
    /sbin/urpmi.addmedia --urpmi-root  "${BUILD_PATH}" --distrib "${MIRROR}"
    err=${?}
    if [ ${err} -ne 0 ]; then
        error "line ${LINENO} error ${err} - can't add medias from ${MIRROR} : exiting"
        exit ${ERR_1}
    fi
    if [ ${NONFREE} -eq 1 ]; then
        title "activating non-free repos"
        /sbin/urpmi.update --urpmi-root  "${BUILD_PATH}" --no-ignore Nonfree\ Release Nonfree\ Updates
        err=${?}
        if [ ${err} -ne 0 ]; then
            error "line ${LINENO} error ${err} - can't activate medias nonfree : exiting"
            exit ${ERR_1}
        fi
    fi
    if [ ${TAINTED} -eq 1 ]; then
        title "activating tainted repos"
        /sbin/urpmi.update --urpmi-root  "${BUILD_PATH}" --no-ignore Tainted\ Release Tainted\ Updates
        err=${?}
        if [ ${err} -ne 0 ]; then
            error "line ${LINENO} error ${err} - can't activate medias tainted : exiting"
            exit ${ERR_1}
        fi
    fi
    return 0
}


function updatemirror()
{
    title "updating mirror"
    /sbin/urpmi.update -a --urpmi-root "${BUILD_PATH}"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} can't update mirrors : exiting"
        exit ${ERR_1}
    fi
    return 0
}


function installbasesystem()
{
    # Create Build path
    if ! [ -d "${BUILD_PATH}" ]; then
        warning "Build path ( ${BUILD_PATH} ) does not exist, do you want to create it ? [Y|n] "
        read yn
        if [ -z ${yn} ] || [ ${yn} = "Y" ] || [ ${yn} = "y" ]; then
            title "Creating ${BUILD_PATH}"
            /bin/mkdir -p "${BUILD_PATH}/usr/bin" "${BUILD_PATH}/usr/lib/binfmt.d"
            if [ ${?} -ne 0 ]; then
                error "line ${LINENO} can't create ${BUILD_PATH} : exiting"
                exit ${ERR_1}
            fi
        else
            exit ${ERR_1}
        fi
    else
        info "Build path exists"
    fi

    title "installing basesystem"
    /sbin/urpmi --urpmi-root "${BUILD_PATH}"  --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 shadow-utils basesystem-minimal
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error installing shadow-utils or basesystem-minimal : exiting"
        exit ${ERR_1}
    fi
    #/sbin/urpmi --urpmi-root "${BUILD_PATH}"  --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 basesystem-minimal
    #if [ ${?} -ne 0 ]; then
    #    error "line ${LINENO} error installing basesystem-minimal  : exiting"
    #    exit ${ERR_1}
    #fi
    /sbin/urpmi --urpmi-root "${BUILD_PATH}"  --ignorearch --no-verify-rpm --auto --split-level 200 --split-length 200 urpmi locales-fr systemd u-boot
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error installing urpmi or locales.fr : exiting"
        exit ${ERR_1}
    fi
    return 0
}


function preparechroot()
{
    title "Preparing chrooting in ${BUILD_PATH}"

    #Copying qemu

    /bin/cp /bin/qemu-arm-static "${BUILD_PATH}/usr/bin/"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} can't copy /bin/qemu-user-static to ${BUILD_PATH}/usr/bin/ : exiting"
        exit ${ERR_1}
    fi
    /bin/cp /usr/lib/binfmt.d/qemu-arm-static.conf "${BUILD_PATH}/usr/lib/binfmt.d"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} can't copy /usr/lib/binfmt.d/qemu-arm-static.conf to ${BUILD_PATH}/usr/lib/binfmt.d : exiting"
        exit ${ERR_1}
    fi


    info "making /etc/hostname"
    echo "${HOSTNAME}" > "${BUILD_PATH}/etc/hostname"

    info "copying second stage script in ${BUILD_PATH}"
    #echo "/bin/cp ${CONFIG_PATH}/second_stage_install.sh ${BUILD_PATH}/"
    cp --preserve=mode "${CONFIG_PATH}/second_stage_install.sh" "${BUILD_PATH}/second_stage_install.sh"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error copying ${CONFIG_PATH}/second_stage_install.sh : exiting"
        exit ${ERR_1}
    fi

    info "creation of user account"
    echo -e "/bin/passwd << EOF\n${ROOT_PWD}\n${ROOT_PWD}\nEOF\n    /sbin/useradd ${ID_USER}\n    /bin/passwd ${ID_USER} << EOF\n${PASSWORD}\n${PASSWORD}\nEOF" >>"${BUILD_PATH}/second_stage_install.sh"

    info "Copying skel in root directory"
    /bin/rsync -rlptH /etc/skel/ "${BUILD_PATH}/root/"

    postPrepareChroot
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error in postPrepareChroot function."
        exit ${ERR_1}
    fi
    return 0
}


function jumpchroot()
{
    title "chrooting to ${BUILD_PATH}"
    info "mounting dev, sys, proc directories in chroot"
    /bin/mount -B /dev "${BUILD_PATH}/dev"
    if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error mounting ${BUILD_PATH}/dev : exiting"
            exit ${ERR_1}
    fi
    /bin/mount -B /sys "${BUILD_PATH}/sys"
    if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error mounting ${BUILD_PATH}/sys : exiting"
            exit ${ERR_1}
    fi
    /bin/mount -B /proc "${BUILD_PATH}/proc"
    if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error mounting ${BUILD_PATH}/proc : exiting"
            exit ${ERR_1}
    fi
    info "Copying resolv.conf"
    /bin/cp -v --preserve=mode /etc/resolv.conf "${BUILD_PATH}/etc/"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error copying ${BUILD_PATH}/etc/resolv.conf : exiting"
        exit ${ERR_1}
    fi
    if [ "$OPT" = "chroot" ]; then
        /sbin/chroot "${BUILD_PATH}"
    else
        /sbin/chroot --userspec root:root "${BUILD_PATH}" /bin/bash -v -c 'sh /second_stage_install.sh'
    fi
    RET=${?}
    if [ ${RET} -ne 0 ]; then
        error "line ${LINENO} Warning : chrooting to ${BUILD_PATH} retrurn an error ${RET}"
        ERRORN=$((${ERRORN}+1))
    fi
    info "unmounting dev, sys, proc"
    /bin/umount -lf "${BUILD_PATH}/dev"
    if [ ${?} -ne 0 ]; then
        warning "line ${LINENO} Warning : error unmounting ${BUILD_PATH}/dev, continuing anyway"
        ERRORN=$((${ERRORN}+1))
    fi
    /bin/umount -lf "${BUILD_PATH}/sys"
    if [ ${?} -ne 0 ]; then
        warning "line ${LINENO} Warning : error unmounting ${BUILD_PATH}/sys, continuing anyway"
        ERRORN=$((${ERRORN}+1))
    fi
    /bin/umount -lf "${BUILD_PATH}/proc"
    if [ ${?} -ne 0 ]; then
        warning "line ${LINENO} Warning : error unmounting ${BUILD_PATH}/proc, continuing anyway"
        ERRORN=$((${ERRORN}+1))
    fi
    return 0
}


function verify_disk_space()
{
    title "Verifying if there is enough space on disk to make the image"
    DISK_SPACE=$(/usr/bin/df -BG --output=avail "${INSTALL_PATH}"  | sed '1d;s/[^0-9]//g')
    info "Free disk space: ${DISK_SPACE}G"
    if [ ${DISK_SPACE} -lt ${IMAGE_SIZE} ]; then
        warning "image size is greater than disk space"
        info "correct the problem and relaunch the script with parameter --create-rpi-image"
        info "${0} --size ${IMAGE_SIZE} --create-rpi-image"
        return 1
    fi
    return 0
}


function createImageWrap()
{
    title "Wrap image creation"
    preImgCreation
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error in the process ${CONFIG_PATH}/specialFunctions.sh ."
        exit ${ERR_1}
    fi
    createimage
    if [ -z "${BOOTFS}" ]; then
        BOOTFS="ext4"
    fi
    formatpartitions ${BOOTFS} ext4
    copyingsystem
    return 0
}


createimage()
{
    title " in ${IMAGE}"
    if [ -f "${INSTALL_PATH}/${IMAGE}" ]; then
        warning "Deleting previous image"
        /bin/rm -f "${INSTALL_PATH}/${IMAGE}"
        if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error can't remove previous image at ${INSTALL_PATH}/${IMAGE} : exiting"
            exit ${ERR_1}
        fi
    fi

    warning "please wait until end of image creation"
    /bin/dd if=/dev/zero of="${INSTALL_PATH}/${IMAGE}" bs=1MB count=$(( ${IMAGE_SIZE} * 1024 ))
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} can't make image at ${INSTALL_PATH}/${IMAGE} : exiting"
        exit ${ERR_1}
    fi
    DEVICE=$(/sbin/losetup -f --show "${INSTALL_PATH}/${IMAGE}")
    
    bunrningBootloader
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error in the process ${CONFIG_PATH}/specialFunctions.sh ."
        exit ${ERR_1}
    fi

    info "making partitions"
    echo -e "${FDISK_SCRIPT}" | /sbin/fdisk ${DEVICE}

    # Activate loop device
    info "device to detach ${DEVICE}"
    /sbin/losetup -d "${DEVICE}"
    DEVICE=$(/sbin/partx -va "${INSTALL_PATH}/${IMAGE}" | /bin/grep -m 1 -E -o '/dev/loop.?')
    info "New attached device ${DEVICE}"
    info "partitions list:"
    info "$(/sbin/partx -v "${INSTALL_PATH}/${IMAGE}")"
    BOOTP="${DEVICE}p1"
    ROOTP="${DEVICE}p2"
    return 0
}


formatpartitions()
{
    info "Formatting partitions"
    info "Boot : ${BOOTP} as ${1}"
    "/sbin/mkfs.${1}" "${BOOTP}"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error formating ${BOOTP} : exiting"
        /sbin/losetup -d "${DEVICE}"
        exit ${ERR_1}
    fi
    info "Root : ${ROOTP} as ${2}"
    "/sbin/mkfs.${2}" "${ROOTP}"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error formating ${ROOTP} : exiting"
        /sbin/losetup -d "${DEVICE}"
        exit ${ERR_1}
    fi
    BOOT_UUID=$(blkid -s UUID -o value UUID "${BOOTP}")
    info "Boot UUID: ${BOOT_UUID}"
    ROOT_UUID=$(blkid -s UUID -o value UUID "${ROOTP}")
    info "Root UUID: ${ROOT_UUID}"
    return 0
}


copyingsystem()
{
    info "mounting partitions, making mountpoint if necessary"
    if ! [ -d "${BOOT}" ]; then
        /bin/mkdir "${BOOT}"
        if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error making directory ${BOOT} : exiting"
            exit ${ERR_1}
        fi
    fi
    if ! [ -d "${ROOT}" ]; then
        /bin/mkdir "${ROOT}"
        if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error making directory ${ROOT} : exiting"
            exit ${ERR_1}
        fi
    fi
    /bin/mount "${BOOTP}" "${BOOT}"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error mounting ${BOOTP} : exiting"
        exit ${ERR_1}
    fi
    /bin/mount "${ROOTP}" "${ROOT}"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error mounting ${ROOTP} : exiting"
        exit ${ERR_1}
    fi

    info "making /etc/fstab"
    ### BUG : /mnt/arm_boot is set to vfat for all plateforms, odroid configuration says ext4.
    #echo -e "proc            /proc           proc    defaults        0       0\nUUID=${BOOT_UUID}  /mnt/arm_boot   vfat    defaults        0       0\nUUID=${ROOT_UUID}  /               ext4    defaults        0       0" > "${BUILD_PATH}/etc/fstab"
    echo -e "proc\t/proc\tproc\tdefaults\t0\t0\nUUID=${BOOT_UUID}\t/mnt/arm_boot\t${BOOTFS}\tdefaults\t0\t0\nUUID=${ROOT_UUID}\t/\text4\tdefaults\t0\t0" > "${BUILD_PATH}/etc/fstab"

    if [ ! -d "${BUILD_PATH}/boot/extlinux" ]; then
        info "making /boot/extlinux/extlinux.conf"
        rm -rf "${BUILD_PATH}/boot/extlinux"
        mkdir -p "${BUILD_PATH}/boot/extlinux"
    fi
    if [ ! -f "${BUILD_PATH}/boot/extlinux/extlinux.conf" ]; then
        sed -e s/\<UUID\>/${ROOT_UUID}/g ${CONFIG_PATH}/extlinux.conf >"${BUILD_PATH}/boot/extlinux/extlinux.conf"
        KERNEL_ID=$(basename ${BUILD_PATH}/usr/lib/linux-*)
        sed -i -e s/\<FDTDIR\>/${KERNEL_ID}/g "${BUILD_PATH}/boot/extlinux/extlinux.conf"
    fi
    
    
    copyingCustomSystem
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error in the process ${CONFIG_PATH}/specialFunctions.sh ."
        exit ${ERR_1}
    fi

#    info "copying tools in /usr/local/bin/"
#    /bin/cp -v --preserve=mode "${SOURCE_PATH}/tools/" "${BUILD_PATH}/usr/local/bin/"
#    if ! [ ${?} -eq 0 ]; then
#        error "line ${LINENO} error copying tools"
#        ERRORN=$((${ERRORN}+1))
#    fi
#    /bin/mkdir "${ROOT}/boot"

    copyingcommon
    # Syncing devices before unmounting
    /usr/bin/sync
    /usr/bin/umount "${BOOT}" "${ROOT}"
    if [ ${?} -eq 0 ]; then
        /bin/rmdir "${BOOT}" "${ROOT}"
    else
        error "line ${LINENO} error unmounting ${ROOT} or ${BOOT} : exiting"
        exit ${ERR_1}
    fi

    /usr/sbin/partx -d "${DEVICE}"
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} warning : error unmounting ${DEVICE} "
        exit ${ERR_1}
    fi
    warning "You can now burn the image ( ${INSTALL_PATH}/${IMAGE} ) on SD card"
    return 0
}


# Copying files common to all systems
copyingcommon()
{
    title "Copying common files and configuration"
    rsync -rlptDH "${SOURCE_PATH}/common/" "${ROOT}/"
    rsync -rlptDH "${SOURCE_PATH}/tools/"*.sh "${ROOT}/usr/local/bin/"
    chown root:root "${ROOT}/usr/local/bin/"
    return 0
}


function preImgCreation()
{
    error "This function is called if no sourced file about fdisk was lauched."
    return 1
}

function postPrepareChroot()
{
    error "This function is called if no sourced file is containing postPrepareChroot function."
    return 1
}

function bunrningBootloader()
{
    error "This function is called if no sourced file is containing bunrningBootloader function."
    return 1
}

function copyingCustomSystem()
{
    error "This function is called if no sourced file is containing bunrningBootloader function."
    return 1
}



ERRORN=0
ERR_1=1
ERR_DEFAULT_CONFIG=2
ERR_NO_CONFIG_FILE=3
ERR_NOT_ROOT=4
ERR_DEPENDENCY_MISSING=5
ERR_NO_SPACE=6
ERR_7=7
ERR_8=8
ERR_9=9


#######################
# PROGRAMM START HERE #
#######################


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="$(/bin/dirname  "$(readlink -f "${0}")")"
INSTALL_PATH="${SOURCE_PATH}/arm"
FILES_PATH="${SOURCE_PATH}/files"
PLATFORMS_PATH="${SOURCE_PATH}/platforms"

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


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


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

echo "${TEMP}"
# Note the quotes around `$TEMP': they are essential! Or not, $( ) do the same as ` `. But there are the ` ' used ?
while true; do
    case "${1}" in
    -h|--help)
        help
        exit 0
        ;;
    -a|--all)
        OPT="all"
        shift 1
        ;;
    --clean)
        OPT="clean"
        shift
        ;;
    --size)
        IMAGE_SIZE_P=${2}
        shift 2
        ;;
    --build-path)
        INSTALL_PATH_P=$(readlink -f "${2}")
        shift 2
        ;;
    --chroot)
        if [ -z ${OPT} ]; then
            OPT="chroot"
        fi
        shift
        ;;
    --create-chroot)
        if [ -z ${OPT} ]; then
            OPT="createchroot"
        fi
        shift
        ;;
    --addmedia)
        if [ -z ${OPT} ]; then
            OPT="addmedia"
        fi
        shift
        ;;
    --update-mirror)
        if [ -z ${OPT} ]; then
            OPT="updatemirror"
        fi
        shift
        ;;
    --install-basesystem)
        if [ -z ${OPT} ]; then
            OPT="installbasesystem"
        fi
        shift
        ;;
    --create-image)
        if [ -z ${OPT} ]; then
            CREATEIMAGE=true
            OPT="createimage"
        fi
        shift
        ;;
    --target)
        TARGET_P=${2}
        shift 2
        ;;
    --target-version)
        TARGET_VERSION_P=${2}
        shift 2
        ;;
    --config)
        CONFIG_PATH="${PLATFORMS_PATH}/${2}"
        shift 2
        ;;
    --bootfs)
        BOOTFS_P=${2}
        shift 2
        ;;
    --nonfree)
        NONFREE_P=1
        shift
        ;;
    --tainted)
        TAINTED_P=1
        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

if ! [ -z "${IMAGE_SIZE_P}" ]; then
    IMAGE_SIZE=${IMAGE_SIZE_P}
fi
if ! [ -z "${INSTALL_PATH_P}" ]; then
    INSTALL_PATH=${INSTALL_PATH_P}
fi
if ! [ -z "${TARGET_P}" ]; then
    TARGET=${TARGET_P}
fi
if ! [ -z "${TARGET_VERSION_P}" ]; then
    TARGET_VERSION=${TARGET_VERSION_P}
fi
if ! [ -z "${BOOTFS_P}" ]; then
    BOOTFS=${BOOTFS_P}
fi
if ! [ -z "${NONFREE_P}" ]; then
    NONFREE=${NONFREE_P}
else
    NONFREE=0
fi
if ! [ -z "${TAINTED_P}" ]; then
    TAINTED=${TAINTED_P}
else
    TAINTED=0
fi

info "Option: "${OPT}

IMAGE="Mageia-${MAGEIA_VERSION}-${TARGET}${TARGET_VERSION}.img"
BOOT="/mnt/boot"
ROOT="/mnt/root"
BUILD_PATH="${INSTALL_PATH}/build"
ARM_VERSION="armv7hl"

# 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}"

# Defining default image size to 7 go
if [[ "${IMAGE_SIZE}" -lt 7 ]] || [[ "${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
        /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
if  [ -z ${OPT} ]; then
    OPT="--help"
fi

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

case ${OPT} in
    "clean")
        clean
        ;;
    "all")
        verify_disk_space
        if [ ${?} -eq 1 ]; 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
        createchroot
        addmedia
        updatemirror
        installbasesystem
        preparechroot
        jumpchroot
        createImageWrap
        ;;
    "createchroot")
        createchroot
        ;;
    "chroot")
        jumpchroot
        ;;
    "addmedia")
        addmedia
        ;;
    "updatemirror")
        updatemirror
        ;;
    "createimage")
        verify_disk_space
        if [ ${?} -eq 1 ]; then
            warning "Not enough space on disk"
            exit ${ERR_NO_SPACE}
        fi
        createImageWrap
        ;;
    "installbasesystem")
        installbasesystem
        ;;
esac


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

exit ${ERRORN}