aboutsummaryrefslogtreecommitdiffstats
path: root/platforms/freebox-delta/specialFunctions.sh
blob: 4386a54fe54eb8f2e2bcbdc446e7bc47af84e460 (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
#!/bin/bash

# Copyright (C) 2017-2018 Daniel Tartavel-jeannot   <contact@librepc.com>
# Copyright (C) 2019-2020 Jean-Baptiste Biernacki   <j.biernacki@free.fr>
#

# 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/>.


#Fdisk partitionment commands :
DEFAULT=""
NEW_DOSTAB="o"
NEW_PART="n"
PRIMARY="p"
CHANGE_TYPE="t"
TYPE_FAT32LBA="c"
CHANGE_BOOTFLAG="a"
APPLY="w"

# PART_BOOT=1
# PART_BOOT_FIRST_SECTOR=${DEFAULT}
# PART_BOOT_SIZE="+128M"
# 
# PART_ROOT=2
# PART_ROOT_FIRST_SECTOR=${DEFAULT}
# PART_ROOT_SIZE=${DEFAULT}

PART_BOOTandROOT="1"
PART_BOOTandROOT_FIRST_SECTOR="${DEFAULT}"
PART_BOOTandROOT_SIZE="${DEFAULT}"


# FDISK_SCRIPT="${NEW_PART}\n${PRIMARY}\n${PART_BOOTandROOT}\n${PART_BOOTandROOT_FIRST_SECTOR}\n${PART_BOOTandROOT_SIZE}\n${CHANGE_BOOTFLAG}\n${APPLY}\n"
p1_Start_sector="${DEFAULT}"
p1_Size="128M"
p1_Size=$((2*1024*128))
p1_Type="uefi"
#p1_Bootable="*"
p1_Bootable="${DEFAULT}"
p2_Start_sector="${DEFAULT}"
p2_Size="${DEFAULT}"
p2_Type="linux"
p2_Bootable="${DEFAULT}"
#SFDISK_SCRIPT=",128M,U,*\n,,L,\nwrite\n"
SFDISK_SCRIPT="label: gpt
${p1_Start_sector}, ${p1_Size}, ${p1_Type}, ${p1_Bootable}
${p2_Start_sector}, ${p2_Size}, ${p2_Type}, ${p2_Bootable}
write
"


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

    return 0
}


function loopingImage() {
    title "Looping image ..."

    # Mettre en place et contrôler des périphériques boucle.
    # -f, --find                    trouver le premier périphérique inutilisé
    # --show                    afficher le nom du périphérique après configuration (avec -f)
#     DEVICE=$(/usr/sbin/losetup -f --show "${INSTALL_PATH}/${IMAGE}")
    DEVICE=$(/usr/bin/udisksctl loop-setup -f ${INSTALL_PATH}/${IMAGE} | sed -e 's|^.*\/dev\/|\/dev\/|' -e 's|\.$||')
    if [ -z "${DEVICE}" ]; then
        error "Error, device missing from udisksctl loop-setup output."
        cleanExit ${ERR_1}
    fi

    print_info "Image looped on ${DEVICE}"
    
    
    #Previous function give us a list of partition. It is easy to get it and define prior this list the partition.
    #But... How to distinguish between the boot p1 and the root p2 if both are empty and ext4 ? ...

    if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
        BOOTP="${DEVICE}p1"
        ROOTP="${DEVICE}p2"
        
#         BOOT_UUID=$(/usr/sbin/blkid -s UUID -o value UUID "${BOOTP}")
        BOOT_UUID=$(/usr/bin/lsblk -n -o UUID "${BOOTP}")
        print_info "Boot UUID: ${BOOT_UUID}"
    else
        ROOTP="${DEVICE}p1"
    fi
    
#     ROOT_UUID=$(/usr/sbin/blkid -s UUID -o value UUID "${ROOTP}")
    ROOT_UUID=$(/usr/bin/lsblk -n -o UUID "${ROOTP}")
    print_info "Root UUID: ${ROOT_UUID}"
    
    return 0
}


