forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·150 lines (125 loc) · 5.13 KB
/
install
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
#!/bin/sh
#
# This script is an entry point for a standalone installer.
# It is expected to probe for the destination installtion
# media and arrange for source files to be in /parts. Some
# of these files will be supplied from outside of the container
# in /bits, some will be constructed on the fly depending
# on settings that were passed via kernel command line:
# eve_install_disk
# eve_pause_before_install
# eve_pause_after_install
# eve_install_skip_config
# eve_install_skip_persist
# eve_install_skip_rootfs
#
[ -n "$DEBUG" ] && set -x
pause() {
echo "Pausing before $1. Entering shell. Type 'exit' to proceed to $1"
exit 0
}
bail() {
echo "$*"
exit 1
}
trunc() {
rm "$1"
touch "$1"
}
root_dev() {
local MAJOR
local MINOR
local DEV=$(readlink -f /dev/root 2>/dev/null)
if [ -z "$DEV" ] ; then
MAJOR=$(( $(stat -c '%d' /hostfs ) / 256))
MINOR=$(( $(stat -c '%d' /hostfs ) - $MAJOR * 256 ))
else
MAJOR=$(( 0x$(stat -c '%t' $DEV) + 0 ))
MINOR=$(( 0x$(stat -c '%T' $DEV) + 0 ))
fi
DEV_MM="$MAJOR:$MINOR"
echo $(grep -l '^'$DEV_MM'$' /sys/block/*/dev /sys/block/*/*/dev | cut -f4 -d/)
}
# find_part LABEL BLOCK_DEV
find_part() {
PARTS=$(lsblk -anl -o "NAME,PARTLABEL" 2>/dev/null | sed -ne "/$1"'$/s#'"$1##p")
for p in $PARTS ; do
[ -f "/sys/block/$2/$p/dev" ] && echo "$p" && exit 0
done
}
# if we are running without a real root filesystem - we have to fake it here
if [ ! -e /dev/root ]; then
# we are running off of an initrd or initramfs
# relying on init setting up mount points for us under /media
DEV_INIT="$(cd /media || exit 1 ; echo */rootfs.img | sed -e 's#/.*$##')"
ln -s "/media/$DEV_INIT" /bits
ln -s "/dev/$DEV_INIT" /dev/root
fi
# lets see if we're told on which disk to install...
INSTALL_DEV=`cat /proc/cmdline | tr ' ' '\012' | sed -ne '/^eve_install_disk=/s#^.*=##p'`
# ...if not we will try to guess, and...
if [ -z "$INSTALL_DEV" ] ; then
# now lets see what sources of installation material are there
FREE_DISKS=$(lsblk -anlb -o "TYPE,NAME,SIZE" | grep "^disk" | awk '$3 { print $2;}' | grep -v $(root_dev))
# if there's more than one free disk, install on the first one but warn about all of them
echo $FREE_DISKS | awk '{ if (NF > 1) { printf("WARNING: found multiple free disks %s, installing on the first one\n", $0); } }'
INSTALL_DEV=$(set ${FREE_DISKS:-""} ; echo $1)
fi
# ...if we didn't find a single free disk - bail
[ -z "$INSTALL_DEV" ] && bail "FATAL: didn't find a single free disk"
# now lets figure out whether we have installation material
mkdir /parts || :
CONFIG_PART=$(find_part CONFIG "$(root_dev)")
if [ -n "$CONFIG_PART" ] ; then
ln -s /dev/$CONFIG_PART /parts/config.img
fi
for i in config.img rootfs.img EFI ; do
SOURCE="/bits/$i"
[ -e "$SOURCE" ] || SOURCE=/dev/null
[ -L /parts/$i ] || ln -s "$SOURCE" /parts/$i
done
# finally lets see if we were given any overrides
grep -q eve_install_skip_rootfs /proc/cmdline && trunc /parts/rootfs.img
grep -q eve_install_skip_config /proc/cmdline && trunc /parts/config.img
# 1M of zeroes should be enough to trigger ext3 filesystem wipe
grep -q eve_install_skip_persist /proc/cmdline || dd if=/dev/zero of=/parts/persist.img bs=1M count=1 2>/dev/null
# we may be asked to pause before install procedure
grep -q eve_pause_before_install /proc/cmdline && pause "formatting the /dev/$INSTALL_DEV"
# FIXME: we need to run one thing from the install image to seed
# the random generator for sgdisk invocatin via make-raw. This
# needs to go away soon as part of the overall restructuring of
# /install vs. make-raw vs. /init
mkdir -p /mnt || :
mount -o ro /parts/rootfs.img /mnt &&
/mnt/containers/onboot/*rngd/rootfs/sbin/rngd -1
# do the install
/make-raw "/dev/$INSTALL_DEV" || bail "Installation failed. Entering shell..."
# generate a Soft Serial Number (if there isn't one)
# SERIAL_NUM=$(uuidgen | sed -e 's#^.*-##')
SERIAL_NUM=$(uuidgen)
INVENTORY_PART=$(find_part INVENTORY "$(root_dev)")
DEST_CONF_PART=$(find_part CONFIG "$INSTALL_DEV")
mkdir -p /run/conf /run/inventory && \
mount -t vfat -o iocharset=iso8859-1 "/dev/$INVENTORY_PART" /run/inventory && \
mount -t vfat -o iocharset=iso8859-1 "/dev/$DEST_CONF_PART" /run/conf && \
[ -f /run/conf/soft_serial ] || echo "$SERIAL_NUM" > /run/conf/soft_serial
PILLAR=/mnt/containers/services/pillar/lower
for i in dev proc sys ; do mount --bind /$i $PILLAR/$i ; done
SERIAL_NUM=$(cat /run/conf/soft_serial) && \
mkdir "/run/inventory/$SERIAL_NUM"
chroot "$PILLAR" /opt/zededa/bin/hardwaremodel -f > "/run/inventory/$SERIAL_NUM/hwfp.json"
chroot "$PILLAR" /opt/zededa/bin/hardwaremodel > "/run/inventory/$SERIAL_NUM/hardwaremodel.txt"
# lets hope this is enough to flush the caches
sync
umount /run/conf
umount /run/inventory
# we also maybe asked to pause after
grep -q eve_pause_after_install /proc/cmdline && pause "before shutting the node down"
sync
sleep 5
sync
# we need a copy of these in tmpfs so that a block device with rootfs can be yanked
cp /sbin/poweroff /bin/sleep /
echo "NOTICE: Device will now power off. Remove the USB stick and power it back on to complete the installation." >/dev/console
/sleep 5
/poweroff -f