# running a script on boot.



## luli (Jun 7, 2010)

I had created a zfs array, and I am using it as a /music partition (/ is on another disk, using the UFS file system).
I have created a little script, zfstart.sh:

```
#!/bin/sh
/sbin/zfs set mountpoint=/music tank/music
```
runinig 
	
	



```
./zfstart.sh
```
 mounts the /music partition and it is ready to be used.
I would like to run it automatically on boot. For that purpose, I have tried to use the following script:


```
#!/bin/sh

zfs-mount_enable=${slpd_enable-"NO"}
zfs-mount_flags=${zfs-mount_flags-"-p /var/run/zfs-mount.pid"}
```


```
. /etc/rc.subr

name="zfs-mount"
rcvar=$(set_rcvar)

#command='/sbin/zfs set mountpoint=/music tank/music'
command="./root/zfstart.sh"
load_rc_config "${name}"

run_rc_command "$1"
```

I have put the script in /usr/local/etc/rc.d with the name zfs-mount, fixed the right permissions for it, and added the line zfs-
	
	



```
mount_enable="YES"
```
 to /etc/rc.conf.
However, it does not work, and all I get is the following message: 

```
zfs-mount_enable=YES: not found
```
What am I doing wrong? How can I fix it?

```
uname -a
FreeBSD nisooi.workgroup 8.0-RELEASE FreeBSD 8.0-RELEASE #0: Sat Nov 21 15:48:17 UTC 2009     [email]root@almeida.cse.buffalo.edu[/email]:/usr/obj/usr/src/sys/GENERIC  i386
```


```
nisooi# df
Filesystem  1K-blocks      Used     Avail Capacity  Mounted on
/dev/ad0s2a  30462636   5617412  22408214    20%    /
devfs               1         1         0   100%    /dev
tank/music  383941248 116169728 267771520    30%    /music
```


----------



## SirDice (Jun 7, 2010)

Why don't you just use /etc/fstab?


----------



## DutchDaemon (Jun 7, 2010)

And [code] tags?


----------



## luli (Jun 7, 2010)

*What entry would you suggest to put there?*



			
				SirDice said:
			
		

> Why don't you just use /etc/fstab?



It is not clear to me.


----------



## luli (Jun 7, 2010)

I don't know what entry to put there.


----------



## vrachil (Jun 8, 2010)

if you set the mountpoint for a dataset once, there is no need to set it every time.
Unless you change it at somepoint.


----------



## phoenix (Jun 8, 2010)

Add *zfs_enable="YES"* into /etc/rc.conf then set the mountpoint property for the dataset: `# zfs set mountpoint=/music tank/music`.

Finally, add the following to /boot/loader.conf:

```
zfs_load="YES"
```

After that, /music will be automatically mounted at every boot.


----------



## luli (Jun 8, 2010)

Thanks for your reply.
I have added the following line to /etc/fstab:

```
zfs set mountpoint=/music tank/music
```
(your other suggestions were already implemented, off course).
On next boot I got the following message:

```
fstab /etc/fstab:5: Inappropriate file type or format.
```
/music had not been mounted.


----------



## SirDice (Jun 8, 2010)

Follow phoenix' advice, forget about fstab. 

If you do add something to fstab make sure it's correct, see fstab(5).


----------



## luli (Jun 8, 2010)

vrachil said:
			
		

> if you set the mountpoint for a dataset once, there is no need to set it every time.
> Unless you change it at somepoint.


This is clearly not working in my case. I need to re-run
`zfs set mountpoint=/music tank/music` each time after I boot.


----------



## SirDice (Jun 8, 2010)

luli said:
			
		

> This is clearly not working in my case. I need to re-run
> zfs set mountpoint=/music tank/music each time after I boot.



That's because zfs isn't loaded at boot. Follow phoenix' advice and zfs will be loaded at boot.


----------



## yks (Jun 8, 2010)

*luli*:
Well, as to your primary post, firstly the variable names may not contain '-', so the variable name "zfs-mount_enable" is simply invalid. Replacing the minus with underscore would do.
Secondly, the line 

```
command="./root/zfstart.sh"
```
is apparently pointing to a nonexistent directory/file ./root/zfstart.sh, rather than to (probably intended) /root/zfstart.sh.
After correcting these small mistakes, I think, the script from /usr/local/etc/rc.d/ should work.
Just a small notice: if you put a script in  /usr/local/etc/rc.d/, don't forget that it will also be executed on shutdown, so it's best to check the parameter for the script and execute your command only in the case when it is equal to "start", or set up "start_cmd"/"stop_cmd" functions .


----------



## Carpetsmoker (Jun 8, 2010)

You can use rc.local. See rc(8).

In this particular case, using fstab is a cleaner solution though (As mentioned before).


----------



## luli (Jun 8, 2010)

Thank you all.
first, to clear things up, Here is my /etc/rc.conf:


```
hostname="nisooi.workgroup"
ifconfig_re0="DHCP"
keymap="us.iso"
sshd_enable="YES"
dbus_enable="YES"
hald_enable="YES"
snddetect_enable="YES"
mixer_enable="YES"
linux_enable="YES"
avahi_daemon_enable="YES"
samba_enable="YES"
winbindd_enable="YES"
cups_enable="YES"
avahi_daemon_enable="YES"
zfs_mount_enable="YES"
moused_port="/dev/mse0"
moused_type="auto"
moused_enable="YES"
moused_ums0_flags="-F 200 -A 1.5,2.0 -a 0.7 -r high -V"
```

and this is /boot/loader.conf

```
ahci_load="YES"
sdhci_load="YES"
sound_load="YES"
snd_hda_load="YES"
snd_uaudio_load="YES"
libiconv_load="YES"
libmchain_load="YES"
cd9660_iconv_load="YES"
msdosfs_iconv_load="YES"
ntfs_load="YES"
ntfs_iconv_load="YES"
udf_load="YES"
udf_iconv_load="YES"
zfs_load="YES"
tmpfs_load="YES"
sem_load="YES"
nvidia_load="YES"
```

therefor the problem is not there.

Secondly, I have found this in /var/log/mount.today:


```
/dev/ad0s2a		/			ufs	rw		1 1
devfs			/dev			devfs	multilabel 	0 0
tank/music		/music			zfs	noatime		0 0
```

So I have tried to enter the third line into /etc/fstab - but again I have got the "fstab Inappropriate line" error.

Finally, I have changed the  boot script name to zfs_mount, and it looks now like this:

```
#!/bin/sh

zfs_mount_enable=${slpd_enable-"NO"}
zfs_mount_flags=${zfs-mount_flags-"-p /var/run/zfs-mount.pid"}

. /etc/rc.subr

name="zfs-mount"
rcvar=$(set_rcvar)

#command='/sbin/zfs set mountpoint=/music tank/music'
command="/root/zfstart.sh"
load_rc_config "${name}"

run_rc_command "$1"
```

The error message has indeed disappeared, but the partition is stile not mounted on boot.


----------



## SirDice (Jun 8, 2010)

luli said:
			
		

> ```
> zfs_mount_enable=${slpd_enable-"NO"}
> ```


This is wrong. It checks if slpd_enable (ldap service) has been set, if not it sets zfs_mount_enable to NO.


----------



## luli (Jun 8, 2010)

SirDice said:
			
		

> This is wrong. It checks if slpd_enable (ldap service) has been set, if not it sets zfs_mount_enable to NO.


Thanks. I have corrected it, and zfs_mount looks now like that:



```
#!/bin/sh

zfs_mount_enable=${zfs_mount_enable-"NO"}
zfs_mount_flags=${zfs_mount_flags-"-p /var/run/zfs_mount.pid"}

. /etc/rc.subr

name="zfs_mount"
rcvar=$(set_rcvar)

#command='/sbin/zfs set mountpoint=/music tank/music'
command="/root/zfstart.sh"
load_rc_config "${name}"

run_rc_command "$1"
```
but yet, /music is not mounted on boot.


----------



## vrachil (Jun 8, 2010)

Have you read what Phoenix suggested?
You don't have zfs_enable="YES" in rc.conf and that is your problem.
Add that line, and set zfs_mount_enable="NO". I'd bet it will work without any more tuning.


----------



## DutchDaemon (Jun 8, 2010)

@luli: start using proper formatting a.s.a.p.!


----------



## luli (Jun 8, 2010)

Thanks!
It has solved the problem.


----------

