i3-copy-scripts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. #This script copies all the scripts specified in a file to a vm of your choice.
  3. #This is a qubes-os specific script.
  4. # i3-copy-scripts - Copy scripts to a vm of your choice.
  5. # Copyright (C) 2017 Fabrizio Romano Genovese <egonigredo@gmail.com>
  6. # This copyrighted material is made available to anyone wishing to use,
  7. # modify, copy, or redistribute it subject to the terms and conditions of
  8. # the GNU General Public License v.2, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY expressed or implied, including the implied warranties of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. # You should have received a copy of the
  15. # GNU General Public License along with this program; if not,
  16. # see <http://www.gnu.org/licenses/>
  17. # Put this file in /bin/ or in /usr/bin. Do not forget to give
  18. # sudo chmod +x i3-copy-scripts
  19. # to make it executable.
  20. # The subroutine that copies files
  21. version=1.1
  22. scripts_file=$HOME/.config/i3/i3-copy-scripts
  23. configs_file=$HOME/.config/i3/i3-copy-configs
  24. # This is the subroutine that performs the copying.
  25. # It checks that the config file exists and is not empty before
  26. # proceeding.
  27. copy_scripts()
  28. {
  29. if [ ! -f $scripts_file ]; then
  30. echo "The scrips file does not exist!"
  31. exit 2
  32. elif [ -z `sed -n "1p" $scripts_file` ]; then
  33. echo "The scripts file is empty!"
  34. exit 2
  35. else
  36. # Here we copy all the scripts specified in the file
  37. # until we hit an empty line
  38. line_number=1
  39. while [ ! -z `sed -n "${line_number}p" $scripts_file` ]; do
  40. script=`sed -n "${line_number}p" $scripts_file`
  41. # Evaluate ~ if present
  42. script="${script/#\~/$HOME}"
  43. echo "Copying $script..."
  44. # Here we abort if a script specified in the file does not exist
  45. # or cannot be located
  46. if [ ! -f $script ]; then
  47. echo "The script '$script' does not exist or I cannot find it."
  48. echo "Aborting."
  49. exit 2
  50. fi
  51. # For security reason we don't want to export executable scripts.
  52. # So we make the script not executable, we pass it to the app vm
  53. # and we make it executable again for dom0. The change of permission must be done
  54. # locally, since invoking it with qvm-run within the appvm causes permission
  55. # problems.
  56. sudo chmod -x $script
  57. qvm-copy-to-vm $1 $script
  58. sudo chmod +x $script
  59. let line_number=line_number+1
  60. done
  61. # Here we copy all the configurations specified in the file
  62. # until we hit an empty line
  63. line_number=1
  64. while [ ! -z `sed -n "${line_number}p" $configs_file` ]; do
  65. config=`sed -n "${line_number}p" $configs_file`
  66. # Evaluate ~ if present
  67. config="${config/#\~/$HOME}"
  68. echo "Copying $config..."
  69. # Here we abort if a configuration file specified in the file does not exist
  70. # or cannot be located
  71. if [ ! -f $config ]; then
  72. echo "The configuration file '$config' does not exist or I cannot find it."
  73. echo "Aborting."
  74. exit 2
  75. fi
  76. # Here we copy to appvm
  77. qvm-copy-to-vm $1 $config
  78. let line_number=line_number+1
  79. done
  80. fi
  81. }
  82. # Here we give help messages and the like
  83. if [[ $# -le 1 ]]; then
  84. case $1 in
  85. -v | --version)
  86. echo "$0 - Version $version"
  87. exit 0
  88. ;;
  89. -h | --help)
  90. echo "This script is QubesOS specific."
  91. echo "It copies all the scripts/configuration files specified in a file to a vm of your choice."
  92. echo "All the inputs must have the format"
  93. echo "/path/to/script/script"
  94. echo "There has to be one input per line, whithout empty lines."
  95. echo "The scripts to copy have to be specified in the file ~./config/i3/i3-copy-scripts."
  96. echo "The configuration files to copy have to be specified in the file ~./config/i3/i3-copy-configs."
  97. echo "- v, --version Display current version"
  98. echo "-h, --help Display this message"
  99. echo "vmname Copies the scripts to vmname appvm"
  100. exit 0
  101. ;;
  102. *)
  103. # Gives usage instruction if imput is empty
  104. if [ -z ${1} ]; then
  105. echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}"
  106. exit 2
  107. fi
  108. # Check if the appvm exists
  109. if [ ! -d /var/lib/qubes/appvms/${1} ]; then
  110. echo "The appvm '$1' does not exist."
  111. echo "Usage: $0 {-v, --version|-h, --help|up|toggle|down}"
  112. echo "Aborting."
  113. exit 2
  114. else
  115. # If the appvm exists, it copies the files
  116. copy_scripts $1
  117. fi
  118. esac
  119. fi