-1

I want to create a directory and provide user permissions in /mnt/resource folder on RHEL Azure VM during startup of the VM.

df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        32G   12G   20G  38% /
devtmpfs         56G     0   56G   0% /dev
tmpfs            56G     0   56G   0% /dev/shm
tmpfs            56G  8.4M   56G   1% /run
tmpfs            56G     0   56G   0% /sys/fs/cgroup
/dev/sda1       497M  103M  394M  21% /boot
/dev/sdb1       221G  2.3G  207G   2% /mnt/resource
tmpfs            12G     0   12G   0% /run/user/1000

I have tried following. But it doesn't help me.

  1. a. chmod +x /etc/rc.d/rc.local

b. Add the following lines at end of file in /etc/rc.d/rc.local

sudo mkdir -p /mnt/resource/sample

sudo chown -R sshuser:sshuser /mnt/resource/sample

c. Restart the VM.

Result:

sample directory not created in /mnt/resource/

  1. a. Add following in /etc/systemd/system/mount.service

[Unit]
Description=Description for sample script goes here
Requires=network.target
After=network.target

[Service]
Type=simple
ExecStart=/home/sshuser/mount.sh
TimeoutStartSec=0
User=root

[Install]
WantedBy=multi-user.target

b. Add following in /home/sshuser/mount.sh

sudo mkdir -p /mnt/resource/sample

sudo chown -R sshuser:sshuser /mnt/resource/sample

c. chmod +x /home/sshuser/mount.sh

d. systemctl daemon-reload

e. systemctl enable mount.service

f. systemctl start mount.service

Stop and Started the Azure VM

Result:- sample directory not created in /mnt/resource/

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
Galet
  • 5,853
  • 21
  • 82
  • 148
  • I assume you want to create a directory in /mnt/resource and set user permissions to it. I try and it comes true no difficult. So what you really want? – Charles Xu Jul 11 '18 at 01:57
  • That only I need. But I want to create a directory in /mnt/resource and set user permissions to it during every time startup of the Azure RHEL VM. Did you tried this? – Galet Jul 11 '18 at 03:50
  • @CharlesXu-MSFT Did you tried the creation of directory during startup of VM? – Galet Jul 11 '18 at 05:19
  • I try it in running time and then restart the VM. Why do you want to do it during startup time? – Charles Xu Jul 11 '18 at 05:23
  • @CharlesXu-MSFT I also try it in VM running time and it works for me. After I deallocate the vm and then start the vm, /mnt/resource/sample directory is not available. So I need the directory during startup time. I deallocate and start the vm often. – Galet Jul 11 '18 at 05:57
  • @CharlesXu-MSFT Have you tried my above scenario? – Galet Jul 11 '18 at 06:37
  • I'm trying and I suggest you can make your mount.sh script run during startup time. – Charles Xu Jul 11 '18 at 06:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174772/discussion-between-karan-and-charles-xu-msft). – Galet Jul 11 '18 at 06:42

1 Answers1

0

I followed the above chat link and found that @karan came across a good answer. Merged it with what I had and here's the code in full for anyone who needs this:

#!/bin/bash
# set this to your /mnt/resource/______ directory name
MY_DIRECTORY=sample

#wait for /mnt/resource to be created
while [ `mount | grep /mnt/resource | wc -l` -lt 1 ];
do
    logger "Waiting five more seconds for /mnt/resource to be created by waagent..."
    sleep 5
done

# test if folder is missing. If so, create it, set SE context & ownership
if [ ! -d /mnt/resource/${MY_DIRECTORY} ]; then
    mkdir --context=system_u:object_r:httpd_sys_rw_content_t:s0 /mnt/resource/${MY_DIRECTORY}
    chown apache: /mnt/resource/${MY_DIRECTORY}
    logger "/mnt/resource/${MY_DIRECTORY} created, SE context and owner & group set"
else
    logger "/mnt/resource/${MY_DIRECTORY} already exists."
fi

As indicated this folder is for apache but change owner, group and se context to your needs.

legatoproteus
  • 27
  • 1
  • 7