keep nc, pkill and killall on miniroot
[unleashed-kayak.git] / install_help.sh
blob772c1f78a8f540bd3c0b7187588db1b92a14f997
1 #!/usr/bin/bash
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License, Version 1.0 only
7 # (the "License"). You may not use this file except in compliance
8 # with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
21 # CDDL HEADER END
24 # Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved.
25 # Use is subject to license terms.
27 LOG_SETUP=0
29 ConsoleLog(){
30 exec 4>/dev/console
31 exec 1>>${1}
32 exec 2>>${1}
33 INSTALL_LOG=${1}
34 LOG_SETUP=1
36 CopyInstallLog(){
37 if [[ -n "$INSTALL_LOG" ]]; then
38 cp $INSTALL_LOG $ALTROOT/var/log/install/kayak.log
41 SendInstallLog(){
42 PUTURL=`echo $CONFIG | sed -e 's%/kayak/%/kayaklog/%g;'`
43 PUTURL=`echo $PUTURL | sed -e 's%/kayak$%/kayaklog%g;'`
44 curl -T $INSTALL_LOG $PUTURL/$ETHER
46 OutputLog(){
47 if [[ "$LOG_SETUP" -eq "0" ]]; then
48 exec 4>/dev/null
49 LOG_SETUP=1
52 log() {
53 OutputLog
54 TS=`date +%Y/%m/%d-%H:%M:%S`
55 echo "[$TS] $*" 1>&4
56 echo "[$TS] $*"
58 bomb() {
59 log
60 log ======================================================
61 log "$*"
62 log ======================================================
63 if [[ -n "$INSTALL_LOG" ]]; then
64 log "For more information, check $INSTALL_LOG"
65 log ======================================================
67 exit 1
70 . /kayak/net_help.sh
71 . /kayak/disk_help.sh
73 ICFILE=/tmp/_install_config
74 getvar(){
75 prtconf -v /devices | sed -n '/'$1'/{;n;p;}' | cut -f2 -d\'
78 # Blank
79 ROOTPW='$5$kr1VgdIt$OUiUAyZCDogH/uaxH71rMeQxvpDEY2yX.x0ZQRnmeb9'
80 RootPW(){
81 ROOTPW="$1"
83 SetRootPW(){
84 sed -i '' -e 's%^root::%root:'$ROOTPW':%' $ALTROOT/etc/shadow
86 ForceDHCP(){
87 log "Forcing all interfaces into DHCP..."
88 /sbin/ifconfig -a plumb 2> /dev/null
89 # for the logs
90 for iface in `/sbin/dladm show-phys -o device -p` ; do
91 /sbin/ifconfig $iface dhcp &
92 done
93 while [[ -z $(/sbin/dhcpinfo BootSrvA) ]]; do
94 log "Waiting for dhcpinfo..."
95 sleep 1
96 done
97 BOOTSRVA=`/sbin/dhcpinfo BootSrvA`
98 log "Next server: $BOOTSRVA"
99 sleep 1
102 BuildBE() {
103 RPOOL=${1:-rpool}
104 if [[ -z $2 ]]; then
105 BOOTSRVA=`/sbin/dhcpinfo BootSrvA`
106 MEDIA=`getvar install_media`
107 MEDIA=`echo $MEDIA | sed -e "s%//\:%//$BOOTSRVA\:%g;"`
108 MEDIA=`echo $MEDIA | sed -e "s%///%//$BOOTSRVA/%g;"`
109 DECOMP="bzip2 -dc"
110 GRAB="curl -s"
111 else
112 # ASSUME $2 is a file path.
113 MEDIA=$2
114 # XXX KEBE SAYS, make switch statement based on $MEDIA's extension.
115 # e.g. "bz2" ==> "bzip -dc", "7z" ==>
116 DECOMP="bzip2 -dc"
117 GRAB=cat
119 zfs set compression=on $RPOOL
120 zfs create $RPOOL/ROOT
121 zfs set canmount=off $RPOOL/ROOT
122 zfs set mountpoint=legacy $RPOOL/ROOT
123 log "Receiving image: $MEDIA"
124 $GRAB $MEDIA | pv -B 128m -w 78 | $DECOMP | zfs receive -u $RPOOL/ROOT/unleashed
125 zfs set canmount=noauto $RPOOL/ROOT/unleashed
126 zfs set mountpoint=legacy $RPOOL/ROOT/unleashed
127 log "Cleaning up boot environment"
128 beadm mount unleashed /mnt
129 ALTROOT=/mnt
130 cp $ALTROOT/lib/svc/seed/global.db $ALTROOT/etc/svc/repository.db
131 chmod 0600 $ALTROOT/etc/svc/repository.db
132 chown root:sys $ALTROOT/etc/svc/repository.db
133 /usr/sbin/devfsadm -r /mnt
134 [[ -L $ALTROOT/dev/msglog ]] || \
135 ln -s ../devices/pseudo/sysmsg@0:msglog $ALTROOT/dev/msglog
136 MakeSwapDump
137 zfs destroy $RPOOL/ROOT/unleashed@kayak
140 FetchConfig(){
141 ETHER=`Ether`
142 BOOTSRVA=`/sbin/dhcpinfo BootSrvA`
143 CONFIG=`getvar install_config`
144 CONFIG=`echo $CONFIG | sed -e "s%//\:%//$BOOTSRVA\:%g;"`
145 CONFIG=`echo $CONFIG | sed -e "s%///%//$BOOTSRVA/%g;"`
146 L=${#ETHER}
147 while [[ "$L" -gt "0" ]]; do
148 URL="$CONFIG/${ETHER:0:$L}"
149 log "... trying $URL"
150 /bin/curl -s -o $ICFILE $URL
151 if [[ -f $ICFILE ]]; then
152 if [[ -n $(grep BuildRpool $ICFILE) ]]; then
153 log "fetched config."
154 return 0
156 rm -f $ICFILE
158 L=$(($L - 1))
159 done
160 return 1
163 MakeBootable(){
164 RPOOL=${1:-rpool}
165 log "Making boot environment bootable"
166 zpool set bootfs=$RPOOL/ROOT/unleashed rpool
167 # Must do beadm activate first on the off chance we're bootstrapping from
168 # GRUB.
169 beadm activate unleashed || return 1
171 if [[ ! -z $1 ]]; then
172 # Generate kayak-disk-list from zpool status.
173 # NOTE: If this is something on non-s0 slices, the installboot below
174 # will fail most likely, which is possibly a desired result.
175 zpool list -v $RPOOL | egrep -v "NAME|rpool|mirror" | \
176 awk '{print $1}' | sed -E 's/s0$//g' > /tmp/kayak-disk-list
179 # NOTE: This installboot loop assumes we're doing GPT whole-disk rpools.
180 for i in `cat /tmp/kayak-disk-list`
182 installboot -mfF /boot/pmbr /boot/gptzfsboot /dev/rdsk/${i}s0 || return 1
183 done
185 bootadm update-archive -R $ALTROOT || return 1
186 return 0
189 SetHostname()
191 log "Setting hostname: ${1}"
192 /bin/hostname "$1"
193 echo "$1" > $ALTROOT/etc/nodename
194 head -n 26 $ALTROOT/etc/hosts > /tmp/hosts
195 echo "::1\t\t$1" >> /tmp/hosts
196 echo "127.0.0.1\t$1" >> /tmp/hosts
197 cat /tmp/hosts > $ALTROOT/etc/hosts
200 AutoHostname() {
201 suffix=$1
202 macadr=`/sbin/ifconfig -a | \
203 /usr/bin/awk '/UP/{if($2 !~ /LOOPBACK/){iface=$1;}} /ether/{if(iface){print $2; exit;}}' | \
204 /bin/tr '[:upper:]' '[:lower:]' | \
205 /bin/sed -e 's/^/ 0/g;s/:/-0/g; s/0\([0-9a-f][0-9a-f]\)/\1/g; s/ //g;'`
206 [ -z $suffix ] && suffix=unleashed
207 [ "$suffix" == "-" ] && suffix= || suffix=-$suffix
208 SetHostname $macadr$suffix
211 SetTimezone()
213 log "Setting timezone: ${1}"
214 sed -i '' -e "s:^TZ=.*:TZ=${1}:" $ALTROOT/etc/default/init
217 SetLang()
219 log "Setting language: ${1}"
220 sed -i '' -e "s:^LANG=.*:LANG=${1}:" $ALTROOT/etc/default/init
223 ApplyChanges(){
224 SetRootPW
225 [[ -L $ALTROOT/etc/svc/profile/generic.xml ]] || \
226 ln -s generic_limited_net.xml $ALTROOT/etc/svc/profile/generic.xml
227 [[ -L $ALTROOT/etc/svc/profile/name_service.xml ]] || \
228 ln -s ns_dns.xml $ALTROOT/etc/svc/profile/name_service.xml
230 # Extras from interactive ISO/USB install...
231 # arg1 == hostname
232 if [[ ! -z $1 ]]; then
233 SetHostname $1
236 # arg2 == timezone
237 if [[ ! -z $2 ]]; then
238 SetTimezone $2
241 # arg3 == Language
242 if [[ ! -z $3 ]]; then
243 SetLang $3
246 # arg4 == Keyboard layout
247 if [[ ! -z $4 ]]; then
248 # Even though /etc/default/kbd is now defunct, we instead use it
249 # For now, use it as the channel from the main menu to here, the
250 # installer. Extract LAYOUT=<foo> and put it in
251 # "setprop keyboard-layout <foo>" in the newly-installed root's
252 # /boot/solaris/bootenv.rc (aka. eeprom(1M) storage for amd64/i386).
253 layout=$4
254 sed "s/keyboard-layout Unknown/keyboard-layout $layout/g" \
255 < $ALTROOT/boot/solaris/bootenv.rc > /tmp/bootenv.rc
256 mv /tmp/bootenv.rc $ALTROOT/boot/solaris/bootenv.rc
257 # Also modify the SMF manifest, assuming US-English was set by default.
258 sed "s/US-English/$layout/g" \
259 < $ALTROOT/lib/svc/manifest/system/keymap.xml > /tmp/keymap.xml
260 cp -f /tmp/keymap.xml $ALTROOT/lib/svc/manifest/system/keymap.xml
263 return 0
266 Postboot() {
267 [[ -f $ALTROOT/.initialboot ]] || touch $ALTROOT/.initialboot
268 echo "$*" >> $ALTROOT/.initialboot
271 Reboot() {
272 # This is an awful hack... we already setup bootadm
273 # and we've likely deleted enough of the userspace that this
274 # can't run successfully... The easiest way to skip it is to
275 # remove the binary
276 rm -f /sbin/bootadm
277 svccfg -s "system/boot-config:default" setprop config/fastreboot_default=false
278 svcadm refresh svc:/system/boot-config:default
279 reboot
282 RunInstall(){
283 FetchConfig || bomb "Could not fetch kayak config for target"
284 # Set RPOOL if it wasn't done so already. We need it set.
285 RPOOL=${RPOOL:-rpool}
286 . $ICFILE
287 Postboot 'exit $SMF_EXIT_OK'
288 ApplyChanges || bomb "Could not apply all configuration changes"
289 MakeBootable || bomb "Could not make new BE bootable"
290 log "Install complete"
291 return 0