Recently a friend of mine asked me if it was possible to limit the amount of space taken up by a user on a samba share without using disk quotas. I assumed it could be done through the smb.conf but since I have never worked with samba I wasn't sure. A bit of research here and there came to nothing so I decided to write a script and solve this problem the dirty way. My friend manages a small network where users run workstations with MS Windows.
First things first, let's create a file containing the usernames and the amount of space we want to give them. Something like this:
# cat /tmp/smbusers.txt maria.perez,5 arnaldo.hernandez,5 gustavo.flores,10 jesus.boss,5 marta.hernandez,5 wilkneman.pascoski,5 tatiana.siu,5 alexandra.delarosa,5
Using this information I need to create the configuration to be appended to the smb.conf; it needs to have the following format:
[alexandra.delarosa] comment = alexandra.delarosa with 5MB path = /mnt/smb_discs/alexandra.delarosa read only = no browseable = yes guest ok = yes [arnaldo.hernandez] comment = arnaldo.hernandez with 5MB path = /mnt/smb_discs/arnaldo.hernandez read only = no browseable = yes guest ok = yes [gustavo.flores] comment = gustavo.flores with 10MB path = /mnt/smb_discs/gustavo.flores read only = no browseable = yes guest ok = yes
Here is the script which basically reads the list of users and how much space they are allowed to use and creates disk images using the command dd, then it mounts each of these images as loop back devices and creates a file with the information that has to be appended to the smb.conf file.
LISTASMBUSERSS=`cat /tmp/smbusers.txt|sort`
PATH_TO_DISKS="/smb_disks/smb_drives"
MOUNT_SMB="/mnt/smb_discs"
TMPSAMBACONF="/tmp/samba.virt.conf"
echo > $TMPSAMBACONF
mkdir -p `echo $PATH_TO_DISKS`
for X in $LISTASMBUSERSS
do echo $X | awk -F , '{print "assigning " $2 "MB to smb_user "$1}'
SMBUSERS=`echo $X | awk -F , '{print $1}'`
PRE_SMBQUOTA=`echo $X | awk -F , '{print $2}'`
let SMBQUOTA=`echo $PRE_SMBQUOTA`*1024
dd if=/dev/zero of=$PATH_TO_DISKS/$SMBUSERS.img bs=1024 count=$SMBQUOTA
/sbin/mke2fs -L $SMBUSERS -j $PATH_TO_DISKS/$SMBUSERS.img
mkdir -p $MOUNT_SMB/$SMBUSERS
mount -t ext3 $PATH_TO_DISKS/$SMBUSERS.img -o loop $MOUNT_SMB/$SMBUSERS
echo "[$SMBUSERS]
comment = `echo $SMBUSERS" with "$PRE_SMBQUOTA"MB"`
path = /mnt/smb_discs/$SMBUSERS
read only = no
browseable = yes
guest ok = yes
" >> $TMPSAMBACONF
done
echo "Used space in $PATH_TO_DISKS: "
du -smh $PATH_TO_DISKS/*.img
du -smh $PATH_TO_DISKS/
mount -l | grep $MOUNT_SMB
echo "CHECK $TMPSAMBACONF AND ADD IT TO YOUR /etc/samba/smb.conf"
My friend would just have to add the output of the script located in /tmp/samba.virt.conf to his /etc/samba/smb.conf and restart the service.
Please note that I have never worked with Samba before so if you know a better way to do this without using disk quotas please let me know.
Original Article (spanish version): Limitar el espacio de una carpeta sin usar cuotas en Samba.