From c32ae915c43089ff4e1706a2c59266b57cc7a902 Mon Sep 17 00:00:00 2001 From: neodarz Date: Sun, 5 Aug 2018 10:43:20 +0200 Subject: Add script for write on disk --- scripts/.scripts/write_on_disk.rb | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/.scripts/write_on_disk.rb diff --git a/scripts/.scripts/write_on_disk.rb b/scripts/.scripts/write_on_disk.rb new file mode 100644 index 0000000..dc6f280 --- /dev/null +++ b/scripts/.scripts/write_on_disk.rb @@ -0,0 +1,63 @@ + + + #!/usr/bin/env ruby + require 'dbus' + require 'awesome_print' + require 'filesize' + require 'colorize' + + loop = DBus::Main.new + bus = DBus::SystemBus.instance + + udisk2 = bus['org.freedesktop.UDisks2'] + device = nil + + EXPECTED_INTERFACES = %w[org.freedesktop.UDisks2.PartitionTable org.freedesktop.UDisks2.Block] + + manager = udisk2['/org/freedesktop/UDisks2']['org.freedesktop.DBus.ObjectManager'] + manager.on_signal 'InterfacesAdded' do |path| + interfaces = udisk2[path].interfaces + next unless (EXPECTED_INTERFACES - interfaces).empty? + block = udisk2[path]['org.freedesktop.UDisks2.Block'] + + path = path.split('/').last + + drive = block['Drive'] + drive = udisk2[drive]['org.freedesktop.UDisks2.Drive'] + id = drive['Id'] + size = Filesize.new drive['Size'] + + device = { path: path, id: id, size: size } + + loop.quit + end + + iso = ARGV.first + size = File.size iso + puts "#{'Plug a drive'.colorize :red} to begin to write #{iso.colorize :green} [#{Filesize.new(size).pretty.colorize :yellow}]" + + loop << bus + loop.run + + print "Erase #{device[:id].colorize :red} [#{device[:size].pretty.colorize :yellow}]? [y/N] " + answer = STDIN.gets.chomp + + exit -1 unless answer.downcase == 'y' + device = File.join '/dev/', device[:path] + current = 0 + chunk = Filesize.from('4MiB').to_i + + File.open iso, 'rb' do |iso| + File.open device, 'wb' do |device| + while read = iso.read(chunk) do + current += read.size + device.write read + print "\r[%6.2f%%] %d/%d" % [current.to_f / size * 100, current, size] + end + puts + puts 'Finalizing...'.colorize :yellow + end + end + puts 'Done!'.colorize :green + + -- cgit v1.2.1