Commit a0be4363 authored by Natanael Copa's avatar Natanael Copa

setup-bootable: update syslinux.cfg if not usbdisk

+ misc fixes
parent b2b998ee
...@@ -18,6 +18,21 @@ die() { ...@@ -18,6 +18,21 @@ die() {
exit 1 exit 1
} }
# find device for mountpoint
find_dev() {
local mnt="${1%/}" # strip trailing /
awk "\$2 == \"$mnt\" {print \$1}" /proc/mounts
}
# check if given device is on usb bus
on_usb_bus() {
local dev="$1"
[ -e /sys/block/$dev ] || return 1
local sysdev=$(readlink -f /sys/block/$dev/device)
test "${sysdev##*/usb[0-9]}" != "$sysdev"
}
usage() { usage() {
cat <<__EOF__ cat <<__EOF__
$prog $version $prog $version
...@@ -41,11 +56,11 @@ __EOF__ ...@@ -41,11 +56,11 @@ __EOF__
while getopts "fhusv" opt; do while getopts "fhusv" opt; do
case "$opt" in case "$opt" in
-f) force=1;; f) force=1;;
-h) usage;; h) usage;;
-u) upgrade=1;; u) upgrade=1;;
-s) syslinux=1;; s) syslinux=1;;
-v) verbose=1;; v) verbose=1;;
esac esac
done done
...@@ -61,30 +76,39 @@ srcmnt=${MNT:-/mnt} ...@@ -61,30 +76,39 @@ srcmnt=${MNT:-/mnt}
# Find the srcdir, mount loopback if needed # Find the srcdir, mount loopback if needed
if [ -f "$src"/.alpine-release ]; then if [ -f "$src"/.alpine-release ]; then
srcdir="$src" srcdir="$src"
[ -n "$verbose" ] && echo "Using directory $src as source"
else else
mount -o loop -t iso9660 "$src" $srcmnt \ mount -o loop -t iso9660 "$src" $srcmnt \
|| die "Failed to mount loopback $src" || die "Failed to mount loopback $src"
[ -n "$verbose" ] && echo "Using image $src as source"
umounts="$srcmnt" umounts="$srcmnt"
srcdir="$srcmnt" srcdir="$srcmnt"
[ -f "$srcdir"/.alpine-release ] || die "No .alpine-release found on image $src" [ -f "$srcdir"/.alpine-release ] || die "No .alpine-release found on image $src"
fi fi
to_version=$(cat "$srcdir"/.alpine-release)
# find target device # find target device
if [ -d "$dest" ]; then if [ -d "$dest" ]; then
dest=${dest%/} # strip trailing /
if ! awk '{print $2}' /proc/mounts | grep -q "^$dest\$"; then if ! awk '{print $2}' /proc/mounts | grep -q "^$dest\$"; then
mount "$dest" || die "Failed to mount $dest" mount "$dest" || die "Failed to mount $dest"
umounts="$umounts $dest" umounts="$umounts $dest"
fi fi
destdir="$dest" destdir="$dest"
[ -n "$vserbose" ] && echo "Using $destdir as target" dest=$(find_dev "$destdir")
elif [ -b "$dest" ]; then elif [ -b "$dest" ]; then
destdir="/media/${dest##*/}" destdir="/media/${dest##*/}"
mkdir -p "$destdir" mkdir -p "$destdir"
mount "$dest" "$destdir" || die "Failed to mount $dest on $destdir" mount "$dest" "$destdir" || die "Failed to mount $dest on $destdir"
[ -n "$vserbose" ] && echo "Using $dest (mounted on $destdir) as target" umounts="$umounts $destdir"
fi fi
[ -n "$verbose" ] && echo "Using $dest as target (mounted on $destdir)"
# find parent device (i.e sda)
dev="$dest"
while [ -L "$dev" ]; do
dev=$(readlink -f $dev)
done
parent_dev=/dev/$(basename $(dirname /sys/block/*/$(basename $dev)))
# check if this files exist and not in upgrade mode # check if this files exist and not in upgrade mode
if [ -z "$upgrade" ] && [ -z "$force" ]; then if [ -z "$upgrade" ] && [ -z "$force" ]; then
...@@ -93,6 +117,21 @@ if [ -z "$upgrade" ] && [ -z "$force" ]; then ...@@ -93,6 +117,21 @@ if [ -z "$upgrade" ] && [ -z "$force" ]; then
done done
fi fi
# check if its same version
if [ -n "$upgrade" ] && [ -e "$destdir"/.alpine-release ]; then
from_version=$(cat "$destdir"/.alpine-release)
if [ -z "$force" ] && [ "$from_version" = "$to_version" ]; then
die "Source and target seems to have same version ($from_version). Aborting."
fi
fi
# Display what versions we are installing/upgrading
if [ -n "$from_version" ]; then
echo "Upgrading $dest from $from_version to $to_version"
else
echo "Copying $to_version to $dest (mounted on $destdir)"
fi
# remove partial upgrades if any. # remove partial upgrades if any.
rm -rf "$destdir"/.new "$destdir"/.old rm -rf "$destdir"/.new "$destdir"/.old
...@@ -105,23 +144,30 @@ available_space=$(df -k "$destdir" | tail -n 1 | awk '{print $4}') ...@@ -105,23 +144,30 @@ available_space=$(df -k "$destdir" | tail -n 1 | awk '{print $4}')
[ $available_space -lt $needed_space ] && die "Not enough space on $destdir. Aborting." [ $available_space -lt $needed_space ] && die "Not enough space on $destdir. Aborting."
# copy the files to .new # copy the files to .new
echo "Copying new files to $destdir/.new..."
mkdir -p "$destdir"/.new || die "Failed to create $destdir/.new" mkdir -p "$destdir"/.new || die "Failed to create $destdir/.new"
for i in boot apks syslinux.cfg .alpine-release; do for i in boot apks syslinux.cfg .alpine-release; do
[ -n "$verbose" [ -n "$verbose" ] && echo "Copying $srcdir/$i to $destdir/.new/"
cp -a "$srcdir"/$i "$destdir"/.new/ cp -a "$srcdir"/$i "$destdir"/.new/
done done
# make sure files are really there before we replace existing # make sure files are really there before we replace existing
[ -n "$verbose" ] && echo "Flushing cache..."
sync sync
echo "Replacing existing files..." [ -n "$verbose" ] && echo "Replacing existing files..."
mkdir -p "$destdir"/.old || die "Failed to create $destdir/.old" mkdir -p "$destdir"/.old || die "Failed to create $destdir/.old"
# do we want keep existing syslinux.cfg? # do we want keep existing syslinux.cfg?
tomove="boot apks .alpine-release" tomove="boot apks .alpine-release"
if [ -n "$force" ] || ! [ -e "$destdir"/syslinux.cfg ]; then if [ -n "$force" ] || ! [ -e "$destdir"/syslinux.cfg ]; then
tomove="$tomove syslinux.cfg" tomove="$tomove syslinux.cfg"
# update syslinux.cfg unless device is on usb bus
# this is so we can boot from CF's and harddisk
if ! on_usb_bus $parent_dev; then
[ -n "$verbose" ] && echo "Updating syslinux.cfg to use $dest"
sed -i -e "s/usbdisk/${dest##*/}/g" \
"$destdir"/.new/syslinux.cfg
fi
fi fi
# move current files to .old # move current files to .old
...@@ -142,27 +188,25 @@ sync ...@@ -142,27 +188,25 @@ sync
# If we only copy then we are done. # If we only copy then we are done.
if [ -z "$syslinux" ]; then if [ -n "$upgrade" ] && [ -z "$syslinux" ]; then
cleanup cleanup
echo "Done."
exit 0 exit 0
fi fi
echo "Making usb bootable..." echo "Making $dest bootable..."
dev=$(awk "\$2 == \"$dest\" {print \$1}" /proc/mounts)
parent=$(basename $(dirname /sys/block/*/$(basename $dev))) if ! [ -x "$(which syslinux)" ]; then
if ! apk info -q -e syslinux; then
apk add -q syslinux || die "Failed to install syslinux" apk add -q syslinux || die "Failed to install syslinux"
uninstalls="syslinux" uninstalls="syslinux"
fi fi
syslinux $dev syslinux $dest
if [ -b /dev/$parent ]; then if [ -b $parent_dev ]; then
dd if=/usr/share/syslinux/mbr.bin of=/dev/$parent dd if=/usr/share/syslinux/mbr.bin of=$parent_dev
else else
echo "Warning: Could not find the parent device for $dev" echo "Warning: Could not find the parent device for $dest"
fi fi
cleanup cleanup
echo "Done." sync
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment