File: partition.sh

package info (click to toggle)
scap-security-guide 0.1.76-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 110,644 kB
  • sloc: xml: 241,883; sh: 73,777; python: 32,527; makefile: 27
file content (59 lines) | stat: -rw-r--r-- 1,552 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

PARTITION="/root/new_partition"

create_partition() {
	# Some scenarios re-mount this device as '/tmp', therefore its size
	# needs to be enough for the produced logs.
	dd if=/dev/zero of=$PARTITION bs=1M count=200
	mkfs.ext2 -F $PARTITION
}

# $1: The mount point
# $2: The type of file system
# $3: The additional mount options
make_fstab_given_partition_line() {
	local _mount_point="$1" _type="$2" _additional_mount_options="$3"
	test -z "$_additional_mount_options" || _additional_mount_options=",$_additional_mount_options"
	printf "%s     %s     %s     rw%s     0 0\n" "$PARTITION" "$_mount_point" "$_type" "$_additional_mount_options" >> /etc/fstab
}

# $1: The mount point
make_fstab_correct_partition_line() {
	make_fstab_given_partition_line "$1" "ext2" "nodev,noexec,nosuid"
}

make_fstab_bind_partition_line() {
	make_fstab_given_partition_line "$1" "none" "nodev,noexec,nosuid,bind"
}

# $1: The mount point
mount_partition() {
	mkdir -p "$1"
	mount --target "$1"
}

mount_bind_partition() {
	mkdir -p "$1"
	mount -B "$PARTITION" "$1"
}

umount_partition() {
	local _mount_point="$1"
	if mount | grep -E "\s+${_mount_point}\s+"; then
		echo "'$_mount_point' is mounted, will unmount it.."
		umount -nl "$_mount_point"
	else
		echo "'$_mount_point' is not mounted, skipping"
	fi
}

# $1: The path to umount and remove from /etc/fstab
clean_up_partition() {
    path="$1"
    escaped_path=${path//$'/'/$'\/'}
    sed -i "/${escaped_path}/d" /etc/fstab
    if mountpoint -q -- "${path}"; then
        umount -l ${path}
    fi
}