diff --git a/mdadm.spec b/mdadm.spec index 41845ac49b6a985df41325c9104d655398974430..14920ca4d5f286989d5889f4714749d1d595d79a 100644 --- a/mdadm.spec +++ b/mdadm.spec @@ -1,6 +1,6 @@ Name: mdadm Version: 4.2 -Release: 6 +Release: 7 Summary: The software RAID arrays user manage tools License: GPLv2+ URL: http://www.kernel.org/pub/linux/utils/raid/mdadm/ @@ -9,6 +9,9 @@ Source0: http://www.kernel.org/pub/linux/utils/raid/mdadm/mdadm-%{version}.tar. Source1: mdcheck-cron Source2: mdmonitor.service Source3: mdadm.conf +Source4: persistent-device-name.service +Source5: persistent-device-name.timer +Source6: persistent_device_name.sh Patch1: 0001-mdadm-remove-Werror-to-fix-Werror-address-of-packed-.patch Patch2: 0002-mdadm-Fix-mdadm-r-remove-option-regresision.patch @@ -54,6 +57,13 @@ install -Dp -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/cron.d/mdcheck #install mdmonitor.service from local file install -D -m 644 %{SOURCE2} %{buildroot}%{_unitdir} +#install persistent-device-name.service persistent-device-name.timer from local file +install -D -m 644 %{SOURCE4} %{buildroot}%{_unitdir} +install -D -m 644 %{SOURCE5} %{buildroot}%{_unitdir} + +#install persistent_device_namd.sh from local file +install -Dp -m 755 %{SOURCE6} %{buildroot}%{_sbindir} + mkdir -p %{buildroot}%{_tmpfilesdir} install -m 644 %{SOURCE3} %{buildroot}%{_tmpfilesdir}/mdadm.conf install -d -m 710 %{buildroot}/var/run/mdadm/ @@ -61,9 +71,13 @@ install -d -m 710 %{buildroot}/var/run/mdadm/ %post %systemd_post mdmonitor.service /usr/bin/systemctl disable mdmonitor-takeover.service >/dev/null 2>&1 || : +/usr/bin/systemctl enable persistent-device-name.service +/usr/bin/systemctl daemon-reload %preun %systemd_preun mdmonitor.service +/usr/bin/systemctl stop persistent-device-name.service +/usr/bin/systemctl disable persistent-device-name.service >/dev/null 2>&1 || : %postun %systemd_postun_with_restart mdmonitor.service @@ -83,6 +97,9 @@ install -d -m 710 %{buildroot}/var/run/mdadm/ %{_mandir}/man*/* %changelog +* Mon Feb 27 2023 XueSinian - 4.2-7 +* add sh,service,timer for persistenting device name + * Fri Jan 6 2023 Zhiqiang Liu - 4.2-6 * fix NULL dereference in super_by_fd diff --git a/persistent-device-name.service b/persistent-device-name.service new file mode 100644 index 0000000000000000000000000000000000000000..1e5190c82fea4fc93f8069cf1b1cd3614faa88cb --- /dev/null +++ b/persistent-device-name.service @@ -0,0 +1,9 @@ +[Unit] +Description=Persistent device name + +[Service] +KillMode=process +ExecStart=/usr/sbin/persistent-device-name.sh + +[Install] +WantedBy=multi-user.target diff --git a/persistent-device-name.timer b/persistent-device-name.timer new file mode 100644 index 0000000000000000000000000000000000000000..bd8913ba4261a97a75e8a5e4e5cc2ae50de6564c --- /dev/null +++ b/persistent-device-name.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Persistent device name + +[Timer] +OnUnitActiveSec=1d +Unit=persistent-device-name.service + +[Install] +WantedBy=multi-user.target diff --git a/persistent_device_name.sh b/persistent_device_name.sh new file mode 100644 index 0000000000000000000000000000000000000000..4dba3e0b1d3e528dba737bdf485773c768691dd3 --- /dev/null +++ b/persistent_device_name.sh @@ -0,0 +1,141 @@ +#! /bin/sh +# +# persistent device name script +# + +set -e + +# Determine which file to modify +test -e /etc/fstab && modifyFstab=true || modifyFstab=false +test -e /etc/mdadm.conf && modifyMdadmConf=true || modifyMdadmConf=false + +default="default" +MAILER=mail +MAIL_SUBJECT=$default +MAIL_ADDRESS=root@localhost + +# Get id by name and change name or other to id +changeToID(){ + if test ${2%/*} = "/dev" + then + # 1.If get name,for example /dev/sda. add '.' to get route + name="../../${3}" + else + # 2.If get uuid or other, get name of it + name=$(ls -l $(find /dev/disk -name "$3") | awk '{print $NF}') + fi + + # Get id of it + id=$(ls -l /dev/disk/by-id | grep "$name" | awk '{print $(NF-2)}' | grep -E "wwn|eui") + + # Change to id + if test -n "$id" + then + sed -i 's%'$1'%'/dev/disk/by-id/$id'%' $4 + fi +} + +# Backup file +backupFile(){ + if test $1 != $2 + then + fileBak=`find /etc/ -maxdepth 1 -name "${3##*/}.bak-*"` + MAIL_SUBJECT=$MAIL_SUBJECT" $3" + rm -f $fileBak + fileBak=$3.bak-$5-$1 + cp $4 $fileBak + changed=1 + else + changed=0 + fi + rm -f $4 +} + +# 1.Modify fstab +if test $modifyFstab = "true" +then + filename="/etc/fstab" + cp /etc/fstab curFstab + md0Fstab=$(md5sum /etc/fstab | awk '{print $1}') + + cat /etc/fstab | grep -v -E '^$|#|dev/mapper' | while read line + do + # 1.Get original field from /etc/fstab + originalField=${line%% *} + + # 2.Get value of original field, such as uuid value, /dev/sda, /dev/disk/by-id/*** + originalFieldValue=${originalField##*=} + + # 3.Get core value of originalFieldValue.if /dev/disk/by-id/***,we get ***.it may be equal to originalFieldValue + coreValue=${originalFieldValue##*/} + + # Get id and change to id + changeToID $originalField $originalFieldValue $coreValue $filename + done + + # Backup file + mtimeFstab=`date "+%Y%m%d%H%M%S"` + md1Fstab=$(md5sum /etc/fstab | awk '{print $1}') + backupFile $md0Fstab $md1Fstab $filename curFstab $mtimeFstab + oldFstab=$fileBak + fstabChanged=$changed +fi + +# 2.Modify mdadm.conf +if test $modifyMdadmConf = "true" +then + # Start modify + filename="/etc/mdadm.conf" + cp /etc/mdadm.conf curMdadm.conf + md0MdadmConf=$(md5sum /etc/mdadm.conf | awk '{print $1}') + + cat /etc/mdadm.conf | grep -E '^device|^DEVICE' | while read line + do + arr=(${line#* }) + for element in ${arr[@]} + do + # 1.Get original field from /etc/mdadm.conf + originalField=$element + + # 2.Get value of original field, such as uuid value, /dev/sda, /dev/disk/by-id/*** + originalFieldValue=${originalField##*=} + + # 3.Get core value of originalFieldValue.if /dev/disk/by-id/***,we get ***.it can be equal to originalFieldValue + coreValue=${originalFieldValue##*/} + + # 4.Get id and change to id + changeToID $originalField $originalFieldValue $coreValue $filename + done + done + + # Backup file + mtimeMdadmConf=`date "+%Y%m%d%H%M%S"` + md1MdadmConf=$(md5sum /etc/mdadm.conf | awk '{print $1}') + backupFile $md0MdadmConf $md1MdadmConf $filename curMdadm.conf $mtimeMdadmConf + oldMdadmConf=$fileBak + mdadmConfChanged=$changed +fi + +# Send mail +if test "$MAIL_SUBJECT" != $default +then + MAIL_SUBJECT="${MAIL_SUBJECT#* } changed" + FULL_MESSAGE=` + if test $fstabChanged = 1 + then + echo "The following is the contents of this modification to the /etc/fstab file" + echo + echo "$(diff /etc/fstab $oldFstab)" + echo + fi + if test $mdadmConfChanged = 1 + then + echo "The following is the contents of this modification to the /etc/mdadm.conf file" + echo + echo "$(diff /etc/mdadm.conf $oldMdadmConf)" + fi + ` + exec $MAILER -s "${MAIL_SUBJECT}" $MAIL_ADDRESS <