Announcement

Collapse
No announcement yet.

BTRFS subvolume handling through a service menu

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    BTRFS subvolume handling through a service menu

    I like using Dolphin so I added this service menu and tasks to my Dolphin this morning. Feel free to make it your own and if you're a bash scripting guru, please comment on improvements. I'm also open to new features that would be useful.

    For now, I can create snapshots and delete subvolumes. I also included an "Is it a BTRFS Subvolume" test in case you don't always use the "@" to lead your subvolume names and aren't sure which is a folder and which is a subvolume. Regardless, the scripts validate that before acting.

    Here's the contents of the files I wrote this morning. All of them are under ~/.local/share/kservices5/ServiceMenus. The bash scripts are in a folder there call "snapshot_manager-scripts"

    "snapshot-manager" service manager desktop file:
    Code:
    [Desktop Entry]
    Type=Service
    Actions=issubvol;_SEPARATOR_;takesnapshot;deletesubvol
    X-KDE-ServiceTypes=KonqPopupMenu/Plugin,inode/directory
    MimeType=inode/directory;
    X-KDE-StartupNotify=false
    X-KDE-Priority=TopLevel
    X-KDE-Submenu=Manage BTRFS Subvolumes
    
    [Desktop Action issubvol]
    Icon=question
    Name=Is this a BTRFS Subvolume
    Exec=~/.local/share/kservices5/ServiceMenus/snapshot_manager-scripts/is_subvol %U
    
    [Desktop Action takesnapshot]
    Icon=camera-photo
    Name=Take a Subvolume Snapshot
    Exec=~/.local/share/kservices5/ServiceMenus/snapshot_manager-scripts/snapshot_subvol %U
    
    [Desktop Action deletesubvol]
    Icon=user-trash-full
    Name=Permanently Delete a Subvolume
    Exec=~/.local/share/kservices5/ServiceMenus/snapshot_manager-scripts/delete_subvol %U
    "is_subvol" script file:
    Code:
    #!/bin/bash# is_subvol
    source="$1"
    
    
    #just check, notify and exit
    if [[ "$(stat -f --format="%T" "$source")" -ne "btrfs" ]] || [[ "$(stat --format="%i" "$source")" -ne 256 ]]
    then
        kdialog --error "Sorry, This is not a BTRFS Subvolume"
    else
        kdialog --msgbox "This folder is a BTRFS Subvolume."
    fi
    exit 0
    "snapshot_subvol" script file:
    Code:
    #!/bin/bash# snapshot_subvol
    source="$1"
    target="$1"_`date +%y%m%d-%H%M%S`
    
    
    # get the sudo password
    PASSWD="$(kdialog --password "This task requires SUDO access. Enter your password: ")"
    
    
    #allow cancel to exit cleanly 
    if [ -z "$PASSWD" ]; then 
    kdialog --passivepopup "Snapshot cancelled" 6
    exit 0; fi
    
    
    #validate target is actually a btrfs subvolume and exit if not
    if [[ "$(stat -f --format="%T" "$source")" -ne "btrfs" ]] || [[ "$(stat --format="%i" "$source")" -ne 256 ]]
    then
        kdialog --error "Sorry, Not a BTRFS Subvolume"
        exit 0
    fi
    
    
    #take the snapshot, passively notify, and exit
    echo -e $PASSWD | sudo -S btrfs su sn "$source" "$target"
    kdialog --passivepopup "Snapshot of $source made as $target" 6
    exit 0
    "delete_subvol" script file:
    Code:
    #!/bin/bash# delete_subvol
    source="$1"
    
    
    PASSWD="$(kdialog --password "This task requires SUDO access. Enter your password: ")"
    
    
    if [ -z "$PASSWD" ]; then 
    kdialog --passivepopup "Subvolume deletion cancelled" 6
    exit 0; fi
    
    
    #validate target is actually a btrfs subvolume
    if [[ "$(stat -f --format="%T" "$source")" -ne "btrfs" ]] || [[ "$(stat --format="%i" "$source")" -ne 256 ]]
    then
        kdialog --error "Sorry, Not a BTRFS Subvolume"
        exit 0
    fi
    
    
    #last chance to quit, buttons reversed so Cancel is default action
    kdialog --title "THIS ACTION IS NOT REVERSIBLE"\
     --warningcontinuecancel "Are you sure you want to delete all this subvolume?"\
     --cancel-label "PERMANENTLY DELETE"\
     --continue-label "Cancel"
    
    
    #send cancel message to notifications and exit
    if [ "$?" = 0 ]; then 
    kdialog --passivepopup "Subvolume deletion cancelled" 6
    exit 0; fi
    
    
    # finally delete the subvolume and notify passively then exit
    echo -e $PASSWD | sudo -S btrfs su de -c "$source"
    kdialog --passivepopup "Subvolume $source deleted" 8
    exit 0
    This should be obvious but "USE AT YOUR OWN RISK"

    I tested it many times before posting, but you never know how it going to end up on your system.

    Also, the "To Do" list so far:
    1. Make functions work with more than one subvolume at a time.
    2. Add check to determine snapshot vs. parent subvolume.
    3. Yours??



    [#]btrfs[/#] [#]subvolume[/#] [#]servicemenu[/#]
    Last edited by oshunluvr; Aug 14, 2018, 05:47 AM.

    Please Read Me

    #2
    I'm going to add this ,,,,after you fix the "del_subvol" script file: ,,,,right now it's the "is_subvol" script file: re-coped

    VINNY
    i7 4core HT 8MB L3 2.9GHz
    16GB RAM
    Nvidia GTX 860M 4GB RAM 1152 cuda cores

    Comment


      #3
      Thanks again Vinny - I couldn't do it without ya!

      Please Read Me

      Comment


        #4
        Originally posted by oshunluvr View Post
        Thanks again Vinny - I couldn't do it without ya!
        LOL ,,,,sorry,,,,,,,I got to it right after you posted ,,,,sso .

        and I like it ,,,,will try it @hear ,,,,after I finish the yard

        VINNY
        i7 4core HT 8MB L3 2.9GHz
        16GB RAM
        Nvidia GTX 860M 4GB RAM 1152 cuda cores

        Comment


          #5
          I'm going to Lowes now. Be back later.

          Please Read Me

          Comment


            #6
            Here's what the menu looks like:

            Click image for larger version

Name:	Screenshot_20170923_141701.png
Views:	1
Size:	49.0 KB
ID:	643633

            Please Read Me

            Comment


              #7
              My first step would have to be mounting/dev/sda1 to /mnt and /dev/sdb1 to / backup. Then an option to do a time stamped to subvolume of @ and @home to /mnt/snapshots with sync and an option to send those snapshots to /backup followed by a umount.

              I never create snapshots inside / or /home


              Sent from my iPhone using Tapatalk
              "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
              – John F. Kennedy, February 26, 1962.

              Comment


                #8
                dose not seem to be working @hear ,,,,,,,,

                the "is this a subvolume" said yes whether it was , or just a folder in the drive .

                the "take a subvolume snapshot" ,,,gives a dialog that it created the sub snap at /mnt/btrfs/@home-<date string> ,,,,but did not ?

                I get

                Code:
                vinny@vinny-Bonobo-Extreme:/mnt/btrfs$ sudo btrfs sub list -a /mnt/btrfs
                ID 1175 gen 54771 top level 5 path @
                ID 1176 gen 54770 top level 5 path @home
                before and after running the service menu .

                VINNY
                i7 4core HT 8MB L3 2.9GHz
                16GB RAM
                Nvidia GTX 860M 4GB RAM 1152 cuda cores

                Comment


                  #9
                  I was just trying to figure out how to add Lubuntu 14.04 as dual boot along side Kubuntu 14.04 in my same btrfs full drive. I created a subvolume for @Kubuntu1404 and @home1404 for @ and @home. I was looking up how to delete the originals when I saw this. I will add it after the great and all powerful Kubuntu god of btrfs (oshunluvr) fixes it.
                  Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                  http://www.kubuntu.org/getkubuntu

                  Comment


                    #10
                    Originally posted by vinnywright View Post
                    dose not seem to be working @hear ,,,,,,,,

                    the "is this a subvolume" said yes whether it was , or just a folder in the drive .

                    the "take a subvolume snapshot" ,,,gives a dialog that it created the sub snap at /mnt/btrfs/@home-<date string> ,,,,but did not ?

                    I get

                    Code:
                    vinny@vinny-Bonobo-Extreme:/mnt/btrfs$ sudo btrfs sub list -a /mnt/btrfs
                    ID 1175 gen 54771 top level 5 path @
                    ID 1176 gen 54770 top level 5 path @home
                    before and after running the service menu .

                    VINNY
                    The "snapshot_subvol" script does not mount /mnt/btrfs. Is that mounted all the time on your system?
                    I didn't see anywhere in the scripts where the ROOT of the Btrfs pool is mounted to /mnt, so I am not surprised that the script didn't work.
                    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                    – John F. Kennedy, February 26, 1962.

                    Comment


                      #11
                      Originally posted by GreyGeek View Post
                      The "snapshot_subvol" script does not mount /mnt/btrfs. Is that mounted all the time on your system?
                      I didn't see anywhere in the scripts where the ROOT of the Btrfs pool is mounted to /mnt, so I am not surprised that the script didn't work.
                      ...
                      Just to remind users and devs that Ubuntu and its flavors have a long way to go to be as usr friendly as they should be.

                      http://www.kubuntu.org/getkubuntu

                      Comment


                        #12
                        Originally posted by GreyGeek View Post
                        The "snapshot_subvol" script does not mount /mnt/btrfs. Is that mounted all the time on your system?
                        yes ,,its my data drive that holds the parents to all the system links to my ~/folders.

                        this dose work now for the "is_subvol" check ,,,,,,,,,,,,,,,,,,,

                        Code:
                         #!/bin/bash
                        #source="$1"
                        
                        #just check, notify and exit
                        
                        #this dosent work for what we want as it's "stating" for FILESYSYTEM and will return "btrfs" for anything in the filesystem (we are cheking in a btrfs filesystem after all) :)
                        #[[ "$(stat -f --format="%T" "$source")" -ne "btrfs" ]] || 
                        
                        source="$(kdialog --getexistingdirectory .)"
                        
                        if
                        
                        [ "$(stat --format="%i" "$source")" -ne 256 ]
                        
                        then
                           kdialog --error "Sorry, This is not a BTRFS Subvolume"
                        else
                           kdialog --msgbox "This folder is a BTRFS Subvolume."
                        fi
                        exit 0
                        the rest ,,,,,maybe over my head ,,,,,,,,O but that wont stop me from trying

                        VINNY
                        i7 4core HT 8MB L3 2.9GHz
                        16GB RAM
                        Nvidia GTX 860M 4GB RAM 1152 cuda cores

                        Comment


                          #13
                          It works here. I shouldn't have assumed you'd know to mount the parent first - but if you're taking a snapshot you have to mount the parent or else you can't "see" the subvolume to snapshot it in the first place.

                          Make sure all the scripts are executable ( chmod +x script_name ) and either use the same folder name I did or edit the desktop file to reflect the correct location.

                          Let me try it again here on a different location and make sure I didn't miss something...

                          Please Read Me

                          Comment


                            #14
                            Here's a video of it working here. I tried it on multiple locations and it worked every time.

                            https://www.dropbox.com/s/pn1iupbwhx...nager.mp4?dl=0

                            Please Read Me

                            Comment


                              #15
                              I went back and edited the first post to make sure I chad the code there the most current. I noticed Vinny had the previous version in the "is_subvol" script (just a housekeeping change that shortened the content)

                              Vinny, make sure you have a blank line at the end of all the scripts too. Bash can be picky.

                              Please Read Me

                              Comment

                              Working...
                              X