NAS自动挂载和关闭硬盘电源原理

家里自配的NAS,没用那些NAS方案,比如FreeNAS或者群辉。就自己捣鼓一套CentOS完事。ZFS之类的,考察下来不适合家用。我用Snapraid + MergerFS + Samba 方案。6 x 3T能耗有些高,待机75w。

其实平时可以关闭一些不用的硬盘,这样可以节能减耗。

 

方法和思路如下:

1.硬盘都挂于 /disk 目录下。用 autofs管理。

/dev/sdb1       2.7T  2.3T  273G  90% /disk/Photos_1
/dev/sda1       2.7T  707G  1.9T  28% /disk/Works_1
[root@localhost /]# cat /etc/autofs.conf  |grep -v ^#
[ autofs ]
timeout = 300
browse_mode = no
mount_nfs_default_protocol = 4
[ amd ]
dismount_interval = 300

这里主要是Timeout设置5分钟,假如5分钟不使用,就umount

[root@sh /]# cat /etc/auto.master | grep -v ^#
/misc   /etc/auto.misc
/disk   /etc/auto.disk
/net    -hosts
+dir:/etc/auto.master.d
+auto.master

auto.master文件,增加了  /disk   /etc/auto.disk

https://blog.csdn.net/m582445672/article/details/7885477

注意:/disk极其之下的目录无需创建,autofs会自动创见

[root@sh /]# cat /etc/auto.disk
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage

#cd             -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

# the following entries are samples to pique your imagination
Photos_1        -fstype=ext4           :/dev/disk/by-uuid/1d09e797-f127-40d5-940c-81a3dab1cf33
Works_1         -fstype=ext4           :/dev/disk/by-uuid/9aa81cbe-f543-4efd-9617-3edd3978a9b3


#linux          -ro,soft,intr           ftp.example.org:/pub/linux
#boot           -fstype=ext2            :/dev/hda1
#floppy         -fstype=auto            :/dev/fd0
#floppy         -fstype=ext2            :/dev/fd0
#e2floppy       -fstype=ext2            :/dev/fd0
#jaz            -fstype=ext2            :/dev/sdc1
#removable      -fstype=ext2            :/dev/hdd

增加了Photos_1 和 works_1 ,uuid,用命令 blkid获取

[root@sh /]# blkid
/dev/sdb1: LABEL="Photos & Videos" UUID="1d09e797-f127-40d5-940c-81a3dab1cf33" TYPE="ext4" PARTLABEL="primary" PARTUUID="23f26680-8d5a-474f-ad17-910794e34bf3"
/dev/loop0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: LABEL="Works" UUID="9aa81cbe-f543-4efd-9617-3edd3978a9b3" TYPE="ext4" PARTLABEL="primary" PARTUUID="4103d5db-eb71-4eb3-95b8-80d2358b3ce9"

2.samba 设置共享

实际操作,我用了MegerFS合并,这里不展开了,具体自己百度使用方法。

[Home Photos and Videos]

	comment = 家庭影像存档(保护区)
	path = /disk/Photos_1
	public = yes
#	only guest = yes
	writable = yes
	printable = no
	
	create mode = 0666
	directory mode = 0777

[Works]
	comment = 工作
	path = /disk/Works_1
	public = yes
	writable = yes
	printable = no

	create mode = 0666
	directory mode = 0777

3. 以后就简单了。写一个脚本监测盘,如果没有被samba引用,就会自动卸载。只要是卸载的硬盘,就关闭硬盘电机节电。然后写入crontab,每隔一段时间自动监测。

[root@sh ~]# cat /root/script/powersave.sh
#!/bin/bash
export PATH=$PATH:/usr/sbin:/usr/bin

#partion UUID
aryDiskUuid[0]="9aa81cbe-f543-4efd-9617-3edd3978a9b3"
aryDiskUuid[1]="1d09e797-f127-40d5-940c-81a3dab1cf33"

for partuuid in ${aryDiskUuid[@]} ; do

        if ls -l /dev/disk/by-uuid | grep -qi $partuuid ; then
#           lrwxrwxrwx 1 root root 10 Mar  6 12:48 9aa81cbe-f543-4efd-9617-3edd3978a9b3 -> ../../sda1

                m_part=/dev/`ls -l /dev/disk/by-uuid | grep -i $partuuid | cut -d'/' -f3`
                m_disk=`echo $m_part | cut -b 1-8`

                if mount | grep -qi $m_part ;then
#                       cpupower  frequency-set -g conservative > /dev/null

                        if systemctl status smartd | grep -q inactive ;then
                                systemctl start smartd
                        fi

                        continue
                else
                        #not mounted
                        hdparm -q -Y $m_disk
                        systemctl stop smartd
#                       cpupower frequency-set -g powersave > /dev/null
                fi
        fi
done

或者,直接设置半小时停机

[root@sh script]# hdparm -S 241 /dev/sda

/dev/sda:
 setting standby to 241 (30 minutes)

  -S     Put the drive into idle (low-power) mode, and also set the standby (spindown) timeout for the drive.  This timeout value is used by the drive to determine how long to wait (with  no  disk activity)  before  turning  off  the  spindle  motor to save power.  Under such circumstances, the drive may take as long as 30 seconds to respond to a subsequent disk access, though most drives are much quicker.  The encoding of the timeout value is somewhat peculiar.  A value of zero means "timeouts are disabled": the device will not  automatically  enter  standby  mode.
            Values  from  1  to 240 specify multiples of 5 seconds, yielding timeouts from 5 seconds to 20 minutes.

           Values from 241 to 251 specify from 1 to 11 units of 30 minutes, yielding timeouts from 30 minutes to 5.5 hours.  

            A value of 252 signifies a timeout of 21 minutes. A value of 253 sets a vendor-defined timeout period between 8 and 12 hours, and the value 254 is reserved.
              255 is interpreted as 21 minutes plus 15 seconds.  Note that some older drives may have very different interpretations of these values.

 参考资料:

https://blog.csdn.net/lakeheart879/article/details/53494878

https://www.phpfans.net/ask/fansa1/9870936185.html

https://www.right.com.cn/forum/thread-142352-1-1.html

https://www.cnblogs.com/meiyu2016/p/5928774.html?utm_source=itdadao&utm_medium=referral

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值