Logo  

CS469 - Linux and Unix Administration and Networking

Partitioning and File-systems (cont)

The DD command:

> dd [if=input] [of=output] [bs=blksize] [count=#-blks] [conv=opts]

  • Convert/copy/create a file. To be used in several places to create empty files using the /dev/zero device (a virtual block device that returns unlimited null ('\0') bytes of data.)

Unix standard virtual character devices: (man 4 null zero random urandom full)

  • Useful for dd as well as many other things:

/dev/null

  • Reads return EOF (End Of File), writes are discarded.

/dev/zero

  • Reads return null characters ('\0'), writes are discarded.

/dev/random

  • Returns random data, sourced from an entropy pool -- can be very slow and block when the pool is empty. Writes update the entropy pool.

/dev/urandom

  • A source of pseudo-random data, not sourced from a pool. Writes update the PRNG (seed)

/dev/full

  • Writes fail with the error "no space left", reads return null bytes. Used for error-testing programs.

Swap partitions and files:

Swap files or page files are used by the virtual memory system of the OS to augment the amount of available memory by using the hard drive as a secondary level of memory. When additional memory is needed, unused memory is "swapped" to disk, freeing up main memory to be used in a different area.

# mkswap [options] device

  • Initialize a swap partition or file (formats the swap space.)

# swapon [-a] [device]

  • Enable swapping on all (-a) devices (present in the /etc/fstab file) or a single device.

# swapoff [-a] [device]

  • Disable swapping

# swaplabel [-L label] device

  • Show/set swap label (a label, discussed later allow us to refer to a device by a name (it's label), rather than some complicated block device enumeration (i.e. /dev/nvme0n1p0 for example.)

> free

  • Display amount of free virtual memory available, including swap space

Files:

/proc/swaps

  • Lists swap areas in use.

swap files vs partitions:

  • Swap space can be allocated to a file as well as a partition. There is very little difference in performance between the two, although partitions can be allocated at the beginning of the disk which is usually faster (for spinning disks anyway,) whereas a swap file could be located anywhere on the disk.
  • Swap files can be created and added/removed as needed.
  • Make sure swap files are mode 0600 so they are not readable by normal users.

Example:

# dd if=/dev/zero of=/swap bs=1M count=1k

  • Creates a blank 1 gigabyte file (/swap) (done as the super-user)

# mkswap /swap

  • Formats the blank file so it is a swap file.

# chmod 0600 /swap

  • Make sure only root can read/write to the file.

# swapon /swap

  • Enables swapping in the new swap file. Should be added to /etc/fstab to make the system start swapping on it on boot-up.

    Example /etc/fstab entry:
    /swap none swap defaults 0 0

Filesystems:

# mount [-o options] [-t type] device dir

  • mounts a filesystem located on device at dir in the directory hierarchy.
    -t forces it to use for the filesystem type, in the event that the kernel cannot automatically determine it.
    -o options Specific mount options.
Option What it does
* defaults Mount with default options (mostly used in /etc/fstab) implies: rw, suid, dev, exec, auto, nouser, and async.
* noatime Don't update atime records on files
* noauto Don't mount it explicitly (mostly used in /etc/fstab)
nodev Don't interpret device files on the file-system
* noexec Don't execute binaries on the file-system
nosuid Don't allow setuid binaries on this file-system
* remount Remounts the file-system with the new options
* ro Mount file-system in read-only mode
rw Mount FS in read-write mode (default)

* = remember these (may be useful in the future)

Example:
# mount -o ro,remount /dev/sda1 /mnt

  • Re-mounts the device /dev/sda1 at /mnt in read-only mode.

# umount dir | device`

  • un-mounts a filesystem.

    Examples:
    # umount /dev/sda1

    • Un-mounts the device /dev/sda1

    # umount /mnt

    • Un-mounts the device mounted at /mnt

# fuser [-uv] [-m filesystem] [-n tcp|udp port] [file]

  • Display processes (& users -u) using files, filesystems (-m) or sockets (-n).

    Example:
    # fuser -uvm /net

    • Display all users/processes using the /net filesystem.

# lsof

  • Like fuser, but more. Lists open files for all processes.