Advertisement
Guest User

Untitled

a guest
Jun 7th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. # クソ
  2. gemの中の `plugins/guests/linux/cap/mount_smb_shared_folder.rb` を最悪改変して対応する
  3.  
  4. ```ruby
  5. require "shellwords"
  6.  
  7. module VagrantPlugins
  8. module GuestLinux
  9. module Cap
  10. class MountSMBSharedFolder
  11. def self.mount_smb_shared_folder(machine, name, guestpath, options)
  12. expanded_guest_path = machine.guest.capability(
  13. :shell_expand_guest_path, guestpath)
  14.  
  15. mount_commands = []
  16. mount_device = "//#{options[:smb_host]}/#{name}"
  17.  
  18. if options[:owner].is_a? Integer
  19. mount_uid = options[:owner]
  20. else
  21. mount_uid = "`id -u #{options[:owner]}`"
  22. end
  23.  
  24. if options[:group].is_a? Integer
  25. mount_gid = options[:group]
  26. mount_gid_old = options[:group]
  27. else
  28. mount_gid = "`getent group #{options[:group]} | cut -d: -f3`"
  29. mount_gid_old = "`id -g #{options[:group]}`"
  30. end
  31.  
  32. # If a domain is provided in the username, separate it
  33. username, domain = [(options[:smb_username] || ''), nil] # Microsoftアカウントのアドレスで認証していると@の後を必ずdomainパラメータに持ってかれて破滅する
  34. smb_password = options[:smb_password]
  35.  
  36. options[:mount_options] ||= []
  37. options[:mount_options] << "sec=ntlm"
  38. options[:mount_options] << "user=#{username},password=#{smb_password}" #"credentials=/etc/smb_creds_#{name}" なんかcredentialでやるとうまくいかないからクソ改変
  39.  
  40. # First mount command uses getent to get the group
  41. mount_options = "-o uid=#{mount_uid},gid=#{mount_gid}"
  42. mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options]
  43. mount_commands << "mount -t cifs #{mount_options} #{mount_device} #{expanded_guest_path}"
  44.  
  45. # Second mount command uses the old style `id -g`
  46. mount_options = "-o uid=#{mount_uid},gid=#{mount_gid_old}"
  47. mount_options += ",#{options[:mount_options].join(",")}" if options[:mount_options]
  48. mount_commands << "mount -t cifs #{mount_options} #{mount_device} #{expanded_guest_path}"
  49.  
  50. # Create the guest path if it doesn't exist
  51. machine.communicate.sudo("mkdir -p #{expanded_guest_path}")
  52.  
  53. # Write the credentials file
  54. machine.communicate.sudo(<<-SCRIPT)
  55. cat <<EOF >/etc/smb_creds_#{name}
  56. username=#{username}
  57. password=#{smb_password}
  58. #{domain ? "domain=#{domain}" : ""}
  59. EOF
  60. chmod 0600 /etc/smb_creds_#{name}
  61. SCRIPT
  62.  
  63. # Attempt to mount the folder. We retry here a few times because
  64. # it can fail early on.
  65. attempts = 0
  66. while true
  67. success = true
  68.  
  69. stderr = ""
  70. mount_commands.each do |command|
  71. no_such_device = false
  72. stderr = ""
  73. status = machine.communicate.sudo(command, error_check: false) do |type, data|
  74. if type == :stderr
  75. no_such_device = true if data =~ /No such device/i
  76. stderr += data.to_s
  77. end
  78. end
  79.  
  80. success = status == 0 && !no_such_device
  81. break if success
  82. end
  83.  
  84. break if success
  85.  
  86. attempts += 1
  87. if attempts > 10
  88. command = mount_commands.join("\n")
  89. command.gsub!(smb_password, "PASSWORDHIDDEN")
  90.  
  91. raise Vagrant::Errors::LinuxMountFailed,
  92. command: command,
  93. output: stderr
  94. end
  95.  
  96. sleep 2
  97. end
  98.  
  99. # Emit an upstart event if we can
  100. machine.communicate.sudo <<-SCRIPT
  101. if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then
  102. /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}'
  103. fi
  104. SCRIPT
  105. end
  106. end
  107. end
  108. end
  109. end
  110.  
  111. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement