slicefix 6.25 KB
Newer Older
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
#!/bin/sh
#
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

if [ -r /etc/emulab/paths.sh ]; then
	. /etc/emulab/paths.sh
else
	BINDIR=/etc/testbed
	BOOTDIR=/var/emulab/boot
	ETCDIR=/etc/emulab
fi

33 34 35 36 37
MFS_DEBUG=${MFS_DEBUG:-'n'}
if [ "$MFS_DEBUG" = "y" ]; then
    set -x
fi

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
disk="sda"

case $# in
1)
	part=$1
	;;
2)
	part=$1
	disk=$2
	;;
*)
	echo "Usage: $0 partition [disk]"
	exit 1
esac

#
# Handle pseudo arguments in the environment.
#

#
# Find out what our console should be:
#   if $CONSOLE is set and valid, use that
#   ow console is "sio1"
#
# XXX on Moonshots, only "sio1" is supported.
#
Mike Hibler's avatar
Mike Hibler committed
64 65
CONSOLE=sio1
#CONSOLE=${SLICEFIX_CONSOLE:-'sio1'}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
case $CONSOLE in
sio|sio1)
    ;;
*)
    echo "WARNING: unknown console type \"$CONSOLE\", assuming \"sio1\""
    ;;
esac

# DOM0MEM is optional
DOM0MEM=$SLICEFIX_DOM0MEM

if [ -r $ETCDIR/bossnode ]; then
    bossnode=`cat $ETCDIR/bossnode`;
    setboss=1
else
    setboss=0
fi

Mike Hibler's avatar
Mike Hibler committed
84 85 86 87 88 89 90 91 92 93 94
# TRIM is optional
TRIM=${SLICEFIX_TRIM:-'0'}
case $TRIM in
0|1)
    ;;
*)
    echo "WARNING: unexpected TRIM argument \"$TRIM\", ignored"
    TRIM=0
    ;;
esac

95 96 97
# XXX hack to determine the root FS partition
RPART=$SLICEFIX_RPART

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
# Indicates the MFS has been properly localized and we can copy stuff
# into the image.
LOCALIZED1="/.localized"
LOCALIZED2="/root/.localized"

islocalized() {
    if [ -e $LOCALIZED1 -o -e $LOCALIZED2 ]; then
	return 0;
    fi
    return 1;
}

#
# Get the last setting of a loader variable from the list of standard files
#
getloadervar() {
    _var=$1
    _mnt=$2
    _flist="$_mnt/boot/defaults/loader.conf $_mnt/boot/device.hints $_mnt/boot/loader.conf $_mnt/boot/loader.conf.local"

    _last=`grep "^${_var}=" $_flist 2>/dev/null | tail -1`
    _val=`echo ${_last#*=} | sed -e 's/^"//' -e 's/"$//'`
    echo $_val
}

