yhkylin-backup-tools/backup-daemon/data/mount_fstab_efi

137 lines
3.8 KiB
Plaintext
Raw Normal View History

2022-02-11 16:28:04 +08:00
#!/bin/bash
parse_device(){
device=$1
if echo $device | grep -E -q "^UUID="; then
echo $device | sed 's:^UUID=:/dev/disk/by-uuid/:' | tr -d "\n"
elif echo $device | grep -E -q "^LABEL="; then
echo $device | sed 's:^LABEL=:/dev/disk/by-label/:' | tr -d "\n"
else
echo $device | tr -d "\n"
fi
}
findmnt_dev()
{
fstab_path=$1
target_dir=$2
cat "$fstab_path" | grep -Ev "^#" | awk '{print $1 " " $2 " " $3 " " $4 " " $5 " " $6}' |
while read device mntdir fstype options dump passno ;
do
if [ "$mntdir" = "$target_dir" ] ; then
parse_device "$device"
break
fi
done
}
is_remote(){
fstype=$1
if [ nfs = "$fstype" ] || [ nfs4 = "$fstype" ] || [ smbfs = "$fstype" ] || [ cifs = "$fstype" ] || [ coda = "$fstype" ] || [ ncp = "$fstype" ]; then
echo yes
elif [ ncpfs = "$fstype" ] || [ ocfs2 = "$fstype" ] || [ gfs = "$fstype" ] || [ gfs2 = "$fstype" ] || [ ceph = "$fstype" ]; then
echo yes
else
echo no
fi
}
parse_options(){
options=$1
optstr="showthrough bootwait nobootwait timeout ro"
echo "$options" | awk -v optstr="$optstr" 'BEGIN{FS=","; split(optstr, opts, " ");} {
result="";
for( i=1; i<=NF; i++ ){
flag=0;
for( opt in opts ){
if( $i == opts[opt] ){
flag=1;
break;
}
}
if( flag == 1 )
continue;
if( i==1 )
result=$i;
else
result=result","$i;
}
print result
}'
}
is_system_partition(){
mntdir=$1
sys_parts="/boot /home /var /usr /srv /opt /usr/local" # files under /tmp is temporary, need to remove
echo $sys_parts | grep -Eq "(^| )"$mntdir"( |$)" && echo "yes" || echo "no"
}
mount_fstab(){
fstab_path=${rootmnt}/etc/fstab
if [ ! -e "$fstab_path" ]; then
fstab_path=/etc/fstab-backup
fi
2022-11-24 17:01:55 +08:00
# 将/usr/share/initramfs-tools/scripts/local-bottom/kybackup脚本放到/usr/share/initramfs-tools/scripts/init-bottom下后在备份还原前就已经mount了各个分区bind的没有mount
echo "rw mode remount $fstab_path begin"
2022-11-25 14:39:01 +08:00
local mounted_path=
local tmp_path=
2022-02-11 16:28:04 +08:00
cat "$fstab_path" | grep -Ev "^#" | awk '{print $1 " " $2 " " $3 " " $4 " " $5 " " $6}' |
while read device mntdir fstype options dump passno ;
do
device=$(parse_device $device)
#mntopts=$(parse_options $options)
mntopts=defaults,rw
#nodev filesystems
( cat /proc/filesystems | grep "$fstype" | grep -q nodev ) && continue
#virtual or network filesystems
([ none = "$mntdir" ] || [ yes = $(is_remote $fstype) ]) && continue
2022-11-25 14:39:01 +08:00
#swap or backup分区backup分区后面单独挂载根分区已挂载
([ swap = "$fstype" ] || [ /backup = "$mntdir" ] || [ / = "$mntdir" ]) && continue
2022-02-11 16:28:04 +08:00
#not system partition
#[ no = $(is_system_partition $mntdir) ] && continue
#去掉首字符'/'
#mntdir_name=$(echo $mntdir | sed 's:^/::g')
#target_mntdir="/$(echo $mntdir_name | sed 's:/:-:g').orig"
2022-11-25 14:39:01 +08:00
target_mntdir=${rootmnt}${mntdir}
mounted_path=
tmp_path=
2022-02-11 16:28:04 +08:00
mkdir -p "$target_mntdir"
2022-11-25 14:39:01 +08:00
tmp_path=$target_mntdir
mounted_path=$(mount | awk -v v_path=${tmp_path} '{ if($3 == v_path) print $3 }')
2022-02-11 16:28:04 +08:00
if [[ $options =~ bind ]] ; then
echo "mount -o $options ${rootmnt}${device} $target_mntdir"
mount -o $options "${rootmnt}${device}" "$target_mntdir"
2022-11-25 14:39:01 +08:00
elif [ "x" == "x"${mounted_path} ]; then
echo "mount -n -t $fstype -o $mntopts $device $target_mntdir"
mount -n -t $fstype -o $mntopts $device "$target_mntdir"
2022-02-11 16:28:04 +08:00
else
2022-11-24 17:01:55 +08:00
# echo "mount -n -t $fstype -o $mntopts $device $target_mntdir"
# mount -n -t $fstype -o $mntopts $device "$target_mntdir"
echo "mount -o rw,remount $target_mntdir"
mount -o rw,remount "$target_mntdir"
2022-02-11 16:28:04 +08:00
fi
#如果一个分区已经安装了,则下列会报错后不再继续
#if [ $? -ne 0 ]; then
# panic "Failed to mount %s on %s" "$device" "$target_mntdir"
# break
#fi
done || exit 1
2022-11-24 17:01:55 +08:00
echo "rw mode remount $fstab_path end"
mount
2022-02-11 16:28:04 +08:00
}
mount_fstab