Skip to content

Instantly share code, notes, and snippets.

@naviocean
Created September 18, 2019 07:10
Show Gist options
  • Select an option

  • Save naviocean/ce5b353d295176dd36bceedf72c6b545 to your computer and use it in GitHub Desktop.

Select an option

Save naviocean/ce5b353d295176dd36bceedf72c6b545 to your computer and use it in GitHub Desktop.
Virtual Disk For User

Create Virtual Disk For User on Ubuntu 16.04

1. Bash script

Create make_disk.sh

#!/bin/bash

V_DISK=/mnt/hdd10tb/virtual_disks

while test $# -gt 0; do
	case "$1" in
		-u|--user)
			U_DISK="$V_DISK/$2.ext4" 
			touch "$U_DISK"
			dd if=/dev/zero of=$U_DISK bs=1M count=1
			mkfs.ext4 $U_DISK
			e2fsck -f $U_DISK
			resize2fs -p $U_DISK 50G
			mkdir "/home/$2"
			chown "$2.$2" "/home/$2"
			mount -o loop,rw,usrquota,grpquota $U_DISK "/home/$2/" 
			exit 1
			;;
		-h|--help|*)
			echo "$package - attempt to capture frames"
			echo " "
			echo "$package [options] [arguments]"
			echo " "
			echo "options:"
			echo "-h, --help                show brief help"
			echo "-u, --user		username"
			exit 0
			;;
	esac
done

2. Make folder to store virtual disk

$ mkdir /mnt/hdd10tb/virtual_disks

3. Create virtual disk

$ sh make_disk.sh -u duong

The virtual disk named duong.ext4 will stored at /mnt/hddt10tb/virtual_disk/duong.ext4

4. Auto mouted

Edit file /etc/fstab Add this line to end of file

/mnt/hdd10tb/virtual_disks/duong.ext4 /home/duong ext4 defaults 0 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment