James Solum
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # LVM Learnings This documents my learnings on using LVM. The notes stem primarily from a LinkedIn learning course that can be found [here](https://www.linkedin.com/learning/linux-foundation-cert-prep-storage-management-ubuntu/understanding-lvm?trk=learning-serp_learning_search-card&upsellOrderOrigin=homepage-learning_learning-search-bar_search-submit). ## Basics ### Key terms * physical volumes * logical volumes * volume groups * extent ### Command Paradigm The commands for each context are named: 1. `pv*` 2. `lv*` 3. `vg*` corresponding to physical volumes (pv), logical volumes (lv), and virtual groups (vg) respectively. ### Notes LVM stands for `Logical Volume Management` and is an abstraction layer over physical volumes that allows for data to be stored in virtual partitions that can span multiple physical disks. A virtual disk can host multiple physical volumes. Physical volumes are grouped in a **volume group** or **VG**. Physical and logical volumes are divided into units called **extents**. They are chunks of blocks that map between the logical and physical volumes. ### What LVM allows you to do 1. You can dynamically shrink or grow logical volumes across physical volumes. 2. Create snapshots of data ## Steps for adding Disks to a Machine LVM can create a volume on a whole disk, but it is recommended that you first create a partition on the disk then add a logical volume on top of that. ### Setting up the disk Add a standard [GPT](https://en.wikipedia.org/wiki/GUID_Partition_Table) partition to the physical disk using`fdisk`. 1. `fdisk /dev/<disk>`. 2. In the `fdisk` shell use the `g` command to create a new GPT partition. 3. Run `n` to add the partition. Feel free to use the defaults on the partition. 4. Finally commit the partition with `w` to write the change. For information on the `/dev` directory see [here](https://www.linuxnix.com/linux-directory-structure-explained-dev-folder/). ### Creating a physical volume To create the physical volume run: 1. `pvcreate /dev/<disk partition>` a. This will create the physical volume. It may prompt you to replace an existing partition type. b. Since you created the partition you will have a reference to the disk itself e.g. `/dev/sda`, and a reference to the partition e.g. `/dev/sda1`. When creating a physical volume reference the disk partition and not the disk itself. The `extent` size is 4mb by default. To see the new physical volume you can use: 1. `pvdisplay` 2. `pvs`: for more of a compact view. ### Creating a volume group To add a physical volume to a new volume group run: `vcreate <group_name> <physical volume1> <physical volume2> ...` ### Creating a Logical Volume Logical volumes are expressed in terms of `extents`. LVM manages the mapping from logical extends to physical extents. Options for creating a logical volume: 1. `lvcreate -L 100G`: used to create a logical volume of an explicit size in GT, TB, etc. 2. `lvcreate -l 100%VG`: use 100% of space in the VG 3. `lvcreate -l 100%FREE`: use 100% of the remaining space of the VG 4. `lvcreate -l <n>`: use n extents. The number of extents of a physical volume can be found with the `pvdisplay` command and looking at the `Total PE` (total physical extents). Example command: `lvcreate -l 100%FREE -n mydata mygroup` Where `-n` represents the name of the logical volume. And the `mygroup` is referencing the volume group the logical volume should be a part of. The logical volumes are available in the `/dev/mapper/` folder. Thee are also availabe in `/dev/<group>/<lv name>`. Example: ``` $ ls /dev/mapper/ mygroup-mydata $ ls /dev/mygroup/ mydata ``` ### Simple Mounting for Data Now that we have a logical volume we need to build a file system on the volume. To do so run ``` mkfs.ext4 /dev/<group>/<volume> ``` For more information on `ext*` file system, go [here](https://www.tecmint.com/what-is-ext2-ext3-ext4-and-how-to-create-and-convert-linux-file-systems/). Now we need to mount the volume. ``` $ mkdir /mnt/<some name>` $ mount /dev/<group>/<volume> /mnt/<some name> ``` Note the `/mnt` directory is for temporary mount points. For more information see [here](http://www.linfo.org/mnt.html). Now it's mounted and we can create files on the volume! Test it out: ``` $ touch file1 /mnt/<some name> $ ls /mnt/<some_name> file1 ``` To permanently mount the volume you will need to create a new directory outside of `/mnt` and setup your `/etc/fstab` to tell your system to mount the disk to that directory on boot. For more on fstab see [here](https://www.howtogeek.com/howto/38125/htg-explains-what-is-the-linux-fstab-and-how-does-it-work/). ## Extending Basic gist of what we are going to do: Given a disk and a physical volume that has no logical volumes attached to it yet, we are going to extend an existing volume group and logical volume to use the new physical volume. ### Extending a Volume Group `vgextend <group> <physical volume>` By looking at your volume group with `vgdisplay` you can see the number of current physical volumes found under `Cur PV` to have increased. ### Extending a Logical Volume Command Options: * `lvextend -l +<number of extents> <LV to extend>` * `lvextend -l +100%FREE <LV to extend>` * `lvextend -L +2G <LV to extend>` The lowerecase `-l` is for specifying extents. The uppercase `-L` is for specifying general size. Note: the `+` is important. Without it you are actually specifing the exact size of the logical volume you want, not to add the specified size to the existing LV. To get the number of extents that are available on your system, use `vgdisplay` and look at `Free PE`. #### Extending the File System This isn't LVM related but it's important for extending your file system To extend the file system use: `resize2fs <lv>` Confirm by using `df -h` to see size of your filesystem before and after resizing. Note: if the logical volume is not showing up with the `df` command, make sure the volume is mounted. ## Migrate and Replace a Physical Volume Say we want to move our data from one physical volume to another. To do this we utilize the `pvmove` command. Two options: * `pvmove <old pv> <new pv>` * specifiying where to put the data explicitly * `pvmove <old pv>` * lvm will figure out where to put it for you `pvmove` is very slow and if it's interrupted can cause data loss. Be careful. Once you have migrated the data off a disk you can remove the old physical volume from the volume group with `vgreduce <group> <old volume>`. If you are repurposing the disk you can use `pvremove <physical volume>`. ## Filesystem Security and Backups ### ACL's * `ACL`: access control list An ACL is an extension of the existing owner group access system, that allows for more granular permissions. To give a specific user access to a file: ``` setfacl -m u:<username>:<permissions> <file> ``` Where permissions can be `rw` for read write. To see the ACL's on a file you can run: `getfacl <file>` Also you can see that a file has an ACL applied to it when you run `ls -l` and see the `+` character appended to the permissions on the left most column. To remove permissions ``` setfacl -x <user> <file> ``` ### Quotas Limiting space that an individual user can use. Quota's are disabled by default. So you will need to install the Quota package and set them up in your fstab file. Example of adding quota to a volume: ``` $ unmount /mnt/<volume> $ mount -o quota /dev/<volume> /mnt/<volume> $ quotacheck -c /mnt/<volume> $ edquota <user> ``` `quotacheck` will create the user quota file at the root of the volume. `edquota` will edit the quotas for a user. If you use the `-G` option you can specify a group to apply the quota to. Soft limit: system will give the user a warning but will continue to let them create data Hard limit: the user can no longer user more data. You can set these limits by editing the file with `edquota`. You can set the number of blocks or use gb like `1GB`. To check if the quota is on: ``` quotaon -pa ``` To enable the quota: ``` quotaon /mnt/<volume> ``` To see the quota you have as a user: `quota`. To see with human readable `quota -s`. You can test this by running `fallocate -l 500mb <filename>`. This will create a file that is 500mb. To see reports on quota from the admin side: `repquota -a -s` where `-a` shows all filesystems with quotas, and `-s` shows data in human readable format. ## Troubleshooting 1. check logs and review error messages 2. `getfacl` helps with showing permissions of a file 3. consider group membership and path permissions. 4. moving a disk between machines can result in strange looking permissions 5. moving a file retains permissions. 6. SELinux or AppArmor can have extra policies. ## Backups There are multiple approaches to building backups. 1. backup locally or remotely? 2. what files do we backup? ## Resources * [Basic How To](https://tldp.org/HOWTO/LVM-HOWTO/) * [Raid and LVM](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/raid_volumes) * [Creating an Encrypted LVM file system](https://www.linux.com/training-tutorials/how-full-encrypt-your-linux-system-lvm-luks/) <- protection for physical attacks on a server

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully