如果有去网上查过,基本上大多数的建议都是让我们编辑/etc/fstab文件,但我要在这里说的的是 个人强烈建议不要贸然编辑/etc/fstab文件 因为如果在编辑此文件时出现失误,你的操作系统很大可能无法正常启动 就像我们在AWS本次实践中的一样(AWS-EC2 + AWS-EFS),官方文档也强调,由于需要指定EFS网络文件系统,所以在EC2实例自动挂载时候需要加入特定参数 “ _netdev ”
否则,会出现EC2无响应并且EFS挂载失败的情况。
https://docs.aws.amazon.com/zh_cn/efs/latest/ug/mount-fs-auto-mount-onreboot.html
在实际情况下,我们采用的方法是利用rc.local文件,原理参考 https://www.cnblogs.com/defifind/p/9285456.html
关键操作步骤如下
找到 rc.local.service 文件,并打开它查看
vim rc.local.service --- # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # This unit gets pulled automatically into multi-user.target by # systemd-rc-local-generator if /etc/rc.local is executable. [Unit] Description=/etc/rc.local Compatibility ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes #----这里开始,下面是我们要手动加入的内容---# [Install] WantedBy=multi-user.target Alias=rc-local.serviceUbuntu18.04默认是没有这个文件的
vim /etc/rc.local --- #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #-----把你需要执行的命令写在这里,并确保它被写在“ exit0 ”之前---# mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-2de59714.efs.ap-southeast-2.amazonaws.com:/efs_from_amazon/ /mnt/efs_from_amazon/ echo "this just a test" > /auto_mount_test.txt exit 0完了重启
如果以上步骤都完成了,依然挂不上,可以先在/etc/rc.local写一些简单的任务,验证rc.local是否有效。 如果简单任务成功执行,说明是rc.local生效的时候nfs还没启动。 所以需要我们在任务前稍等一会,就像这样
sleep 8;mount -t nfs -o rw 172.31.151.191:/mnt/nfs_from_master03 /mnt/nfs_from_master03