| Server IP : 193.86.120.172 / Your IP : 216.73.216.67 Web Server : Apache/2.4.63 (Unix) System : Linux JServices 3.10.108 #86003 SMP Wed Oct 22 13:20:46 CST 2025 x86_64 User : kubec ( 1026) PHP Version : 8.2.28 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /usr/lib/systemd/scripts/ |
Upload File : |
#!/bin/bash
linkname=/usr/syno/etc/preference # path to symbolic link that points to preference folder
synoinfo=/etc/synoinfo.conf # path to synoinfo.conf file
movepreferencetovolumeFn() # main function to move preference folder to a different volume
{
echo "pid: $$, movepreferencetovolumeFn"
##############
## Precheck ##
##############
# Early exit if migration is running or volume service is not ready
if [[ $(/usr/syno/bin/synosystemctl get-active-status syno-volume.service) != "active" || -f /tmp/synomigrationassistant_vspace_stacking ]]
then
exit 0
fi
# Get the real preference folder path saved in synoinfo.conf, e.g., folderpath = /volume1/@userpreference
folderpath=$(/bin/gawk 'match($0, /userpreference_realpath="(.*)"/, ary) { print ary[1] }' "$synoinfo")
# If symbolic link doesn't exist and preference folder exists on volume, create symbolic link
if [[ ! -e $linkname && -d $folderpath && $linkname != "$folderpath" ]]
then
/bin/ln -s "$folderpath" $linkname
fi
# If /usr/syno/etc/preference is still not exist or not a folder, create a new preference folder on md0
# Represents that the realpath saved in synoinfo is lost or uninitialized
if [[ ! -d $linkname ]]
then
/bin/rm $linkname >/dev/null 2>&1
/bin/mkdir $linkname
fi
# Current preference storage path
realpath="$(/bin/readlink -f $linkname)"
# Handle restoration when encrypted volume mounted back with user preference configured
encryptedvolumepath=$(/bin/gawk 'match($0, /userpreference_encryptedvolume="(.*)"/, ary) { print ary[1] }' "$synoinfo")
if [[ -n "$encryptedvolumepath" ]]; then
# Set the variable restorePath to the path of the folder containing the restored user preference data on the encrypted volume
restorePath="$encryptedvolumepath"/@userpreference
# If restorePath is a directory and realpath is not the same as restorePath
if [[ -d "$restorePath" && "$realpath" != "$restorePath" ]]; then
# Remove the existing symlink at realpath
rm -rf "$realpath"
# Create a symlink at linkname pointing to restorePath
/bin/ln -s "$restorePath" "$linkname"
synosetkeyvalue /etc/synoinfo.conf "userpreference_realpath" "$restorePath"
echo "pid: $$, restore preference from encrypted volume $encryptedvolumepath"
exit 0
fi
fi
# Early exit if the preference is already on volume and not in skipped volume path
if [[ -L $linkname && -d $realpath && $realpath =~ /volume[1-9][0-9]*/@userpreference && $realpath != $1/@userpreference ]]
then
# If the realpath has updated, save realpath into synoinfo
if [[ "$realpath" != "$folderpath" ]]
then
synosetkeyvalue /etc/synoinfo.conf "userpreference_realpath" "$realpath"
fi
exit 0
fi
######################################
## Find and move to new target path ##
######################################
preferencesize=$(/bin/du -sm $linkname/ | /bin/awk '{printf $1}') # size of preference folder in MB
# Get the target volume path to move preference folder to, e.g., /volume1, and skip any specified paths
volumepath=$(/usr/syno/bin/servicetool --get-available-volume --space-requirement "$preferencesize" --skip-volume-path "$1")
ret=$?
if [[ $ret -eq 1 ]]; then
# Move to volume
target=$volumepath/@userpreference
tmp=$volumepath/@userpreference.tmp
else
# Move to /usr/syno/etc/preference
# preserve 100 MB in case of moving to md0
md0availablesize=$(/bin/df /dev/md0 | /bin/tail -1 | /bin/awk '{print $4}')
if [[ "$((preferencesize + 100*1024))" -gt "$md0availablesize" ]]; then
cp -ar $realpath $realpath.bak >/dev/null 2>&1
exit 0
fi
target=$linkname
tmp=${linkname}.tmp
fi
# Early exit if target == current path
if [[ "$target" = "$realpath" ]]
then
if [[ "$realpath" != "$folderpath" ]]
then
synosetkeyvalue /etc/synoinfo.conf "userpreference_realpath" "$realpath"
fi
exit 0
fi
# Handle volume encryption
# Check if the target volume is encrypted
isTargetEncryptVolume=$(/usr/syno/sbin/synostgvolume --is-encrypted-volume "$volumepath"; echo $?)
# If the target volume is encrypted, write the volume path to userpreference_encryptedvolume in synoinfo
if [[ "$isTargetEncryptVolume" -eq 1 ]]; then
synosetkeyvalue /etc/synoinfo.conf "userpreference_encryptedvolume" "$volumepath"
else
# If the target volume is not encrypted, get the current volume path and check if it is encrypted
currentVolume=$(echo "$realpath" | awk -F'/' '{print "/"$2}')
isCurrentVolumeEncrypted=$(/usr/syno/sbin/synostgvolume --is-encrypted-volume "$currentVolume"; echo $?)
# If moving user preferences out of encrypted volume to normal volume, reset userpreference_encryptedvolume in synoinfo
if [[ "$isCurrentVolumeEncrypted" -eq 1 ]]; then
synosetkeyvalue /etc/synoinfo.conf "userpreference_encryptedvolume" ""
fi
fi
############################
## Move Preference Folder ##
############################
# Copy preference to tmp folder
/bin/mkdir -p $tmp
/bin/cp -au "$realpath"/* $tmp/
retVal=$?
# Early exit if copying files fails, possibly because the disk is full
if [[ $retVal -ne 0 ]]; then
/bin/rm -rf $tmp
exit 0
fi
# Remove original preference folder
if [[ -L "$realpath" && -d "$realpath" ]]
then
/bin/rm -rf "$realpath" >/dev/null 2>&1
else
/bin/rm -rf "$realpath".old >/dev/null 2>&1
/bin/mv -f "$realpath" "$realpath".old
fi
# Move tmp path to target path
/bin/rm -rf $target >/dev/null 2>&1
/bin/mv -f $tmp $target
# Re-link to correct target path
if [[ "$linkname" != "$target" ]]
then
/bin/rm -rf $linkname >/dev/null 2>&1
/bin/ln -s $target $linkname
fi
synosetkeyvalue /etc/synoinfo.conf "userpreference_realpath" "$target"
/bin/rm -rf "$realpath".old >/dev/null 2>&1
echo "pid: $$, preference moved from $realpath to $target"
}
# due to DSM 7.0 has modified ssh shell umask to 0077, 0022 is needed to run this script
umask 0022 && movepreferencetovolumeFn "$1"