summaryrefslogtreecommitdiff
path: root/installer/install.sh
blob: f01f00efcb60dbd216ebfc8e9bc959e6d05f76be (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
#!/bin/sh
read_blk_dev()
{
    kernel_name_descriptor=$(blkid | grep ONIE-BOOT | awk -F ":" '{print $1}' | awk -F "/" '{print $NF}')
    major_device_number=$(cat /proc/partitions | grep ${kernel_name_descriptor} | awk '{print $1}')
    disk_device_name=$(cat /proc/partitions | awk -v major="$major_device_number" '$1 == major && $2 == "0" {print $4}')
    echo "/dev/${disk_device_name}"
}

blk_dev=$(read_blk_dev)
echo "TSG-OS install on ${blk_dev}"
random_dev=/dev/urandom
root_disk=hd0
distro_part=4
distro_dev="${blk_dev}${distro_part}"
distro_mnt=/mnt/distro
onie_root_dir=/mnt/onie-boot/onie

kernel_args="%%KERNAL_ARGS%%"
kernel_args_device_9000="%%KERNEL_ARGS_9000_NPB%%"
grub_serial_command="%%GRUB_SERIAL_COMMAND%%"
DISTR0_VER="%%DISTR0_VER%%"
MACHINE_ID="%%MACHINE_ID%%"
CHROOT_PKG="%%CHROOT_PKG%%"
APP_BUNDLE_BIN="%%APP_BUNDLE_BIN%%"
HELM_CHART_TAR_GLOB="%%HELM_CHART_TAR_GLOB%%"
CONTAINER_IMAGES_TAR_GLOB="%%CONTAINER_IMAGES_TAR_GLOB%%"

size_part_tsg_os_update="%%SIZE_PART_UPDATE%%"
size_part_tsg_os_sysroot="%%SIZE_PART_SYSROOT%%"
size_part_tsg_os_data="%%SIZE_PART_DATA%%"

vol_label_tsg_os_update="TSG-OS-UPDATE"
vol_label_tsg_os_sysroot="TSG-OS-SYSROOT"
vol_label_tsg_os_data="TSG-OS-DATA"

exp_part_tsg_os_update=3
exp_part_tsg_os_sysroot=4
exp_part_tsg_os_data=5

dev_part_tsg_os_update="${blk_dev}${exp_part_tsg_os_update}"
dev_part_tsg_os_sysroot="${blk_dev}${exp_part_tsg_os_sysroot}"
dev_part_tsg_os_data="${blk_dev}${exp_part_tsg_os_data}"

tsg_os_efi_prefix="TSG-OS"
tsg_os_efi_label="TSG-OS: ${DISTR0_VER} (${MACHINE_ID})"

# auto-detect whether BIOS or UEFI
if [ -d "/sys/firmware/efi/efivars" ] ; then
    firmware="uefi"
else
    firmware="bios"
fi

check_is_upgrade()
{
  part_tsg_os_update=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_update" | awk '{print $1}')
  part_tsg_os_sysroot=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_sysroot" | awk '{print $1}')
  part_tsg_os_data=$(sgdisk -p $blk_dev | grep -e "$vol_label_tsg_os_data" | awk '{print $1}')

  if [ "$part_tsg_os_update"  != "$exp_part_tsg_os_update" ]; then return 1
  elif [ "$part_tsg_os_sysroot" != "$exp_part_tsg_os_sysroot" ]; then return 1
  elif [ "$part_tsg_os_data" != "$exp_part_tsg_os_data" ]; then return 1
  fi
  return 0
}

format_update_partitions() {
  mkfs.ext4 -L $vol_label_tsg_os_update $dev_part_tsg_os_update || {
    echo "ERROR: Unable to create file system on \$dev_part_tsg_os_update"
    exit 1
  }
}

format_sysroot_partitions() {
  mkfs.ext4 -L $vol_label_tsg_os_sysroot $dev_part_tsg_os_sysroot || {
    echo "ERROR: Unable to create file system on \$dev_part_tsg_os_sysroot"
    exit 1
  }
}

format_presistent_partitions() {
  mkfs.ext4 -L $vol_label_tsg_os_data $dev_part_tsg_os_data || {
    echo "ERROR: Unable to create file system on \$dev_part_tsg_os_data"
    exit 1
  }
}

fix_fs_volume_name() {
    local dev_part=$1
    local desired_fs_volume_name=$2

    current_fs_volume_name=$(tune2fs -l $dev_part | grep 'Filesystem volume name' | awk '{print $4}')
    if [ "$current_fs_volume_name" != "$desired_fs_volume_name" ]; then
        tune2fs -L $desired_fs_volume_name $dev_part ||  {
            echo "ERROR: Failed to update $dev_part volume name from $current_fs_volume_name to $desired_fs_volume_name"
            exit 1
        }
    fi
}

create_gpt_partitions() {
  # remove old partitions
  for p in $(seq 3 9) ; do
    umount -f $blk_dev$p  > /dev/null 2&>1
    sgdisk -d $p $blk_dev > /dev/null 2&>1
  done

  partprobe ${blk_dev}

  echo "Create partition TSG-OS-UPDATE."
  begin=$(sgdisk -F $blk_dev)
  sgdisk --new=${exp_part_tsg_os_update}:${begin}:+${size_part_tsg_os_update} \
         --change-name=${exp_part_tsg_os_update}:"TSG-OS-UPDATE" ${blk_dev} || {
        echo "ERROR: Unable to create partition TSG-OS-UPDATE on $blk_dev"
        exit 1
  }

  echo "Create partition TSG-OS-SYSROOT."
  begin=$(sgdisk -F $blk_dev)
  sgdisk --new=${exp_part_tsg_os_sysroot}:${begin}:+${size_part_tsg_os_sysroot} \
         --change-name=${exp_part_tsg_os_sysroot}:"TSG-OS-SYSROOT" ${blk_dev} || {
        echo "ERROR: Unable to create partition TSG-OS-SYSROOT on $blk_dev"
        exit 1
  }

  echo "Create partition TSG-OS-DATA."
  begin=$(sgdisk -F $blk_dev)
  end=$(sgdisk -E $blk_dev)

  sgdisk --new=${exp_part_tsg_os_data}:${begin}:${end} \
         --change-name=${exp_part_tsg_os_data}:"TSG-OS-DATA" ${blk_dev} || {
        echo "ERROR: Unable to create partition TSG-OS-DATA on $blk_dev"
        exit 1
  }

  partprobe ${blk_dev}
}

# Install legacy BIOS GRUB for DEMO OS
install_grub()
{
    local demo_mnt="$1"
    local blk_dev="$2"

    # get running machine from conf file
    [ -r /etc/machine.conf ] && . /etc/machine.conf

    local grub_target="i386-pc"

    grub-install --target="$grub_target" \
        --boot-directory="$demo_mnt" --recheck "$blk_dev" || {
        echo "ERROR: grub-install failed on: $blk_dev"
        exit 1
    }
}

# Install UEFI BIOS GRUB for DEMO OS
install_uefi_grub()
{
    local demo_mnt="$1"
    local blk_dev="$2"

    # get running machine from conf file
    [ -r /etc/machine.conf ] && . /etc/machine.conf

    # Look for the EFI system partition UUID on the same block device as
    # the ONIE-BOOT partition.
    local uefi_part=0
    for p in $(seq 8) ; do
        if sgdisk -i $p $blk_dev | grep -q C12A7328-F81F-11D2-BA4B-00A0C93EC93B ; then
            uefi_part=$p
            break
        fi
    done

    [ $uefi_part -eq 0 ] && {
        echo "ERROR: Unable to determine UEFI system partition"
        exit 1
    }

    # erase any related EFI BootOrder variables from NVRAM.
    for b in $(efibootmgr | grep "$tsg_os_efi_prefix" | awk '{ print $1 }') ; do
        local num=${b#Boot}
        # Remove trailing '*'
        num=${num%\*}
        efibootmgr -b $num -B > /dev/null 2>&1
    done

    # Regular GRUB install
    grub_install_log=$(mktemp)
    grub-install \
        --no-nvram \
        --bootloader-id="$tsg_os_efi_prefix" \
        --efi-directory="/boot/efi" \
        --boot-directory="$demo_mnt" \
        --recheck \
        "$blk_dev" > /$grub_install_log 2>&1 || {
        echo "ERROR: grub-install failed on: $blk_dev"
        cat $grub_install_log && rm -f $grub_install_log
        exit 1
    }
    rm -f $grub_install_log

    # Configure EFI NVRAM Boot variables.  --create also sets the
    # new boot number as active.
    efibootmgr --quiet --create \
        --label "$tsg_os_efi_label" \
        --disk $blk_dev --part $uefi_part \
        --loader "/EFI/$tsg_os_efi_prefix/$onie_uefi_boot_loader" || {
        echo "ERROR: efibootmgr failed to create new boot variable on: $blk_dev"
        exit 1
    }

    # Delete onie UEFI bootnum if machine_id is aarch64_COTS
    if [ ${MACHINE_ID} == "aarch64_COTS" ];then
        boot_num=$(efibootmgr -v| grep "ONIE:" | grep "HD" | awk '{ print $1 }')
        boot_num=${boot_num#Boot}
        # Remove trailing '*'
        boot_num=${boot_num%\*}
        if [ -n "$boot_num" ] ; then
            efibootmgr --quiet --bootnum $boot_num --delete-bootnum  && return
            echo "ERROR: Problems delete ONIE UEFI Bootnum variable"
        else 
            echo "INFO: ONIE UEFI Bootnum is not exist"
        fi
    fi
}


# patch install.
DEVICE_TYPE=
read_device_type()
{
    product_name=`ipmitool fru list | grep 'Product Name' | awk '{print $4}' | head -n 1`

    case ${product_name} in
        "ACB300-040-00" | "9000-NPB-P01R01")
            DEVICE_TYPE="9000-NPB"
            ;;
        *)
        ;;
    esac
}

install_patches()
{
    if [ ${DEVICE_TYPE} == "9000-NPB" ]; then
        chmod 0755 ./tsg-os_9000-NPB-P01R01_patch.bin
        cp ./tsg-os_9000-NPB-P01R01_patch.bin ${distro_mnt}/tmp
        chroot ${distro_mnt} /tmp/tsg-os_9000-NPB-P01R01_patch.bin
        rm ${distro_mnt}/tmp/tsg-os_9000-NPB-P01R01_patch.bin
    fi
}

append_kernel_args()
{
    if [ ${DEVICE_TYPE} == "9000-NPB" ]; then
        kernel_args="${kernel_args} ${kernel_args_device_9000}"
    fi
}

remove_onie_swap()
{
    data_part_mount_path="/tmp/data_mount_path"
    if cat /proc/swaps | grep -q "/dev/loop0"; then
        swapoff /dev/loop0
        losetup -d /dev/loop0
        rm -rf ${data_part_mount_path}/.onie_swapfile
    fi
}

install_onie_swap()
{
    data_part_mount_path="/tmp/data_mount_path"
    mkdir -p ${data_part_mount_path}

    mount ${dev_part_tsg_os_data} ${data_part_mount_path}

    remove_onie_swap ${data_part_mount_path}

    dd if=/dev/zero of=${data_part_mount_path}/.onie_swapfile bs=32M count=1024

    if [ ! -e /dev/loop0 ]; then
        mknod /dev/loop0 b 7 0
    fi

    losetup /dev/loop0 ${data_part_mount_path}/.onie_swapfile

    mkswap /dev/loop0
    swapon /dev/loop0
}

############################### START AT HERE #########################################
lib_dir="/lib/onie"
. $lib_dir/onie-blkdev-common

cd $(dirname $0)

if check_is_upgrade; then
   echo "Upgrading TSG-OS, the config and data partitions will be keeped in $blk_dev."

   format_sysroot_partitions || {
       echo "ERROR: Unable to format sysroot partition on $blk_dev, TSG-OS install failed."
       exit 1
   }

else
   echo "Installing TSG-OS, all partitions on $blk_dev will be erased."

   create_gpt_partitions || {
       echo "ERROR: Unable to create partitions on $blk_dev, TSG-OS install failed."
       exit 1
   }

   format_sysroot_partitions || {
       echo "ERROR: Unable to format sysroot partition on $blk_dev, TSG-OS install failed."
       exit 1
   }

   format_presistent_partitions || {
       echo "ERROR: Unable to format presistent partitions on $blk_dev, TSG-OS install failed."
       exit 1
   }

    format_update_partitions || {
       echo "ERROR: Unable to format update partitions on $blk_dev, TSG-OS install failed."
       exit 1
   }
fi

fix_fs_volume_name $dev_part_tsg_os_update $vol_label_tsg_os_update

# Mount filesystem
mkdir -p $distro_mnt || {
    echo "ERROR: Unable to create distro file system mount point: \$distro_mnt"
    exit 1
}

mount -t ext4 -o defaults,rw $distro_dev $distro_mnt || {
    echo "ERROR: Unable to mount $distro_dev on $distro_mnt"
    exit 1
}

# bonk out on errors
set -ex

install_onie_swap
cp -f distro-setup.sh ${distro_mnt}
echo "Extract chroot environment ..."
tar -xf ${CHROOT_PKG} -C ${distro_mnt}

[ -e ${distro_mnt}/dev/pts ] && {
    mount -o bind /dev/pts \${distro_mnt}/dev/pts
}

mount -t proc proc ${distro_mnt}/proc
mount -t sysfs sys ${distro_mnt}/sys
cp -a ${blk_dev} ${distro_mnt}/${blk_dev}
cp -a ${random_dev} ${distro_mnt}/${random_dev}

mkdir -p ${distro_mnt}/data
mount -t ext4 ${blk_dev}${exp_part_tsg_os_data} ${distro_mnt}/data

echo "Setting up TSG-OS sysroot .."
chroot ${distro_mnt} /distro-setup.sh ${blk_dev}
rm -f ${distro_mnt}/distro-setup.sh
read_device_type
install_patches
append_kernel_args

[ -e ${distro_mnt}/dev/pts ] && {
   umount ${distro_mnt}/dev/pts
}

if [ -f ${APP_BUNDLE_BIN} ]; then
    mkdir -p ${distro_mnt}/data/var/lib/rancher/k3s/agent/images
    mkdir -p ${distro_mnt}/data/var/lib/rancher/k3s/server/static/charts
    rm -rf ${distro_mnt}/data/var/lib/cni
    rm -rf ${distro_mnt}/data/var/lib/rancher/k3s/agent/images/${CONTAINER_IMAGES_TAR_GLOB}
    rm -rf ${distro_mnt}/data/var/lib/rancher/k3s/server/static/charts/${HELM_CHART_TAR_GLOB}
    chmod 0755 ${APP_BUNDLE_BIN}
    export APP_BUNDLE_CHARTS_DEST_DIR="${distro_mnt}/data/var/lib/rancher/k3s/server/static/charts"
    export APP_BUNDLE_IMAGES_DEST_DIR="${distro_mnt}/data/var/lib/rancher/k3s/agent/images"
    export APP_BUNDLE_MANIFESTS_DEST_DIR="${distro_mnt}/data/var/lib/rancher/k3s/server/manifests"
    export APP_BUNDLE_CLIXON_CONF_PATH="${distro_mnt}/opt/tsg/clixon/etc/mgnt-srv.conf"
    export APP_BUNDLE_LOAD_IMAGES_SWITCH="off"
    ./${APP_BUNDLE_BIN}
fi

rm -rf ${distro_mnt}/data/vdisks

umount ${distro_mnt}/proc
umount ${distro_mnt}/sys
umount ${distro_mnt}/data

# Install boot loader
echo "Install GRUB2 bootloader .."

if [ "$firmware" = "uefi" ] ; then
    install_uefi_grub "${distro_mnt}/boot" "$blk_dev"
else
    install_grub "${distro_mnt}/boot" "$blk_dev"
fi

# Prepare boot loader configuration file
grub_cfg=$(mktemp)

# Add common configuration, like the timeout and serial console.
(cat <<EOF2

${grub_serial_command}
terminal_input serial
terminal_output serial
set timeout=5

# Add the logic to support grub-reboot
if [ -s \$prefix/grubenv ]; then
  load_env
fi
if [ "\${saved_entry}" ] ; then
   set default="\${saved_entry}"
   set saved_entry=
   save_env saved_entry
fi

if [ "\${next_entry}" ] ; then
   set default="\${next_entry}"
   set next_entry=
   save_env next_entry
fi

EOF2
) >> ${grub_cfg}


# Add a menu entry for o/s distro
(cat <<EOF3
menuentry 'TSG-OS ${DISTR0_VER} (${MACHINE_ID})' {
  set root='(${root_disk},gpt${distro_part})'
  search --no-floppy --label --set=root ${vol_label_tsg_os_sysroot}
  echo    'Loading TSG-OS ${DISTR0_VER} (${MACHINE_ID}) ...'
  linux   /boot/vmlinuz ${kernel_args} root="LABEL=${vol_label_tsg_os_sysroot}"
  echo    'Loading TSG-OS ${DISTR0_VER} (${MACHINE_ID}) initial ramdisk ...'
  initrd  /boot/initrd.img
 }
EOF3
) >> ${grub_cfg}


# Add menu entries for ONIE -- use the grub fragment provided by the
# ONIE distribution.
$onie_root_dir/grub.d/50_onie_grub >> ${grub_cfg}

# Copy boot loader config to appropriate location
cp -f ${grub_cfg} ${distro_mnt}/boot/grub/grub.cfg
cat ${grub_cfg} >> ${distro_mnt}/etc/grub.d/40_onie_grub

umount ${distro_mnt}
remove_onie_swap
cd /

echo "TSG-OS install successfully, need to reboot."
exit 0