Run script to restore "/home/guest" snapshot when user "guest" logs out

I am not sure where to post this, so here it goes. I am installing a few desktops in my shop that have Endeavour installed on them to get some exposure to Linux for some of my clients to try out before they make the leap from Windows. I have a user set up as “guest” and have the home directory as a subvolume and backed up in snapper. The plan is to have guests use the system and have complete standard user control over the system and save files to home, but when they log out I want the home folder restored back to the initial snapshot. If I have guests that are using the computer more frequently I can then create a separate user for them to keep their desktop and files intact. How do I set up the system to restore the backup snapshot when the “guest” user logs out? Is there a better way to do this I have not thought of? Thanks.

LightDM seems to support enabling of guest sessions.
Now, Plasma uses sddm as display manager so that might not be that relevant to you.

I’m sure some forum member could help you out with the script to achieve what you want but I just wanted to mention this in case you want to experiment by switching to LightDM.

https://wiki.archlinux.org/title/LightDM#Enabling_guest_sessions

Here is also an article about the same to give some ideas:

1 Like

I found this:

# From the kdm man page
#  http://manpages.ubuntu.com/manpages/oneiric/man1/kdm.1.html

/etc/kde4/kdm/Xreset - script to run as root after session exits

/etc/kde4/kdm/Xsession - script to run as user after login of user

/etc/kde4/kdm/Xsetup - script to run as root before the login dialog appears

/etc/kde4/kdm/Xstartup -  script to run as root before session starts

Maybe this will work. I just need the command to restore the snapshot, or is there a simple one. Do I have to delete the home directory and copy everything from the /home/guest/.snapshot directory?

The only problem with these files is that if I am correct since it is in the root directory it will run everytime a user logs out. I only need it for the single “guest” user.

No:

  • Unmount the guest subvolume
  • Delete the subvolume
  • Take a r/w snapshot of the clean snapshot and save it to the location of the subvolume you just deleted
  • Mount the guest subvolume

Note, if it is a nested subvol, you don’t need to unmount or mount it.

1 Like

Ok, here is my setup:

 jeremiah  JLaptop-Endeavour  ~  sudo btrfs subvol list /
ID 256 gen 12338 top level 5 path @
ID 257 gen 12338 top level 5 path @home
ID 258 gen 12335 top level 5 path @cache
ID 259 gen 12338 top level 5 path @log
ID 260 gen 25 top level 256 path var/lib/portables
ID 261 gen 26 top level 256 path var/lib/machines
ID 262 gen 12307 top level 256 path .snapshots
ID 263 gen 12306 top level 257 path @home/.snapshots
ID 280 gen 3618 top level 257 path @home/guest
ID 281 gen 9683 top level 280 path @home/guest/.snapshots
ID 285 gen 560 top level 281 path @home/guest/.snapshots/1/snapshot
ID 374 gen 3495 top level 263 path @home/.snapshots/2/snapshot
ID 375 gen 3496 top level 262 path .snapshots/5/snapshot
ID 382 gen 3615 top level 262 path .snapshots/11/snapshot
ID 383 gen 3616 top level 262 path .snapshots/12/snapshot
ID 384 gen 3618 top level 281 path @home/guest/.snapshots/2/snapshot
ID 385 gen 3621 top level 262 path .snapshots/13/snapshot
ID 386 gen 3622 top level 262 path .snapshots/14/snapshot
ID 387 gen 3626 top level 262 path .snapshots/15/snapshot
ID 388 gen 3627 top level 262 path .snapshots/16/snapshot
ID 389 gen 3631 top level 262 path .snapshots/17/snapshot
ID 390 gen 3632 top level 262 path .snapshots/18/snapshot
ID 437 gen 6022 top level 263 path @home/.snapshots/27/snapshot
ID 438 gen 6023 top level 262 path .snapshots/42/snapshot
ID 485 gen 8424 top level 263 path @home/.snapshots/51/snapshot
ID 486 gen 8425 top level 262 path .snapshots/66/snapshot
ID 533 gen 10809 top level 263 path @home/.snapshots/75/snapshot
ID 534 gen 10810 top level 262 path .snapshots/67/snapshot
ID 557 gen 12005 top level 263 path @home/.snapshots/87/snapshot
ID 559 gen 12104 top level 263 path @home/.snapshots/88/snapshot
ID 561 gen 12204 top level 263 path @home/.snapshots/89/snapshot
ID 563 gen 12208 top level 262 path .snapshots/69/snapshot
ID 564 gen 12215 top level 262 path .snapshots/70/snapshot
ID 565 gen 12306 top level 263 path @home/.snapshots/90/snapshot
ID 566 gen 12307 top level 262 path .snapshots/71/snapshot

Not sure why there is two guest snapshots. I only see one in snapper.

Config: guest, subvolume: /home/guest
 # | Type   | Pre # | Date                            | User | Cleanup | Description     | Userdata
---+--------+-------+---------------------------------+------+---------+-----------------+---------
0  | single |       |                                 | root |         | current         |         
2  | single |       | Tue 28 Mar 2023 12:05:40 AM CDT | root |         | Manual Snapshot |         

Not sure if it is nested. I created the user in KDE, logged into the desktop, logged out and back into the admin user, then deleted /home/guest. I then created a subvolume named /home/guest and setup everything for snapper.

I wouldn’t use snapper for @home/guest. It is going to be more work and doesn’t add any real value for your specific use case.

I would just take a manual snapshot and keep restoring that one manual snapshot.

I only see one in both places :slight_smile:

It is nested. I would leave it that way.

I am technically using snapper for @/home/guest, I guess I miss-worded it. I used btrfs-assistant to make a manual snapshot and the config for it.

So, I can create a script for this, the question now is how to execute it?

Oh, I thought it was 2. Still learning.

ID 285 gen 560 top level 281 path @home/guest/.snapshots/1/snapshot
..
..
..
ID 384 gen 3618 top level 281 path @home/guest/.snapshots/2/snapshot

That is two snapshots.

I am not sure why #1 isn’t showing up in snapper.

I wouldn’t do it like that for this specific subvolume. That will make your job a lot harder.

You will need to relocate the .snapshots subvol every time.

So should I just make a copy of the /home/guest folder then and copy it back for the script? I am guessing that would be the same thing. I guess I was confused since I am thinking of instantaneous restores with snapshots.

No, just use a manual snapshot instead of a snapper snapshot.

Ok, I just played around a little bit and just used rsync to copy files from a backup location to the /home/guest folder and it seems to work. The only thing I need to figure out now is how to make the script run on logout for “guest” user. The Xreset file looks like it was only in KDE4.

googled a bit, if it’s a shell user there’s a ./bash_logout file, never tried it though. If it’s a desktop, maybe check with cron every minute or so if the user is logged in?

No, it’s a desktop environment, specifically KDE. I am sure there is a way to do it, I’ll just have to trade out the shovel for a backhoe.

I have 0 knowledge about kde. Maybe there’s a login manager that could run a comand after the desktop session finished.

Maybe make a script and put it in place of the program which starts kde. This might be the easiest, if there’s no option for this in kde.

ksnip_3

1 Like