You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ublinux-init/ublinux/rc.preinit.d/11-partitions

160 lines
6.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/bash
#
# Author: Dmitry Razumov <asmeron@ublinux.com>
# Copyright (c) 2021-2025 UBLinux <support@ublinux.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Initial script for Linux UBLinux
# This script are launching before starting init from initrd script
# Current dir allways must be set to root (/)
# All system path must be relative, except initrd dirs
ENABLED=no
[[ ${ENABLED} == "yes" ]] || { return 0 2>/dev/null && return 0 || exit 0; }
PATH=.:/:/usr/bin:/usr/local/bin:/usr/local/sbin
DEBUGMODE=no
[[ -d /usr/lib/ublinux ]] && { ROOTFS= ; CMD_CHROOT= ; } || { [[ -d /sysroot ]] && ROOTFS="/sysroot" || ROOTFS="."; CMD_CHROOT="chroot ${ROOTFS}"; }
#SOURCE=${ROOTFS}/usr/lib/ublinux/scripts/liblinuxlive; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
SOURCE=${ROOTFS}/usr/lib/ublinux/functions; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
SOURCE=${ROOTFS}/usr/lib/ublinux/default; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null || exit 0
debug_mode "$0" "$@"
SYSCONF="${ROOTFS}${SYSCONF}"
SOURCE=${SYSCONF}/config; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
SOURCE=${SYSCONF}/mount; [[ -f ${SOURCE} ]] && . ${SOURCE} 2>/dev/null
##############################################################
## TODO: Need to test
##############################################################
[[ ${MNT_BLOCK} == "yes" ]] && chmod 600 /mnt
[[ -z ${PARTITIONSMOUNTUID} ]] && PARTITIONSMOUNTUID=500
FSCKFLAG=0
[ $(cmdline_parameter fsck) ] && FSCKFLAG=2
exec_tmp_fstab(){
#Creating partitions in /etc/fstab
if [[ ${TMPFS} == "no" ]]; then
ln -sf /dev/null ${ROOTFS}/etc/systemd/system/tmp.mount
fi
if [[ ${VARTMPFS} == "no" && -h ${ROOTFS}/var/tmp ]]; then
rm -f ${ROOTFS}/var/tmp 2>/dev/null
mkdir -m 1777 ${ROOTFS}/var/tmp
elif [[ ${VARTMPFS} != "no" && ! -h ${ROOTFS}/var/tmp ]]; then
rm -fr ${ROOTFS}/var/tmp 2>/dev/null
ln -s ../tmp ${ROOTFS}/var/tmp
fi
[[ $(< ${ROOTFS}/etc/fstab) =~ "tmpfs /media tmpfs" ]] || echo "tmpfs /media tmpfs size=1M 0 0 # AutoUpdate" >> ${ROOTFS}/etc/fstab
}
exec_partautomount(){
get_name(){
for GEN_NUM in "" $(seq 2 64); do
grep -q " /media/$1${GEN_NUM} " ${ROOTFS}/etc/fstab || break
done
echo $1${GEN_NUM}
}
fs_options(){
## TODO
true
}
if [[ ${PARTITIONSAUTOMOUNT} == "yes" ]]; then
sed -i /PARTITIONSAUTOMOUNT/d ${ROOTFS}/etc/fstab
while IFS= read -ru3 SELECT_DEVICE; do
[[ $(< ${ROOTFS}/etc/fstab) =~ (^|$'\n')"${SELECT_DEVICE} "]] && continue
if [[ $(< /proc/mounts) =~ (^|$'\n')"${SELECT_DEVICE} " ]]; then
grep -m1 "^${SELECT_DEVICE} " /proc/mounts | grep /memory | sed s/$/" #PARTITIONSAUTOMOUNT=yes"/ >> ${ROOTFS}/etc/fstab
else
DEVTYPE=$(blkid ${SELECT_DEVICE} | awk -FTYPE=\" '{print $2}'| sed s/\".*// )
if [[ ${DEVTYPE} != "" && ${DEVTYPE} != "swap" && ${DEVTYPE} != "udf" && ${DEVTYPE} != "iso9660" ]]; then
DEVLABEL=$(blkid ${SELECT_DEVICE} | awk -FLABEL=\" '{print $2}'| sed s/\".*// | tr -d [:cntrl:][:space:])
[[ -z ${DEVLABEL} ]] && DEVLABEL=$(get_name disk)
[[ -d "${ROOTFS}/media/${DEVLABEL}" ]] && DEVLABEL=$(get_name ${DEVLABEL})
tr ',;' ' ' <<< " ${PARTITIONSNOMOUNT} " | grep -q " ${DEVLABEL} " && continue
DEVOPTS=$(fs_options ${SELECT_DEVICE} fstab)
[[ ${DEVTYPE} == "vfat" || ${DEVTYPE} = "ntfs" ]] && DEVOPTS="${DEVOPTS},uid=${PARTITIONSMOUNTUID}"
echo "${SELECT_DEVICE} /media/${DEVLABEL} ${DEVTYPE} ${DEVOPTS} 0 ${FSCKFLAG} #PARTITIONSAUTOMOUNT=yes" >> ${ROOTFS}/etc/fstab
[[ -d "${ROOTFS}/media/${DEVLABEL}" ]] || mkdir -p ${ROOTFS}/media/${DEVLABEL}
elif [[ ${DEVTYPE} == "swap" ]]; then
[[ $(< ${ROOTFS}/etc/fstab) =~ (^|$'\n')"${SELECT_DEVICE} none swap" ]] || echo "${SELECT_DEVICE} none swap defaults 0 0 #PARTITIONSAUTOMOUNT=yes" >> ${ROOTFS}/etc/fstab
fi
fi
done 3< <(blkid | grep -Ev "loop|/dev/ram" | grep /dev/ | awk -F: '{ print $1 }')
fi
}
exec_connect_win_swap(){
#Find and use swap partition. If not, use window$ swapfile.
if [[ -n $(cmdline_parameter findswap) ]]; then
sed -i /FINDSWAP/d ${ROOTFS}/etc/fstab
debug_log "Find linux native patitions"
FSWAP=$(blkid -t TYPE="swap" -l | awk -F: '{print $1}')
if [[ ${FSWAP} ]]; then
echo "${FSWAP} none swap defaults 0 0 #FINDSWAP" >> ${ROOTFS}/etc/fstab
else
debug_log "Capturing windows swapfile"
FSWAP=$(find_in_computer /pagefile.sys /${UNION}/mnt/windows)
if [[ -n ${FSWAP} ]]; then
if [[ -f ${FSWAP} ]]; then
mkswap ${FSWAP}
DSWAP=$(dirname ${FSWAP})
DEV=$(grep /${UNION}/mnt/windows /proc/mounts | awk '{ print $1 }')
DEVTYPE=$(blkid ${DEV} -s TYPE | awk -F\" '{print $2}' )
if [[ ${DSWAP} == "/${UNION}/mnt/windows" -a -b "${DEV}" ]]; then
DEVOPTS=$(fs_options ${DEV} fstab),uid=${PARTITIONSMOUNTUID}
echo "${DEV} /mnt/windows ${DEVTYPE} ${DEVOPTS} 0 ${FSCKFLAG} # FINDSWAP" >> ${ROOTFS}/etc/fstab
umount /${UNION}/mnt/windows 2>/dev/null
fi
echo "${FSWAP} none swap defaults 0 0 # FINDSWAP" | sed s-/$UNION-- >> ${ROOTFS}/etc/fstab
fi
fi
fi
fi
}
exec_compcache(){
#Enable compcache if it is needed
[[ $(< ${ROOTFS}/etc/fstab) =~ " swap " || ${SERVICESNOSTART} =~ "compcache" ]] || ${CMD_CHROOT} chkconfig --add compcache >/dev/null 2<&1
}
exec_partitions_shared_nfs(){
#Exporting partitions
if [[ -n ${PARTITIONS_SHARED_NFS} ]]; then
for SELECT_POINT in $(grep " /media/" ${ROOTFS}/etc/fstab | awk '{print $2}' | sed s:/media/::); do
[[ $(< ${ROOTFS}/etc/exports) =~ (^|$'\n')"/media/${SELECT_POINT} " ]] && continue
[[ ${PARTITIONS_NOSHARE_NFS} =~ (^|,|;)"${SELECT_POINT}"(,|;|$) ]] && continue
echo "/media/${SELECT_POINT} ${PARTITIONS_SHARED_NFS}(all_squash,anonuid=500,anongid=100,async,secure,no_subtree_check,rw)" >> ${ROOTFS}/etc/exports
done
fi
}
exec_link(){
if [[ -d /mnt/livemedia/ublinux-data && ! -d /mnt/livedata ]]; then
ln -sf livemedia ${ROOTFS}/mnt/livedata
fi
}
################
##### MAIN #####
################
# Если файл подключен как ресурс с функциями, то выйти
return 0 2>/dev/null && return 0
if [[ -z $@ ]]; then
while read -r FUNCTION; do
$"${FUNCTION##* }"
done < <(declare -F | grep "declare -f exec_")
else
FUNCTION=
while [[ $# -gt 0 ]]; do
[[ -z ${1} ]] || { declare -f "${1}" &>/dev/null && FUNCTION+="; ${1}" || FUNCTION+=" '${1//\'/}'"; }
shift
done
eval ${FUNCTION#*; }
fi