Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Project Structure, Added Docker Compile, Makefile #7

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.sh text eol=lf
*.go text eol=lf
*.md text eol=lf
*.gitignore text eol=lf
*.gitattributes text eol=lf
makefile text eol=lf
71 changes: 70 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
dist/
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin
dist
dist/
bin/*.*
bin/**/*.*
*.DS_Store

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE
.idea
.idea/*.*
.idea/**/*.*
**/.idea/*.*
**/.idea/**/*.*
**/**/.idea/*.*
**/**/.idea/**/*.*
.idea/*.*
.idea/**/*.*
build/
*.db
*.db-journal

# web
swagger.zip
swagger/
swagger/*.*
routers/commentsRouter_controllers.go


.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

**/*.log
**/node_modules
.coverage
.nyc_output
docs/
out/
system-test/secrets.js
system-test/*key.json
*.lock
*-lock.js*
build/
.vscode
package-lock.json
__pycache__
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all: build-run

run-main:
go run cmd/fsqm/*.go

clear:
rm -rf bin

build:
go build -o bin/fsqm cmd/fsqm/*.go

clear-build: clear build

install: clear-build
go install cmd/fsqm/*.go

snapshot:
cd scripts && sudo chmod +x ./docker-deploy.sh
cd scripts && sudo bash ./docker-deploy.sh

build-run: clear build
./bin/fsqm
123 changes: 113 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,133 @@
[![codecov](https://codecov.io/gh/anexia-it/fsquota/branch/master/graph/badge.svg)](https://codecov.io/gh/anexia-it/fsquota)
[![Go Report Card](https://goreportcard.com/badge/github.com/anexia-it/fsquota)](https://goreportcard.com/report/github.com/anexia-it/fsquota)


fsquota is a native Go library for interacting with (Linux) filesystem quotas.
This library does **not** make use of cgo or invoke external commands, but rather interacts directly with the kernel interface by use of syscalls.
This library is maintained by the [Anexia](https://www.anexia-it.com/) R&D team.
fsquota is a native Go library for interacting with (Linux) filesystem quotas. This library does **not** make use of cgo
or invoke external commands, but rather interacts directly with the kernel interface by use of syscalls. This library is
maintained by the [Anexia](https://www.anexia-it.com/) R&D team.

## Portability

fsquota has been developed with Linux in mind and as such only supports Linux for now.
Support for other platforms may be added in the future.
fsquota has been developed with Linux in mind and as such only supports Linux for now. Support for other platforms may
be added in the future.

## fsqm

This repository also ships *fsqm*, a simple command line interface to filesystem quotas. *fsqm* provides the ability to retrieve user and group quota reports and management of user and group quotas.
This repository also ships *fsqm*, a simple command line interface to filesystem quotas. *fsqm* provides the ability to
retrieve user and group quota reports and management of user and group quotas.

*fsqm* can be obtained from [the releases page](https://github.com/anexia-it/fsquota/releases).

## fsqm(v0.1.3)

```shell
git clone https://github.com/anexia-it/fsquota.git && \
cd fsquota && \
go mod init github.com/anexia-it/fsquota && \
go mod tidy && \
go mod vendor && \
cd cmd/fsqm && \
go build && \
go install
```

## How to use it

[Initialize System](https://anexia.com/blog/en/filesystem-quota-management-in-go/) [a sample file](https://github.com/anexia-it/wad2018-quotactl/blob/master/quotactl.go)

We can start with simple setup first

```shell
sudo su
truncate -s 1G /tmp/test.ext4 && \
/sbin/mkfs.ext4 /tmp/test.ext4 && \
mkdir -p /mnt/quota_test && \
mount -o usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv1 /tmp/test.ext4 /mnt/quota_test && \
quotacheck -vucm /mnt/quota_test && \
quotacheck -vgcm /mnt/quota_test && \
quotaon -v /mnt/quota_test && \
lsblk
```

![lsblk should show all mouted items](https://i.imgur.com/aCQjKwM.png)

Thus, the file path will be `/dev/loop27`[/dev/loopN] from the initialize article.

We can run [a sample file](https://github.com/anexia-it/wad2018-quotactl/blob/master/quotactl.go)
get the sample file and run:

```shell
# special:path userid
go run *.go "/dev/loop27" "0"
```

It will yeild the details

```notes
Space (1K Blocks):
- hard limit: 0
- soft limit: 0
- usage : 20480
Inodes:
- hard limit: 0
- soft limit: 0
- usage : 2
```

Now we can add limit using fsquota:

Docker Compile `make snapshot`

```shell
# mounted username softL,hardL
./bin/fsqm-amd64 user set "/dev/loop27" "a" --files "1mb,2mb"
```

```notes
bytes:
- soft: 0 B
- hard: 0 B
- used: 12 MiB
files:
- soft: 1.0 M
- hard: 2.0 M
- used: 3
```

Which will create following files into `/mnt/quota_test` folder as following

```shell
sudo chmod -R 777 /mnt/quota_test # will give error ignore.
cd /mnt/quota_test && \
ls -lah
```

```shell
➜ /mnt cd /mnt/quota_test && \
ls -lah
total 13M
drwxrwxrwx 3 root root 4.0K Mar 16 22:53 .
drwxr-xr-x 4 root root 4.0K Mar 16 22:33 ..
-rw------- 1 root root 7.0K Mar 16 22:34 aquota.group
-rw------- 1 root root 7.0K Mar 16 22:34 aquota.user
drwxrwxrwx 2 root root 16K Mar 16 22:33 lost+found
```

Now create random files will not stop the user, not usre why developer may correct here:

```
dd if=/dev/urandom of=somefile.bin bs=1M count=1 && \
dd if=/dev/urandom of=somefile_x1.bin bs=1M count=1 && \
dd if=/dev/urandom of=somefile_x2.bin bs=1M count=1
```

## Issue tracker

Issues in fsquota are tracked using the corresponding GitHub project's [issue tracker](https://github.com/anexia-it/fsquota/issues).
Issues in fsquota are tracked using the corresponding GitHub
project's [issue tracker](https://github.com/anexia-it/fsquota/issues).

## Status

The current release is **v0.1.3**.

The current release is **v0.1.4**.

Changes to fsquota are subject to [semantic versioning](http://semver.org/).

Expand Down
10 changes: 10 additions & 0 deletions cmd/fsqm/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func isNumeric(s string) bool {
return true
}

// parseLimitsFlag
//
// Takes a flag name and parse flag by comma split
// Expected value in flag "SoftLimit,HardLimit"
//
// Dependent function humanize.ParseBytes
//
// Example:
// - "2 MB, 4mib" => Soft-limit : 2 mb, Hard-limit: 2 mb
// - "6mib, 1MB" => Soft-limit : 6 mb, Hard-limit: 1 mb
func parseLimitsFlag(cmd *cobra.Command, flagName string) (soft, hard uint64, present bool, err error) {
var flagString string
if flagString, err = cmd.Flags().GetString(flagName); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module github.com/anexia-it/fsquota

go 1.17

require (
github.com/docker/docker v1.13.1
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f
github.com/speijnik/go-errortree v1.0.0
github.com/spf13/cobra v0.0.1
github.com/stretchr/testify v1.2.1
golang.org/x/sys v0.0.0-20180312081825-c28acc882ebc
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.0 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/docker v1.13.1 h1:IkZjBSIc8hBjLpqeAbeE5mca5mNgeatLHBy3GO78BWo=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4 h1:qk/FSDDxo05wdJH28W+p5yivv7LuLYLRXPPD8KQCtZs=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f h1:QnRipjW3Mm+sgD+vyO87cb+1RLzoM0mGVTYClil7mQg=
github.com/hashicorp/go-version v0.0.0-20171129150820-4fe82ae3040f/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/speijnik/go-errortree v1.0.0 h1:xuDsI87cwIu4jlbWkDDt4t1WDyox1dSAzjln4rplULM=
github.com/speijnik/go-errortree v1.0.0/go.mod h1:u+UxW5C6dtS3peNNDwafbIs+Ccv3SfiOwqRjBngslaI=
github.com/spf13/cobra v0.0.1 h1:zZh3X5aZbdnoj+4XkaBxKfhO4ot82icYdhhREIAXIj8=
github.com/spf13/cobra v0.0.1/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.0 h1:oaPbdDe/x0UncahuwiPxW1GYJyilRAdsPnq3e1yaPcI=
github.com/spf13/pflag v1.0.0/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180312081825-c28acc882ebc h1:eCzBKjjvhDDXMoH5l7eA+YK1PEtyJ2QLj3f4IArr+zg=
golang.org/x/sys v0.0.0-20180312081825-c28acc882ebc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2 changes: 1 addition & 1 deletion quotactl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (d dqblk) toInfo() (info *Info) {
BytesUsed: d.dqbCurSpace,
}

//info.BytesUsed = info.BytesUsed
// info.BytesUsed = info.BytesUsed
*info.Limits.Bytes.hard = dqBlocksToBytes(*info.Limits.Bytes.hard)
*info.Limits.Bytes.soft = dqBlocksToBytes(*info.Limits.Bytes.soft)

Expand Down
49 changes: 49 additions & 0 deletions scripts/docker-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

WorkDir="$PWD/.."
BinDir=$WorkDir/bin
Platforms="linux"
Architectures="amd64"
GoVersion=1.17.8

echo ""
echo " ---- [Start] deploy for platforms ($Platforms [$Architectures]) [Start]-----"
echo ""
echo "Work dir : $WorkDir"
echo "Binaries dir : $BinDir"

docker run --rm -it -v "$WorkDir":/usr/src/myapp -v "$GOPATH":/go -w /usr/src/myapp golang:$GoVersion bash -c '
echo $PWD && \
rm -rf bin && \
mkdir bin && \
for GOOS in linux; do
for GOARCH in 386 amd64; do
export GOOS=$GOOS
export GOARCH=$GOARCH
echo "Building $GOOS-$GOARCH"
go build -o bin/fsqm-$GOARCH cmd/fsqm/*.go
done
done
' && \
echo "Build complete" && \
echo "" && \
echo "Permission adding:" && \
echo "chown -R root:root $BinDir" && \
echo "chmod -R 777 $BinDir" && \
echo "" &&\
chown -R root:root "$BinDir" && \
chmod -R 777 "$BinDir" && \
echo "" && \
echo "ls -la $BinDir:" && \
ls -la "$BinDir" && \
echo "" && \
echo "EnvPath" && \
export PATH=$PATH:"$BinDir" && \
echo "" && \
echo "\$Path:\"$BinDir\"" && \
"$BinDir"/fsqm-amd64 && \
echo "" && \
echo "export PATH=\$PATH:\"$BinDir\"" && \
echo "" && \
echo " ---- [End] deploy for all platforms ($Platforms [$Architectures]) [end]-----" && \
echo ""
Loading