Storage - Filesystem


Storage - Filesystem

For the default Debian distribution, the SD card is organised into two main sections, known as partitions. These partitions  split the SD card to 2 areas, which can be seen by fdisk.

sudo -fdisk -l will show this at the end of the output:

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   122879   114688   56M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      122880 31211519 31088640 14.8G 83 Linux

The first partition on the disk is a small (56 MB) partition formatted as VFAT, the same format used by Microsoft Windows for removable drives. This is mounted, (or made accessible), by Linux in the /boot directory and contains all the files required to configure the Raspberry Pi and to load Linux itself.

The second partition is larger and formatted as EXT4, a native Linux file system designed for high-speed access and data safety. This partition contains all the programs, the users’ files and any software that you install.

=======================================

File system

"On a UNIX system, everything is a file; if something is not a file, it is a process"

A file system is a hierarchical tree structure of files and directories. Some tasks are performed more efficiently on a file system than on each directory within the file system. For example, you can back up, move, or secure an entire file system. An example of how this works is the /media folder. By default, if you plug a USB drive into your Raspberry Pi, Raspbian will automatically create a folder inside /media. This folder will represent the contents of your USB drive. If you have more than one storage device attached, they will all appear in /media.

This filesystem hierarchy starts from one place, the root directory: /. Every file and device in Linux is contained within this single hierarchy. Listing the directories under "/" will show the content of the SD card, and by default these folders are there:


• /      <--The root of the Linux filesystem.
• bin    <--Operating system-related binary files are stored here.
• boot   <--Contains Linux kernel and other packages, configuration files that Raspberry Pi uses to boot
• dev    <--All devices connected to the Pi— like storage devices, sound card...—can be accessed from here
• etc    <--Contains configuration files for software packages, and user related files
• home   <--Each user has a home directory here
• lib    <--Software libraries contain shared code that is shared between applications (these files end in .so)
• lost+found <--This is a special directory where file fragments are stored if the system crashes.
• media  <--Special directory for removable storage devices, like USB memory sticks or external CD drives.
• mnt    <--This folder is used to manually mount storage devices, such as external hard drives.
• opt    <--Stores optional software that is not part of the operating system itself.
• proc   <--This contains information about running programs which are known in Linux as processes.
/root    <--This folder is similar to the /home, but only contains files for the root user.
• selinux <--Files related to Security Enhanced Linux
• sbin   <--Stores special binary files, primarily used by the root (superuser) account for system maintenance.
• sys    <--This directory is where special operating system files are stored.
• tmp    <--Any temporary files are normally stored in /tmp. These files are usually deleted on reboot.
• usr    <--User-accessible programs with their libraries and documentation are here
• var    <--contains all the logs and other files that are constantly changing

The /proc and /sys are "virtual file sytems", and the main purpose is that communication to the kernel is going through by files there. Information can be gathered from the kernel by reading those virtual files, and sending information to the kernel is possible by writing to those virtual files.
/sys in particular is designed to provide access to internal kernel objects, especially those representing the various devices in the system.

================================

mount - umount

On Linux files are organized in a single tree-like hierarchy of directories. The / directory is called the “root directory”; all additional directories are sub-directories within this root. “Mounting” is the action of including the content of a device (often a hard drive) into the system’s general file tree. The root filesystem is always mounted at boot by the kernel, other filesystems can be mounted later manually as well. For example, if you use a separate hard drive to store personal data, this disk can be “mounted” in the /mydata directory. If you want to remove it, it must be unmounted (removed from the file tree).

Make sure to use the "nofail" option  for external drives that might be unplugged when you boot, because systemd really ensures that all mount points that must be automatically mounted
are actually mounted before letting the boot process continue to its end. Note that you can combine this with x-systemd.device-timeout=5s to tell systemd to not wait more than 5 seconds for the device to appear.

auto mounting
The am-utils package provides the amd auto-mounting utility, able to mount removable media on demand when a user attempts to access their usual mount point. It will unmount these devices when no process is accessing them any longer. Other auto-mounting utilities exist, such as automount in the autofs package. Some graphical desktop environments work together with udisks, and can automatically mount removable media when they are connected.

================================

/etc/fstab (File System Table)

This file lists file systems with their settings. It tells to the Operating System what file systems should be mounted on which mount points.

pi@raspberrypi:~ $ sudo cat /etc/fstab
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
# use  dphys-swapfile swap[on|off]  for that

Each column means following:
device to mount: the location of the device to be mounted
mount point: the directory where the device should be accessible
type: file system type (ext4, ext3, ntfs...)
options: some common options
    – rw or ro: device will be mounted with read/write or read-only permissions
   – noauto: deactivates automatic mounting on boot
   – nofail: allows the boot to proceed even when the device is not present.
   – user: authorizes all users to mount this filesystem (otherwise would be restricted to the root user)
   – defaults: means the group of default options: rw, suid, dev, exec, auto, nouser and async
backup: usually 0 (means fs should not be dumped in the event of a system problem)
fsck order: if the file system should be checked by the fsck on boot and in which order
(0 means no check, the root filesystem should have the value 1, other permanent filesystems the value 2)

To make the system mount the new partition automatically, add this line at the end of /etc/fstab:
/dev/mmcblk0p3  /storage    ext4     defaults   0     2

(there should be tab, between each field)

================================

fdisk        <--shows partitions
mount        <--shows what is mounted
df -h        <--shows filesystem usage

================================

Creating  a new partition on an SD card
(By creating a new partition in the empty space at the end of the card, the sd card can be used to store any large files without using an external storage device)

1. sudo fdisk -l             <--list storage devices/partitions (note the SD card, like /dev/mmcblk0)
2. sudo cfdisk /dev/mmcblk0  <--start cfdisk, a partition management tool
3. choose "Free space"       <--with cursor keys highlight line "Free space" (it shows the size as well)
4. choose "New"              <--after pressing "Enter" the size of the partition can be modified
5. choose "primary"          <--primary or extended, choose primary, and new partition is created like p3
7. choose "Write"            <--the partition table need to be written to the disk
8. sudo reboot               <--however the partition table is already updated with this new partition (fdisk will list it), but it is not visible to the OS until it is reloaded (with reboot it will be reloaded)


================================

Creating a filsystem on a partition
(Before files can be stored on a new partition, a file system needs to be created by formatting the partition using mkfs)

1. sudo fdisk -l                         <--list the storage devices/partitions. (we will use /dev/mmcblk0p3)
2. sudo mkfs.ext4 /dev/mmcblk0p3         <--create an EXT4 file system in the partition
3. sudo mkdir /mystorage                 <--create mount point (directory) for our new filesystem
4. sudo mount /dev/mmcblk0p3 /mystorage  <--mount the new filsystem
5. sudo chown -R pi /mystorage           <--change the owner of the filesystem to the user "pi", -R: recursive
6. sudo chgrp -R users /mystorage        <--change the group membership  to "users", -R: recursive
7. sudo chmod -R g+w /mystorage          <--all members of the group (g) will be able to write (+w) there

The new partition is now ready for use, but  it is not automatically mounted after reboot. For automatic mount during reboot add this line at the end of /etc/fstab:
/dev/mmcblk0p3  /storage    ext4     defaults   0     2

(there should be tab, between each field, and after a reboot it can be checked by "df -h")

========================

No comments:

Post a Comment