function out_partitionningimage() {
    title "Partitionning image"
    warning "JE SUIS ICI DEDANS!"

    if  [ "x" != "${SFDISK_SCRIPT:="x"}" ]; then
        HDD_INFO="$(echo -e "${SFDISK_SCRIPT}" | /usr/sbin/sfdisk "${INSTALL_PATH}/${IMAGE}")"
        warning "JE check"
        /usr/sbin/sfdisk -V "${INSTALL_PATH}/${IMAGE}"
        warning "DONE"
        echo "${HDD_INFO}"
        SECTOR_SIZE="$(echo "${HDD_INFO}" | grep -m1 "Units: sectors" | sed -e 's/^.* \([[:digit:]]\+\) bytes[[:space:]]*$/\1/')"
        if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
            BOOT_P_INFO=$(echo "${HDD_INFO}" | grep -m1 -A3 "^Device" | grep "${IMAGE}1" | tr '*' ' ' | sed -e 's/[[:space:]]\+/ /g')
            ROOT_P_INFO=$(echo "${HDD_INFO}" | grep -m1 -A3 "^Device" | grep "${IMAGE}2" | tr '*' ' ' | sed -e 's/[[:space:]]\+/ /g')
            BOOT_OFFSET_S="$(echo "${BOOT_P_INFO}" | sed -e "s/^.*${IMAGE}[[:digit:]][[:space:]\*]\+//g" | cut -d ' ' -f 1)"
            BOOT_NB_SECTORS=$(echo "${BOOT_P_INFO}" | sed -e "s/^.*${IMAGE}[[:digit:]][[:space:]\*]\+//g" | cut -d ' ' -f 3)
            BOOT_SIZE_KB="$((${SECTOR_SIZE}*${BOOT_NB_SECTORS}/1024))"
            BOOT_SIZE_B="$((${SECTOR_SIZE}*${BOOT_NB_SECTORS}))"
            BOOT_OFFSET_KB="$((${SECTOR_SIZE}*${BOOT_OFFSET_S}/1024))"
            print_info "Boot offset : ${BOOT_OFFSET_S}"
            print_info "Boot offset (kB) : ${BOOT_OFFSET_KB}"
            print_info "Boot sectors : ${BOOT_NB_SECTORS}"
            print_info "Boot size (kB) : ${BOOT_SIZE_KB}"
            print_info "Boot size (B) : ${BOOT_SIZE_B}"
        else
            ROOT_P_INFO=$(echo "${HDD_INFO}" | grep -m1 -A3 "^Device" | grep "${IMAGE}1")
        fi
        ROOT_OFFSET_S=$(echo "${ROOT_P_INFO}" | sed -e "s/^.*${IMAGE}[[:digit:]][[:space:]\*]\+//g" | cut -d ' ' -f 1)
        ROOT_NB_SECTORS=$(echo "${ROOT_P_INFO}" | sed -e "s/^.*${IMAGE}[[:digit:]][[:space:]\*]\+//g" | cut -d ' ' -f 3)
        ROOT_SIZE_KB="$((${SECTOR_SIZE}*${ROOT_NB_SECTORS}/1024))"
        ROOT_OFFSET_B="$((${SECTOR_SIZE}*${ROOT_OFFSET_S}))"
        
        print_info "Root offset : ${ROOT_OFFSET_S}"
        print_info "Root offset (B) : ${ROOT_OFFSET_B}"
        print_info "Root sectors : ${ROOT_NB_SECTORS}"
        print_info "Root size kB : ${ROOT_SIZE_KB}"
    else
        error "Error, no partitionning script found."
	cleanExit ${ERR_1}
    fi

    return 0
}

function formatpartitions() {
    print_info "Formatting partitions"
    if [ 1 -eq ${SEPARATE_BOOT_PARTITION} ]; then
        print_info "Boot : ${BOOTP} as ${1}"
        if [ "vfat" = ${1} ]; then
            FSINFO=$(/usr/sbin/mkfs.vfat ${BOOTP} )
            BOOT_UUID=$(echo "${FSINFO}" | grep 'Volume ID is' | sed 's/Volume ID is \([[:alnum:]]*\),.*$/\1/')
            print_info "Boot UUID = ${BOOT_UUID}"
            rm -f ${temporary}
        else
            FSINFO=$(yes | /usr/sbin/mkfs.ext4 -E offset=${BOOT_OFFSET_B} ${INSTALL_PATH}/${IMAGE} $((${BOOT_SIZE_KB}))k)
            BOOT_UUID=$(echo "${FSINFO}" | grep 'Filesystem UUID' | sed 's/Filesystem UUID: //')
        fi
#         "/sbin/mkfs.${1}" "${BOOTP}"
        if [ ${?} -ne 0 ]; then
            error "line ${LINENO} error formating ${BOOTP} : exiting"
#             /usr/sbin/losetup -d "${DEVICE}"
            udisksctl unmount -b ${BOOTP}
            cleanExit ${ERR_1}
        fi
#         BOOT_UUID=$(/usr/sbin/blkid -s UUID -o value UUID "${BOOTP}")
#         print_info "Boot UUID: ${BOOT_UUID}"
    fi

    print_info "Root : ${ROOTP} as ${2}"
    FSINFO=$(yes | /sbin/mkfs.${2} "${ROOTP}" )
    if [ ${?} -ne 0 ]; then
        error "line ${LINENO} error formating ${ROOTP} : exiting"
        cleanExit ${ERR_1}
    fi
    ROOT_UUID=$(echo "${FSINFO}" | grep 'Filesystem UUID' | sed 's/Filesystem UUID: //')
#     ROOT_UUID=$(/usr/sbin/blkid -s UUID -o value UUID "${ROOTP}")
    print_info "Root UUID: ${ROOT_UUID}"

    /usr/bin/sync
    
    return 0
}

function imageConversion()
{
    title "Converting image into a qcow2 file"
    
    qemu-img convert -O qcow2 ${INSTALL_PATH}/${IMAGE} ${INSTALL_PATH}/${IMAGE}.qcow2
    return 0
}

function preImgCreation()
{
#     title "Creating Banana Pro image"
    #Nothing to download.
    return 0
}


function postPrepareChroot()
{
    #Nothing to do.
    return 0
}

function burningBootloader()
{
    # DEVICE is set just before this function and looped.
    return 0
}

function copyingCustomSystem()
{
#     installpkg ${CONFIG_PATH}/package.mga7.aarch64.rpm
#     installpkg package
    return 0
}