🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

**Week 5** **PARTITION TABLE AND PARTITIONING** Partitioning is the action of dividing a storage device into multiple partitions. The partition table contains information on the partitions, for example, which range of sectors is used by each partition. **Partition Table Types** The MBR, Master...

**Week 5** **PARTITION TABLE AND PARTITIONING** Partitioning is the action of dividing a storage device into multiple partitions. The partition table contains information on the partitions, for example, which range of sectors is used by each partition. **Partition Table Types** The MBR, Master Boot Record (or DOS) partition table support disks with up to 2TB disk space, and allows the following: - Up to 4 **primary** partitions (where an OS could be booted from) - One of the primary partitions can be an **extended** partition - Extended partition is a container for **logical** partitions (which are mainly used for data storage) **Devices and partitions in Linux** Devices in Linux can be found under the **/dev** directory. Disks and partitions follow certain naming conventions. SATA/SCSI storage devices show as: **/dev/sdX** where X is **a** for the first device, **b** the second and so on. For example: **/dev/sdd** is the 4th storage device. Partitions on a storage device are numbered: **/dev/sdXN** where N is the partition number. For example: **/dev/sdb3** is the 3rd partition on the 2nd storage device If the disk is partitioned with MBR and N \> =5, it must be a logical partition. **Partitioning in Linux** The program **fdisk** can create partition tables and partition disks. A usage example is available on [The Linux Documentation Project](http://tldp.org/HOWTO/Partition/fdisk_partitioning.html) **File Systems** Creating a partition using **fdisk **simply divides up the space on a disk. It doesn't by itself allow the operating system to store files there. You first have to create some infrastructure to hold content inside the partition; you have to create a **File System** inside the partition. A **File System** is a way of storing content inside a disk partition. There are many kinds of file systems, each with different characteristics. Linux supports a huge number. A file system is the way the O/S stores and retrieves data inside the partition, e.g. - create, move and delete files and directories (touch cp mkdir rmdir) - create, remove, modify filenames (ln, rm, mv) - open files for reading and writing (vi, cat, head, tail, sort) - Search for files (find) - seek within a file (fgrep or grep) - list content of a directory (ls) - etc. The above basic functions are common to most operating systems, but they are implemented and managed differently from one OS to another and perhaps even from one kind of file system to another. There are common file systems used in Linux, but it also supports file systems from other proprietary operating systems. The opposite is not always possible. **The Filesystem Hierarchy Standard** Linux distributions commonly follow this standard for the directory structure: **Directory** **Description** --------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- / The topmost (nameless) directory /boot The files needed to boot the system: The compressed kernel, the boot loader /dev Devices. Everything in Linux is represented as an item in the filesystem tree. The devices are virtual files, not physically stored on the disk. /etc Mostly configuration files or other informative files. /etc/os-release information about the current distribution System/application configuration files (like the registry in windows) will be found here, and are simple editable text files. /home User home directories /mnt Where admins often often mount things temporarily, manually. /media Where devices like USB or DVD get mounted automatically /run Temporary filesystem, used by system components (systemd and udev) when a new device is connected, removable devices may be mounted here on some systems. /opt Optional software, that is installed outside the package management system (apt on Ubuntu) /proc Virtual file system, can be used to read kernel and process information. E.g. Process IDs, cpuinfo. Files are created as you access them. /root The home directory for the root account. /sys Virtual file system, information on the system and its components, read or write settings to change how the kernel works /tmp Temporary files, for short term storage. /usr Operating system, application and documentation files (Similar to C:Windows and C:Program Files) /usr/bin User commands applications, can be excuted by regular users. /usr/sbin Admin commands applications need to be root to run. /var Files expected to be changing often (variable content). A web-server or database is often located here. **Creating File Systems in Linux** The **mkfs** command is used for creating file systems on partitions. mkfs -t type \[other options\] device\_name - common types: ext3, ext4, vfat - use vfat for USB keys, floppy disks, and Microsoft compatibility - the device\_name is almost always the /dev/ name of an existing partition, not a whole disk mkfs -t ext4 /dev/sda1 \# 1st disk, 1st partition, ext4 file system mkfs -t vfat /dev/sdb1 \# 2nd disk, 1st partition, VFAT file system **Additional details and tips for using mkfs** - A file system must be created inside an existing drive/partition You must have an existing partition before you can create a file system. - You must have a file system created before you can mount it. - The type defaults to the old ext2 type, but for modern Linux hard disk systems you should always specify the type explicitly as either ext3 or ext4. These newer file system types are journalling file systems. Do not use the old default ext2 file system type, especially on large (over 400MB) disks. - The device\_name is the pathname (usually absolute) of the existing device or partition that will be used, usually of the form /dev/sdXN, where X is the letter of the device (disk) being used and N is the partition number on that device. (Recall that partitions are created using the fdisk command.) - The mkfs commands do not check to see what is in the partition already. They will not ask "Are You Sure?" before they re-format a partition and destroy whatever was previously there. Be extremely careful to get the device name correct! A partition destroyed by mkfs is impossible to recover. - The mkfs commands do not care about the System ID (type) of a partition given in the partition table. You can create any type of file system in any type of partition. (For example, you can create an ext4 file system on a partition labelled in the partition table as NTFS or as Swap, but this is a bad idea.) - Creating a partition with fdisk does not automatically create any type of file system in that partition. - Creating a file system with mkfs does not automatically mount or make available that file system for use in Linux. The new file system is not accessible. More on mounting file systems in the next section. - If file -s can't find a file system inside a disk partition, you cannot mount it -- you probably forgot to make one using mkfs. Optionally, you can check a file system for inconsistencies or errors after creating it using fsck. (Almost never done right after mkfs unless you suspect that your disk may have bad blocks on it.) fsck \[options\] device\_name **Choosing a File System Type** Your system install likely created "journalling" file systems on your virtual disk, using the **-t ext4** option to **mkfs.** Journalling file systems are more resistant to corruption due to sudden power loss, allowing the system to come back up more quickly by avoiding a long file system check at boot time. This does NOT give you permission to power off a running Linux system! Always shut down cleanly. The correct command-line for an immediate, safe system shut down is: **The useful file -s command** The Unix/Linux **file** command is very useful for identifying things in the file system, such as directories, programs, images, files, and special files such as disk partitions. The -**s** option is useful for finding out what kind of file system (if any) is inside a disk partition: The \# prompt indicates the command is running by **root**, the administrator in Linux. This can also be accomplished by using **sudo**. ![A screenshot of a computer program Description automatically generated](media/image2.png) If **file -s** doesn't show a valid file system inside a disk partition, then you can't **mount** the file system. **MOUNTING: ACCESSING FILE SYSTEMS** Mounting is the act of making a file system accessible to users, in the directory hierarchy. **Where File Systems are Mounted in Linux** A computer screen with green text Description automatically generated **\ ** Where DOS/Windows use multiple drive letters, Linux has a single-ROOTed file system tree. "Mounting" attaches an existing file system found on a block device (usually a disk partition, e.g. **/dev/sda1 , /dev/sdb5**) to the Linux directory structure, e.g. onto some directory **/boot**. Accessing that directory, and everything under that directory, accesses the file system on that disk partition. Any number of separate file systems (stored in disk partitions) can be attached anywhere in the same Linux directory tree, resulting in one single ROOTed tree to access every file on every disk. You can detach a file system from one place in the tree and attach it somewhere else, but then the pathnames to files inside that file system would change to reflect the new mount location. File systems can be mounted from the Unix/Linux command line using the **mount** command. File systems can be mounted automatically at system boot time by putting their names and mount points into the **/etc/fstab** file. File systems on removable devices (USB keys, DVDs) can also be mounted dynamically at device insertion time using rules in system configuration files ("automounting"). (Most distribution use an existing **/mntmediaetc/fstab** file and it will always be mounted at boot time. To learn about the format of this file:   **man 5 fstab** File systems can be listed in the **/etc/fstab** file to automate the mounting process. Both the mount point (the existing directory on which the file system is to be mounted) and any other options can be stored in the **fstab** file. *Warning:* *If you make a mistake in the /etc/fstab file, you may not be able to boot Linux regularly.* *You can, however, verify the changes you just saved by using:* *\$ findmnt --verify This will verify the content in /etc/fstab* *Always verify changes to /etc/fstab before rebooting!* The file systems can be identified in **fstab** in the traditional way by partition name (e.g. **/dev/sda1**), which is a bit risky since disk names can change depending on what is attached to your machine at boot time. Another way to identify file systems is with a Volume Label or UUID (Unique UID) identifier that is configured into the partition and that doesn't change even if the partition name changes. You can see these Labels and UUIDs using the **blkid** command. You can set Labels and UUIDs into a partition using the **tune2fs** command. If a file system is not listed in the **/etc/fstab** file and you wish to mount it, then all mount information about the file system and all options need to be given on the command line. At minimum, you need the device name (partition) and the existing mount point (directory) where the file system should be mounted: - e.g.   **mount /dev/sda2 /home** - e.g.   **mount -o \'ro\' /dev/sda2 /home\_readonly** If a file system is listed in the **/etc/fstab** file, then indicating either just the mount point (the directory name) or just the device name (e.g. **/dev/sda1**) will indicate to the **mount** command that the rest of the information needed for the mount should be copied from the matching entry in the **/etc/fstab** file: - e.g.   **mount /dev/sda1**       *\# gets the directory name and options from **/etc/fstab*** - e.g.   **mount /home**                 *\# gets the device name and options from **/etc/fstab*** **Example of an /etc/fstab file** s A screen shot of a computer program Description automatically generated Each line in **/etc/fstab** has six fields that describes a file system: 1. **Field 1:** Identifies the **device** (partition) to be mounted - traditionally, this a partition name under **/devdev/sda1** - can be a LABEL or a UUID: Above, both ROOT **etc/fstab** file to be mounted -- you can always mount anything manually from the command line. The **fstab** is just for things that you want mounted when the system boots. Having a file system listed in the **fstab** means you only need to specify the **device** name or the **mount point** to the **mount** command; the other information for the mount will be read from the **/etc/fstab** file: ![A screen shot of a computer Description automatically generated](media/image6.png) **Some common options in fstab used by mount** - **auto** -- will be mounted when **mount** is used with the -a option (and so mounted at boot time) (default) - **noauto** -- will be ignored by the -a option (not mounted at boot time) - **exec** -- permit execution of binaries (default) - **ro** -- mount the file system read-only (read, execute, no writing) - **rw** -- mount the file system read-write (default) - **user** -- let a non-root user mount and unmount this file system - **nouser** -- only root can mount and unmount this file system (default) - **defaults** -- use default options:   **auto, exec, rw, nouser** - Defaults may vary with distribution! Be careful! **Virtual Memory -- Swap Partitions** Another use for disk partitions is to hold memory pages used to implement Unix/Linux Virtual Memory. These are called **swap** partitions or the **swap area**, and they, too need some minimal structure created to be used by the system. Usually, you find only one swap partition in a system, but systems with a lot of memory may have more. **Step 1: Create the partition using fdisk** - - Same as creating any other kind of partition -- use **fdisk** - Set the partition type ID to be **Linux Swap** to document that this is a swap partition. Many systems will automatically connect and use swap partitions if they have the correct type ID. **Step 2: Initialize(format) the swap are using mkswap** - Make a Linux partition into a swap partition - **mkswap \[options\] device\_name** - e.g.   **mkswap /dev/sdb9** - rarely need any options - This destroys whatever else was in the partition! - you don't get asked to confirm -- type it right the first time The **device\_name** is the pathname (usually absolute) of the existing swap partition that will be used. These commands do not care about the System ID (type) of a partition given in the partition table. You can initialize as swap any type of partition. Most Linux systems at boot time will automatically use as swap all partitions that have a type of "Linux swap", if they are initialized as swap. You can add more swap space to a running system using the above two commands. To display the currently active swap partitions, omit the **device\_name** and use only the **-s** or **\--summary** option to the **swapon** command: - Display currently active swap partitions: **swapon -s** **Step 3: Connect the swap area to the system using swapon** - Use **swapon** to activate the swap area you just created with **mkswap** - **swapon device\_name** - e.g.   **swapon /dev/sdb9** - To list the currently active swap areas: - **swapon -s**         *(list all currently active swap areas)* - Use **swapoff** to de-activate (stop using) a swap area: - **swapoff device\_name** - e.g.   **swapoff /dev/sdb9** - de-activates that swap partition and allows the partition to be re-formatted and used for something else - the **-a** option will disconnect *all* swap areas (not usually a good idea) - At boot time, partitions in **/etc/fstab** marked with type **swap** will be automatically connected and used as swap areas - You can manually ask that all **fstab** swap partitions be connected using: - **swapon -a**       *(connect and enable all known swap partitions)* - the **swapoff** command has an identical option to *disconnect* all active swap areas

Tags

computer science operating systems partitioning
Use Quizgecko on...
Browser
Browser