<p>I'm trying to create a dynamic Azure Disk volume to use in a pod that has specific permissions requirements.</p>

<p>The container <a href="http://docs.grafana.org/installation/docker/#user-id-changes" rel="nofollow noreferrer">runs under the user id <code>472</code></a>, so I need to find a way to mount the volume with rw permissions for (at least) that user.</p>

<p>With the following <code>StorageClass</code> defined</p>

<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
provisioner: kubernetes.io/azure-disk
reclaimPolicy: Delete
volumeBindingMode: Immediate
metadata:
  name: foo-storage
mountOptions:
  - rw
parameters:
  cachingmode: None
  kind: Managed
  storageaccounttype: Standard_LRS
</code></pre>

<p>and this PVC</p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: foo-storage
  namespace: foo
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: foo-storage
  resources:
    requests:
      storage: 1Gi
</code></pre>

<p>I can run the following in a pod:</p>

<pre><code>containers:
  - image: ubuntu
    name: foo
    imagePullPolicy: IfNotPresent
    command:
      - ls
      - -l
      - /var/lib/foo
    volumeMounts:
      - name: foo-persistent-storage
        mountPath: /var/lib/foo
volumes:
  - name: foo-persistent-storage
    persistentVolumeClaim:
      claimName: foo-storage
</code></pre>

<p>The pod will mount and start correctly, but <code>kubectl logs &lt;the-pod&gt;</code> will show</p>

<pre><code>total 24
drwxr-xr-x 3 root root  4096 Nov 23 11:42 .
drwxr-xr-x 1 root root  4096 Nov 13 12:32 ..
drwx------ 2 root root 16384 Nov 23 11:42 lost+found
</code></pre>

<p>i.e. the current directory is mounted as owned by <code>root</code> and read-only for all other users.</p>

<p>I've tried adding a <code>mountOptions</code> section to the <code>StorageClass</code>, but whatever I try (<code>uid=472</code>, <code>user=472</code> etc) I get mount errors on startup, e.g.</p>

<pre><code>mounting arguments: --description=Kubernetes transient mount for /var/lib/kubelet/plugins/kubernetes.io/azure-disk/mounts/m1019941199 --scope -- mount -t ext4 -o group=472,rw,user=472,defaults /dev/disk/azure/scsi1/lun0 /var/lib/kubelet/plugins/kubernetes.io/azure-disk/mounts/m1019941199
Output: Running scope as unit run-r7165038756bf43e49db934e8968cca8b.scope.
mount: wrong fs type, bad option, bad superblock on /dev/sdc,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.
</code></pre>

<p>I've also tried to get some info from <a href="https://linux.die.net/man/8/mount" rel="nofollow noreferrer">man mount</a>, but I haven't found anything that worked.</p>

<p><strong>How can I configure this storage class, persistent volume claim and volume mount so that the non-root user running the container process gets access to write (and create subdirectories) in the mounted path?</strong></p>

<p>You need to define the <code>securityContext</code> of your pod spec like the following, so it matches the new running user and group id:</p>

<pre><code>securityContext:
  runAsUser: 472
  fsGroup: 472
</code></pre>

<p>The stable Grafana Helm Chart also does it in the same way. See <code>securityContext</code> under Configuration here: <a href="https://github.com/helm/charts/tree/master/stable/grafana#configuration" rel="nofollow noreferrer">https://github.com/helm/charts/tree/master/stable/grafana#configuration</a></p>
