254 lines
6.5 KiB
Bash
Executable file
254 lines
6.5 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
labsync_set_environment() {
|
|
if grep -q "^${1}=" /${rootmnt}/etc/environment; then
|
|
sed -i "s/^\(${1}\)=.*$/\1='${2}'/" /${rootmnt}/etc/environment
|
|
else
|
|
echo "${1}='${2}'" >> /${rootmnt}/etc/environment
|
|
fi
|
|
}
|
|
|
|
labsync_lv_path() {
|
|
lvm lvs "$1/$2" -o LV_PATH --noheadings | sed 's/\s//g'
|
|
}
|
|
|
|
labsync_create_or_resize_lv() {
|
|
vg="$1"
|
|
lv="$2"
|
|
new_size_bytes="$3"
|
|
minimum_size_bytes="$4"
|
|
current_lv_size_bytes="$(lvm lvs "$vg/$lv" -o LV_SIZE --noheadings --units b --nosuffix 2>/dev/null | sed 's/\s//g')"
|
|
|
|
if [ -z "$current_lv_size_bytes" ]; then
|
|
# no size returned, it's likely that lv doesn't exist
|
|
lvm lvcreate --wipesignatures y -L ${new_size_bytes}b -n "$lv" "$vg"
|
|
elif [ "$minimum_size_bytes" != "" ] && [ $minimum_size_bytes -gt 0 ]; then
|
|
# lv already exists
|
|
if [ $current_lv_size_bytes -lt $minimum_size_bytes ]; then
|
|
lvm lvextend -L ${new_size_bytes}b "$vg/$lv"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
labsync_info() {
|
|
echo -e "\n\033[36m${1}... \033[0m"
|
|
}
|
|
|
|
labsync_warn() {
|
|
echo -e "\n\033[31mWARNING: ${1}\033[0m"
|
|
}
|
|
|
|
labsync_err() {
|
|
labsync_warn "$1; interrupting startup"
|
|
read x
|
|
}
|
|
|
|
labsync_top() {
|
|
if [ -z ${labsync_localboot} ]; then
|
|
# Check if mandatory parameters are missing
|
|
if [ -z "${labsync_disk}" ]; then
|
|
labsync_err "The disk parameter is missing"
|
|
fi
|
|
if [ -z "${labsync_partsize_boot}" ]; then
|
|
labsync_err "The partsize_boot parameter is missing"
|
|
fi
|
|
if [ -z "${labsync_torrent}" ]; then
|
|
labsync_err "The torrent parameter is missing"
|
|
fi
|
|
fi
|
|
|
|
cat << EOM
|
|
Starting
|
|
""# #
|
|
# mmm #mmm mmm m m m mm mmm
|
|
# " # #" "# # " "m m" #" # #" "
|
|
# m"""# # # """m #m# # # #
|
|
"mm "mm"# ##m#" "mmm" "# # # "#mm"
|
|
m"
|
|
""
|
|
@@PROJECT_URL@@
|
|
|
|
EOM
|
|
}
|
|
|
|
labsync_premount() {
|
|
if [ -z "$labsync_localboot" ]; then
|
|
labsync_info "Configuring networking"
|
|
configure_networking
|
|
labsync_configure_dns
|
|
fi
|
|
}
|
|
|
|
labsync_configure_dns() {
|
|
for c in /run/net-*.conf; do
|
|
(
|
|
. "$c"
|
|
if ! [ -z "$IPV4DNS0" ]; then
|
|
echo "nameserver $IPV4DNS0" >> /etc/resolv.conf
|
|
fi
|
|
)
|
|
done
|
|
}
|
|
|
|
|
|
labsync_mount_root() {
|
|
haveged
|
|
|
|
export PATH="/usr/local/bin:$PATH"
|
|
[ "$labsync_debug" = 1 ] && set -x
|
|
sed -i 's/use_lvmetad = 1/use_lvmetad = 0/' /etc/lvm/lvm.conf
|
|
vg_name="vglab"
|
|
if [ -z "$labsync_localboot" ]; then
|
|
labsync_info "Downloading torrent file from ${labsync_torrent}"
|
|
while ! aria2c -t 10 --follow-torrent=false --dir /tmp -o torrent "${labsync_torrent}"; do sleep 1; done
|
|
aria2c -S /tmp/torrent > /tmp/torrent_info
|
|
squashfs_file="$(sed -n '/idx|path\/length/,${s/\s*1|\(.*\)$/\1/p}' /tmp/torrent_info)"
|
|
image_size_bytes="$(sed -n 's/Total Length: .*(\([0-9,]*\)).*$/\1/p' /tmp/torrent_info | tr -d ',')"
|
|
torrent_name="$(sed -n 's/^Name: \(.*\)$/\1/p' /tmp/torrent_info)"
|
|
|
|
lv_name_sqashfs="sqfs-$torrent_name"
|
|
lv_name_overlay="ovly-$torrent_name"
|
|
|
|
labsync_info "Updating partitions"
|
|
size_disk=$(blockdev --getsz /dev/${labsync_disk})
|
|
size_part_1=$((labsync_partsize_boot * 1024 * 1024 / 512))
|
|
size_part_2=$((size_disk - size_part_1 - 2048))
|
|
start_part_2=$((size_part_1 + 2048))
|
|
|
|
dmsetup ls --tree
|
|
|
|
lvm pvs -o PV_NAME,VG_NAME --noheadings | while read pv vg; do
|
|
if echo "$pv" | grep -q "^/dev/${labsync_disk}"; then
|
|
lvm vgchange -a n "$vg"
|
|
lvm vgexport $vg
|
|
fi
|
|
done
|
|
|
|
dmsetup ls --tree
|
|
|
|
sfdisk --force -q /dev/${labsync_disk} << PARTTABLE
|
|
label: dos
|
|
label-id: 0xdeadbeef
|
|
device: /dev/${labsync_disk}
|
|
unit: sectors
|
|
|
|
/dev/${labsync_disk}1 : start= 2048, size= $size_part_1, type=83
|
|
/dev/${labsync_disk}2 : start=$start_part_2, size= $size_part_2, type=8e
|
|
PARTTABLE
|
|
|
|
lvm vgimport -a
|
|
|
|
labsync_info "Doing lvm stuff"
|
|
pv_device="/dev/${labsync_disk}2"
|
|
|
|
lvm pvcreate -t "$pv_device"
|
|
pvstatus=$?
|
|
if [ $pvstatus -eq 0 ]; then
|
|
# is not a physical volume
|
|
lvm pvcreate "$pv_device"
|
|
fi
|
|
|
|
lvm vgcreate -t "$vg_name" "$pv_device"
|
|
vgstatus=$?
|
|
if [ $? -eq 0 ]; then
|
|
lvm vgcreate "$vg_name" "$pv_device"
|
|
fi
|
|
|
|
lvm lvchange -ay "$vg_name"
|
|
|
|
labsync_create_or_resize_lv "$vg_name" "$lv_name_sqashfs" $((image_size_bytes * 2)) $image_size_bytes
|
|
labsync_create_or_resize_lv "$vg_name" "$lv_name_overlay" $((10000 * 1024 * 1024))
|
|
|
|
#lvm vgchange -ay "$vg_name"
|
|
#lvm lvscan -a --ignorelockingfailure
|
|
#lvm lvchange -aly --ignorelockingfailure "$vg_name/$lv_name_sqashfs"
|
|
lv_path_squashfs="$(labsync_lv_path "$vg_name" "$lv_name_sqashfs")"
|
|
lv_path_overlay="$(labsync_lv_path "$vg_name" "$lv_name_overlay")"
|
|
|
|
mke2fs -q -F -t ext4 "$lv_path_overlay"
|
|
|
|
labsync_info "Downloading squashfs image $squashfs_file to $lv_path_squashfs"
|
|
(cd /tmp
|
|
ln -s "$(realpath "$lv_path_squashfs")" "$squashfs_file"
|
|
aria2c \
|
|
--console-log-level=warn \
|
|
--allow-overwrite \
|
|
--check-integrity \
|
|
--seed-time=0 \
|
|
--summary-interval=0 \
|
|
--file-allocation=none \
|
|
--enable-dht=false \
|
|
--force-save \
|
|
/tmp/torrent
|
|
)
|
|
else
|
|
# local boot
|
|
lv_name_sqashfs="sqfs-$labsync_localboot"
|
|
lv_name_overlay="ovly-$labsync_localboot"
|
|
lv_path_squashfs="$(labsync_lv_path "$vg_name" "$lv_name_sqashfs")"
|
|
lv_path_overlay="$(labsync_lv_path "$vg_name" "$lv_name_overlay")"
|
|
lvm lvchange -ay "$vg_name"
|
|
fi
|
|
|
|
labsync_info "Mounting squashfs and overlay"
|
|
modprobe overlay
|
|
mkdir -p /rom /overlay
|
|
mount -t squashfs "$(realpath "$lv_path_squashfs")" /rom
|
|
mount -t ext4 "$lv_path_overlay" /overlay
|
|
mkdir -p /overlay/upper /overlay/work
|
|
mount -t overlay overlay -o lowerdir=/rom,upperdir=/overlay/upper,workdir=/overlay/work ${rootmnt}
|
|
mkdir -p ${rootmnt}/overlay ${rootmnt}/rom
|
|
mount -n -o move /overlay ${rootmnt}/overlay
|
|
mount -n -o move /rom ${rootmnt}/rom
|
|
|
|
hostname=$(ip -o link show | sed -ne 's#^.*link/ether \([0-9a-f:]*\).*$#\1#p' | sed -ne '1{s/://g;p}')
|
|
echo $hostname > ${rootmnt}/etc/hostname
|
|
sed "s/@@HOSTNAME@@/$hostname/" ${rootmnt}/etc/hosts.template > ${rootmnt}/etc/hosts
|
|
|
|
mkdir -p ${rootmnt}/var/lib/labsync
|
|
cp /tmp/torrent.aria2 ${rootmnt}/var/lib/labsync
|
|
|
|
if [ -z "$labsync_localboot" ]; then
|
|
labsync_set_environment LABSYNC "$torrent_name"
|
|
labsync_set_environment LABSYNC_TORRENT "${labsync_torrent}"
|
|
labsync_set_environment LABSYNC_SQUASHFS_FILE "$squashfs_file"
|
|
labsync_set_environment LABSYNC_SQUASHFS_LV "$lv_path_squashfs"
|
|
labsync_set_environment LABSYNC_DISK "$labsync_disk"
|
|
fi
|
|
|
|
case "$labsync_wait" in
|
|
"")
|
|
;;
|
|
pause)
|
|
sh -i
|
|
;;
|
|
*)
|
|
sleep $labsync_wait
|
|
;;
|
|
esac
|
|
set +x
|
|
}
|
|
|
|
labsync_bottom() {
|
|
:
|
|
}
|
|
|
|
mountroot()
|
|
{
|
|
labsync_mount_root
|
|
}
|
|
|
|
mount_top()
|
|
{
|
|
labsync_top
|
|
}
|
|
|
|
mount_premount()
|
|
{
|
|
labsync_premount
|
|
}
|
|
|
|
mount_bottom()
|
|
{
|
|
labsync_bottom
|
|
}
|