\section{Block filesystems}

\begin{frame}
  \frametitle{Block vs. flash}
  \begin{itemize}
  \item Storage devices are classified in two main types: {\bf block
      devices} and {\bf flash devices}
    \begin{itemize}
    \item They are handled by different subsystems and different
      filesystems
    \end{itemize}
  \item {\bf Block devices} can be read and written to on a per-block
    basis, without erasing, and do not wear out when being used for a
    long time
    \begin{itemize}
    \item Hard disks, floppy disks, RAM disks
    \item USB keys, Compact Flash, SD card, these are based on
      flashes, but have an integrated controller that emulates a block
      device
    \end{itemize}
  \item {\bf Flash devices} can be read, but writing requires erasing,
    and often occurs on a larger size than the “block” size
    \begin{itemize}
    \item NOR flash, NAND flash
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Block device list}
  \begin{itemize}
  \item The list of all block devices available in the system can be
    found in \code{/proc/partitions}\\
\begin{verbatim}
$ cat /proc/partitions
major minor #blocks name

8 0 312571224 sda
8 1 303903744 sda1
8 2 1         sda2
8 5   8665088 sda5
\end{verbatim}
  \item And also in \code{/sys/block/}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Traditional block filesystems}
  Traditional filesystems
  \begin{itemize}
  \item Can be left in a non-coherent state after a system crash or
    sudden poweroff, which requires a full filesystem check after
    reboot.
  \item \code{ext2}: traditional Linux filesystem\\
    (repair it with \code{fsck.ext2})
  \item \code{vfat}: traditional Windows filesystem\\
    (repair it with \code{fsck.vfat} on GNU/Linux or Scandisk on
    Windows)
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Journaled filesystems}
  \begin{columns}
    \column{0.4\textwidth}
    \begin{itemize}
    \item Designed to stay in a correct state even after system crashes
      or a sudden poweroff
    \item All writes are first described in the journal before being
      committed to files
    \end{itemize}
    \column{0.6\textwidth}
    \includegraphics[width=\textwidth]{slides/sysdev-block-filesystems/journal.pdf}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Filesystem recovery after crashes}
  \begin{columns}
    \column{0.6\textwidth}
    \includegraphics[width=\textwidth]{slides/sysdev-block-filesystems/journal-recovery.pdf}
    \column{0.4\textwidth}
    \begin{itemize}
    \item Thanks to the journal, the filesystem is never left in a
      corrupted state
    \item Recently saved data could still be lost
    \end{itemize}
  \end{columns}
\end{frame}

\begin{frame}
  \frametitle{Journaled block filesystems}
  Journaled filesystems
  \begin{itemize}
  \item \code{ext3}: \code{ext2} with journal extension\\
    \code{ext4}: the new generation with many improvements.\\
    Ready for production. They are the default filesystems for all
    Linux systems in the world.
  \item The Linux kernel supports many other filesystems:
    \code{reiserFS}, \code{JFS}, \code{XFS}, etc.  Each of them have
    their own characteristics, but are more oriented towards server or
    scientific workloads
  \item \code{Btrfs} (``Butter FS'')\\
    The next generation. In mainline but still experimental.
  \end{itemize}
  We recommend \code{ext2} for very small partitions ($<$ 5 MB),
  because other filesystems need too much space for metadata
  (\code{ext3} and \code{ext4} need about 1 MB for a 4 MB partition). 
\end{frame}

