Linux: Lost with CLI and Folder Copy lazarus / 32-Linux ---------------------------------------------------------------------- QEnnay · 2022-01-30 18:36 Mint 20.2, Laz 2.0.12 I need to backup a QEMU Virtual Machine Folder and using the code below it is crashing. Could someone please assist me on how I need to proceed with this? I have done a bunch of searches and testing, but still getting nowhere with it. I have manually tested the aStr+bStr in the Terminal and it does the copy fine after entering the user-password. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     var    aStr : String;      bStr : String;      cStr : String;      fromFolder : String;      toFolder : String;     begin      fromFolder:='/var/lib/libvirt/images';  // local Ryzen PC      toFolder:='/media/username/vmbak/';  // an external drive      aStr:='sudo cp';      bStr:='[ -aRv ,'+fromFolder+',  '+toFolder+' ]';      if (runCommand(aStr,bStr,cStr)) then      begin         ...   Thanks ---------------------------------------------------------------------- MarkMLl · 2022-01-30 19:20 I'm uncomfortable with expecting sudo to work reliably like that. I'd suggest testing your code first using a couple of directories which you can manipulate without needing root access rights. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-01-30 19:25 I concur. And we need more concrete information. ---------------------------------------------------------------------- MarkMLl · 2022-01-30 19:38 Well spotted Jamie. So without checking actual parameters, it shouldn't be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bStr:='[ -aRv ,'+fromFolder+',  '+toFolder+' ]';  but instead Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bStr:= ['-aRv', fromFolder, toFolder];  But I'm still unhappy with that sudo... at the very least cp should be in bStr since it's a parameter (to sudo) rather than part of the executable name. MarkMLl ---------------------------------------------------------------------- dsiders · 2022-01-30 19:42 Quote from: QEnnay on January 30, 2022, 07:36:30 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       if (runCommand(aStr,bStr,cStr)) then  RunCommand does not have a variant that accepts 3 string arguments. It does if you omit the last 2 arguments... disregard. ---------------------------------------------------------------------- dbannon · 2022-01-30 22:15 And why do you need to use su to access a QEMU Virtual Machine directory ? QEMU's files would be in the name of the user, if thats not you, then you should su to that user before running the app. If they are in the name of root, well, thats a very bad thing indeed, I cannot imagine any situation where QEMU should be run as root. Davo ---------------------------------------------------------------------- QEnnay · 2022-01-30 22:57 Quote from: dbannon on January 30, 2022, 11:15:46 pmAnd why do you need to use su to access a QEMU Virtual Machine directory ? Because virt-manager puts them in the fromFolder in my OP. I tried using a Folder in my Home, but virt-manager changes the permissions to itself and I STILL need root. So I just left them where it wanted them. ---------------------------------------------------------------------- QEnnay · 2022-01-30 23:01 Quote from: MarkMLl on January 30, 2022, 08:38:06 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i bStr:= ['-aRv', fromFolder, toFolder];  Thanks, I will give that a try and mess with where "cp" goes too. Is there some way I can have my program request "sudo" status at start up so I can just have "cp" in aStr? This is for my own use so it does not need to be overly-slick. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2022-01-30 23:25 Hey QEnnay, This thread has me quite curious on 2 main fronts: Why use Object Pascal, when this appears to be a one line bash script?How are you injecting the password to sudo when using RunCommand? On point 1: Unless you need to do some other stuff than just copy the files, why use the wrong tool for the job? On point 2: I would really like to know how you solve this. Of course you could start your Object Pascal program from the command line using sudo, but then I come back to point one: Why even mess with Object Pascal when a one line bash script can do the work? Like I stated at the beginning, you're completely baffling me with the use of Object Pascal for what seems like a task for some very simple bash scripting. As per my usual, I may be missing something and I'm sticking my foot in my mouth, but hey, why break with tradition Cheers, Gus ---------------------------------------------------------------------- MarkMLl · 2022-01-31 08:34 Quote from: QEnnay on January 31, 2022, 12:01:55 am Thanks, I will give that a try and mess with where "cp" goes too. Is there some way I can have my program request "sudo" status at start up so I can just have "cp" in aStr? [/quote] The R is redundant, check the manpage. I'm pretty sure there is no easy way to switch to a more privileged user after starting your program. There's various workarounds: making sure that the person running the program is a member of the group that owns the target directory, running the program setuid, doing stuff with POSIX capabilities and so on. However as Gus has pointed out: you need some way of sending sudo your password, since it's normally resistant to having it piped etc. I submitted a patch to Lazarus a few months ago that handles this, but by and large it's not really standard practice. You might do better using something like scp: even though it's intended for use between different systems there's no reason it can't run locally, and that allows you to give the user a pregenerated key rather than relying on a password. MarkMLl ---------------------------------------------------------------------- dbannon · 2022-01-31 09:53 Quote from: MarkMLl on January 31, 2022, 09:34:31 am.... You might do better using something like scp: even though it's intended for use between different systems there's no reason it can't run locally, and that allows you to give the user a pregenerated key rather than relying on a password. Trouble is that scp of ssh used that way really requires sshpass and that brings along its own security questions. Personally, I'd say OK on a single user, at home machine. Not OK on any shared system. I have only used QEMU from the command line, not used the GUIs available for it because they appeared very awkward and, honestly, unnecessary. Starting QEMU from the command line, it was very easy to ensure its files were stored in a sensible place and everything ran as me, as it should. If the GUI needs root access, then do not use it ! Linux is different to Windows, in Windows, it seems normal to say "Oh, that did not run, try running as administrator" and, magically, everything works. In Unix generally, you never run something as root unless you have a clear reason why it needs to and what will happen if you do. Davo ---------------------------------------------------------------------- MarkMLl · 2022-01-31 10:15 Quote from: dbannon on January 31, 2022, 10:53:02 amI have only used QEMU from the command line, not used the GUIs available for it because they appeared very awkward and, honestly, unnecessary. Starting QEMU from the command line, it was very easy to ensure its files were stored in a sensible place and everything ran as me, as it should. If the GUI needs root access, then do not use it ! Add to that that it's possible to start Qemu as root (so it can set up bridging etc.) and then switch to a lesser user for actual operation. However I prefer not to speculate on what OP's actually doing: I get the impression that he generally knows his stuff but has limited experience on Linux hence his specific question. And let's face it: on a shared machine just about every security assumption goes out of the window: the habit of just about every OS with desktop pretensions to incorporate things which "seemed like a good idea at the time" makes them little better than Windows in practice. MarkMLl ---------------------------------------------------------------------- QEnnay · 2022-01-31 14:18 Quote from: MarkMLl on January 31, 2022, 11:15:45 amI get the impression that he generally knows his stuff but has limited experience on Linux hence his specific question. Good assumption. I do use varish with QEMU -- mostly, but installed and launch using virt-manager. I have a backup system I wrote years back and continually tweak, as one does. I only recently began messing with QEMU and decided to add the .qcow2 files to the backup , hence the current Permissions issues. ---------------------------------------------------------------------- MarkMLl · 2022-01-31 14:38 Quote from: QEnnay on January 31, 2022, 03:18:48 pmI only recently began messing with QEMU and decided to add the .qcow2 files to the backup , hence the current Permissions issues. This isn't CIX so I really don't want to answer your question by saying "You don't want to do that" :-) BUT: Code: [Select]$ ls -ld ReactOS.d ReactOS.d/* drwxr-xr-x 2 markMLl markMLl       4096 May  5  2020 ReactOS.d -rwxr-xr-x 1 markMLl markMLl       2768 May  5  2020 ReactOS.d/do2-ReactOS -rw-r--r-- 1 markMLl markMLl  268435456 May  6  2020 ReactOS.d/drive_e.raw -rw-r--r-- 1 markMLl markMLl 3297378304 May  6  2020 ReactOS.d/ReactOS.img where Qemu is started using the -runas option. In more detail, I've got the whole thing wrapped in a script which is run as root using sudo, the script includes Code: [Select]if [ -n "$SUDO_USER" ]; then   RUNAS="-runas $SUDO_USER" fi so that the option is set to the name of the user that invoked sudo and Qemu switches to that user as early as possible. I'm sure you're also aware that there are preferred ways of backing up a .qcow2 (etc.) file, and while I'd need to check that almost certainly allows it to be given prescribed ownership and access rights once the operation has completed. MarkMLl