dolinux() {
    isroot=0

126
    if [ $part -eq $RPART ]; then
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
	isroot=1
    fi

    #
    # Turn off time-dependent fsck.
    #
    check=`dumpe2fs /dev/${disk}${part} 2>/dev/null | \
	grep 'Check interval:' | \
	sed -e 's/.*Check interval:  *\([0-9]*\).*/\1/'`
    if [ $check -ne 0 ]; then
	echo "  disabling time-dependent fsck"
	tune2fs -i 0 /dev/${disk}${part} >/dev/null 2>&1
	if [ $? -ne 0 ]; then
	    echo "  WARNING: could not disable time-dependent fsck"
	fi
    fi

    # The rest is just for the root FS
    if [ $isroot -eq 0 ]; then
	return
    fi

    rootdev=/dev/${disk}${part}

    #
    # See if there is a valid filesystem UUID to use in fstab and grub.
    #
    RUUID=`dumpe2fs $rootdev 2>/dev/null | grep UUID: | \
	sed -e 's/.*UUID:  *\([^ ]*\).*/\1/'`
    #
    # To be pedantic, we should do the following (generate a new
    # UUID). Note that this will cause the root FS to be fsck'ed
    # everytime (see needfsck below).
    #
    if [ -n "$RUUID" ]; then
	# generate/install a new random UUID with tune2fs
	echo "  setting new root FS UUID"
	tune2fs -U random $rootdev 2>/dev/null
	RUUID=`dumpe2fs $rootdev 2>/dev/null | grep UUID: | \
	    sed -e 's/.*UUID:  *\([^ ]*\).*/\1/'`
    fi

    #
    # Attempt to mount filesystem   
    #
    echo "  fixing Linux root partition ${disk}${part}"
    for t in ext4 ext3; do
	if mount -t $t $rootdev /mnt; then
	    break;
	fi
    done
    if [ $? -ne 0 ]; then
	echo "Failed to mount ${disk}${part}"
	return 1
    fi

    # hardwire the boss node
    if [ $setboss -eq 1 -a -d /mnt/etc/emulab ]; then
	echo "  hardwiring boss to $bossnode"
	cp $ETCDIR/bossnode /mnt/etc/emulab/bossnode || {
	    echo "Failed to create /etc/emulab/bossnode"
	    umount $rootdev
	    return 1
	}
    fi

    # change the devices in fstab
    echo "  updating /etc/fstab"
    sed -i.preemulab -e "s;^/dev/[hs]d.\([0-7]\);/dev/${disk}\1;" /mnt/etc/fstab || {
	echo "Failed to update /etc/fstab"
	umount $rootdev
	return 1
    }

    #
    # If filesystem has a UUID, make sure that is used in fstab.
    #
    # Note that we will only use the UUID if it is already being used
    # in the image we just downloaded. This is just because I don't want
    # to chance breaking some really old image.
    #
    if [ -n "$RUUID" ]; then
	sed -i -e "s;^UUID=[^ ][^ ]*  */ ;UUID=$RUUID / ;" /mnt/etc/fstab || {
	    echo "Failed to update /etc/fstab"
	    umount $rootdev
	    return 1
	}
    fi
	
    #
    # XXX let's not mess with /boot/grub/menu.lst unless we have to
    #

    #
    # XXX there is only one choice of console as well
    #

    #
    # Localize the image. We only do this if the MFS we are running in
    # has the necessary files.
    #
    if islocalized; then
	echo "  localizing ..."
	$BINDIR/localize_image /mnt || {
	    umount $rootdev
	    return 1
	}
    fi

Mike Hibler's avatar
Mike Hibler committed
236 237 238 239 240 241 242 243 244 245
    #
    # See if we need to trim the FS
    #
    if [ $TRIM -eq 1 -a -x "/sbin/fstrim" ]; then
	echo "  TRIMing FS"
	/sbin/fstrim /mnt || {
	    echo "WARNING: could not fstrim /"
	}
    fi

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
    umount $rootdev

    return 0
}

fixone() {
    echo "*** ${disk}${part}:"
    ptype=`sgdisk -p /dev/$disk | \
	sed -n "s/^  *$part  *.* \([0-9a-fA-F][0-9a-fA-F]*\) *$/\1/p"`
    ptype=${ptype:-0}

    case $ptype in
    8300)
	dolinux
	return $?
	;;
    8200)
	echo "ignoring Linux swap partition"
	;;
    7|0x7)
	echo "ignoring NTFS partition"
	;;
    0)
	;;
    *)
	echo "ignoring GPT partition type $ptype"
	;;
    esac
    return 0
}

case $part in
0)
    part=1; fixone
    part=2; fixone
    part=15; fixone

    # when fixing all partitions, do not fail on an error
    if [ $? -ne 0 ]; then
    	echo "WARNING: failed to fix one or more partitions, BEWARE!"
    fi
    ;;
# handle any numeric argument
[1-9]|[1-9][0-9])
    fixone
    ;;
*)
    echo "bad GPT partition: $part"
    false
    ;;
esac

exit $?