\begin{frame}
  \frametitle{Creating ext2/ext3 volumes}
  \begin{itemize}
  \item To create an empty ext2/ext3 filesystem on a block device or
    inside an already-existing image file
    \begin{itemize}
    \item \code{mkfs.ext2 /dev/hda3}
    \item \code{mkfs.ext3 /dev/sda2}
    \item \code{mkfs.ext2 disk.img}
    \end{itemize}
  \item To create a filesystem image from a directory containing all
    your files and directories
    \begin{itemize}
    \item Use the \code{genext2fs} tool, from the package of the same name
    \item \code{genext2fs -d rootfs/ rootfs.img}
    \item Your image is then ready to be transferred to your block
      device
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Mounting filesystem images}
  \begin{itemize}
  \item Once a filesystem image has been created, one can access and
    modifies its contents from the development workstation, using the
    {\bf loop} mechanism
  \item Example:\\
    \code{genext2fs -d rootfs/ rootfs.img}\\
    \code{mkdir /tmp/tst}\\
    \code{mount -t ext2 -o loop rootfs.img /tmp/tst}
  \item In the \code{/tmp/tst} directory, one can access and modify
    the contents of the \code{rootfs.img} file.
  \item This is possible thanks to \code{loop}, which is a kernel
    driver that emulates a block device with the contents of a file.
  \item Do not forget to run \code{umount} before using the filesystem
    image !
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Squashfs}
  Squashfs: \url{http://squashfs.sourceforge.net}
  \begin{itemize}
  \item Read-only, compressed filesystem for block devices. Fine for
    parts of a filesystem which can be read-only (kernel, binaries...)
  \item Great compression rate and read access performance
  \item Used in most live CDs and live USB distributions
  \item Supports LZO compression for better performance on embedded
    systems with slow CPUs (at the expense of a slightly degraded
    compression rate)
  \end{itemize}
  Benchmarks: (roughly 3 times smaller than ext3, and 2-4 times faster)\\
  \url{http://elinux.org/Squash_Fs_Comparisons}
\end{frame}

\begin{frame}
  \frametitle{Squashfs - How to use}
  \begin{itemize}
  \item Need to install the \code{squashfs-tools} package
  \item Creation of the image
    \begin{itemize}
    \item On your workstation, create your filesystem image:\\
      \code{mksquashfs rootfs/ rootfs.sqfs}
    \item Caution: if the image already exists remove it first,\\
      or use the \code{-noappend} option.
    \end{itemize}
  \item Installation of the image
    \begin{itemize}
    \item Let's assume your partition on the target is in
      \code{/dev/sdc1}
    \item Copy the filesystem image on the device\\
      \code{dd if=rootfs.sqfs of=/dev/sdc1}\\
      Be careful when using \code{dd} to not overwrite the incorrect
      partition!
    \end{itemize}
  \item Mount your filesystem:\\
    \code{mount -t squashfs /dev/sdc1 /mnt/root}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{tmpfs}

  Not a block filesystem of course!

  Perfect to store temporary data in RAM: system log files, connection
  data, temporary files...

  \begin{itemize}
  \item \code{tmpfs} configuration: \code{File systems -> Pseudo filesystems}\\
    Lives in the Linux file cache. Doesn't waste RAM: unlike ramdisks, no need
    to copy files to the file cache, grows and shrinks to accommodate stored files.
    Saves RAM: can swap out pages to disk when needed.
  \item How to use: choose a name to distinguish the various tmpfs
    instances you could have. Examples:\\
    \code{mount -t tmpfs varrun /var/run}\\
    \code{mount -t tmpfs udev /dev}
  \end{itemize}
  See \code{Documentation/filesystems/tmpfs.txt} in kernel sources.
\end{frame}

\begin{frame}
  \frametitle{Mixing read-only and read-write filesystems}
  \begin{columns}
    \column{0.7\textwidth}
    Good idea to split your block storage into:
    \begin{itemize}
    \item A compressed read-only partition (\code{Squashfs})\\
      Typically used for the root filesystem (binaries, kernel...).\\
      Compression saves space. Read-only access protects your system
      from mistakes and data corruption.
    \item A read-write partition with a journaled filesystem (like \code{ext3})\\
      Used to store user or configuration data.\\
      Guarantees filesystem integrity after power off or crashes.
    \item Ram storage for temporary files (\code{tmpfs})
    \end{itemize}
    \column{0.3\textwidth}
    \includegraphics[width=\textwidth]{slides/sysdev-block-filesystems/mixing-filesystems.pdf}
  \end{columns}
\end{frame}

\setuplabframe
{Block filesystems}
{
  \begin{itemize}
  \item Creating partitions on your block storage
  \item Booting your system with a mix of filesystems: SquashFS for
    applications, ext3 for configuration and user data, and tmpfs for
    temporary system files.
  \end{itemize}
}
