mounted: counter for each mountpoint, for umount after last release. moving output-methods to OutputHelper.

This commit is contained in:
Denis Knauf 2020-06-20 23:09:28 +02:00
parent 6307b9a273
commit e9831ba31b
2 changed files with 66 additions and 45 deletions

View file

@ -19,7 +19,19 @@ class Pathname
end
end
class Base
module OutputHelper
def d msg, eq
raise ProgrammError, msg, caller[1..-1] unless eq
end
def err *args
STDERR.puts "\e[31;1m#{args.join ' '}\e[0m"
end
def msg first, *args
STDERR.puts "\e[33m#{first}\e[35m #{args.join ' '}\e[0m"
end
def task name, &exe
STDERR.puts "\e[30;1m***\e[0m #{name} \e[30;1m#{'*' * [0,80-name.length].max}\e[0m"
if yield
@ -31,23 +43,15 @@ class Base
STDERR.puts "\n\e[31;1m|||\e[0m #{name} \e[31;1m#{'|' * [0,80-name.length].max}\e[0m\n"
raise
end
end
class Base
include OutputHelper
def self.run *args, &exe
new( *args).instance_eval &exe
end
def d msg, eq
raise ProgrammError, msg, caller[1..-1] unless eq
end
def err *args
STDERR.puts "\e[31;1m#{args.join ' '}\e[0m"
end
def msg first, *args
STDERR.puts "\e[33m#{first}\e[35m #{args.join ' '}\e[0m"
end
def kpartx image, read_only: nil
sh.kpartx "-sa#{read_only ? :r : ''}", image
lpartx image
@ -158,21 +162,21 @@ class Base
#end
#STDERR.puts x.inspect
#exit 1
@mounted, @activated_vgs = [], []
@mounted, @activated_vgs = Hash.new.tap {|h| h.default = 0 }, []
@looped = Hash.new {|h,k| h[k] = [] }
@base = Image.new self, 'base', image: @baseimage
@dest = Image.new self, 'dest', image: @destination
@qemu_bin = dest.root.join 'usr/bin/qemu-arm-static'
STDERR.print <<EOF
Settings:
username: #{@username || '(default)'}
password: #{@password ? '*********' : '(default)'}
baseimage: #{@base.image}
destination: #{@dest.image}
vgname: #{@vgname}
hostname: #{@hostname || '(default)'}
STDERR.print <<-EOF.gsub( /^\s+#/, '')
#Settings:
# username: #{@username || '(default)'}
# password: #{@password ? '*********' : '(default)'}
# baseimage: #{@base.image}
# destination: #{@dest.image}
# vgname: #{@vgname}
# hostname: #{@hostname || '(default)'}
EOF
sh.def_system_commands *%i[echo sed mount umount partx kpartx sync rsync xz gzip bzip2 zip tar bash dpkg apt]
@ -241,12 +245,12 @@ EOF
def cleanup &exe
@cleanup ||= []
@cleanup.shift exe
@cleanup.unshift exe
end
def after &exe
@after ||= []
@after.shift exe
@after.unshift exe
end
def run
@ -304,31 +308,38 @@ EOF
end
end
def mount from, to, *opts, &exe
@mounted.push to
sh.mount *opts, from, to
def mount dev, mp, *opts, &exe
@mounted[mp] += 1
sh.mount *opts, dev, mp
if block_given?
begin
yield
ensure
umount to
umount mp
end
end
end
def umount mp, *opts
if 0 < @mounted[mp]
@mounted[mp] -= 1
if 0 == @mounted[mp]
r = sh.umount *opts, mp
@mounted.delete to
@mounted.delete mp
r
end
end
end
def umount_all ignore_exceptions: nil
until @mounted.empty?
m = @mounted.keys.first
if ignore_exceptions
capsulated_rescue { sh.umount @mounted.pop }
capsulated_rescue { sh.umount m }
else
sh.umount @mounted.pop
sh.umount m
end
@mounted.delete m
end
end

View file

@ -167,22 +167,32 @@ class Raspbian < Base
'pi1' => { initramfs: 'initramfs initrd.img followkernel', },
'pi0' => { initramfs: 'initramfs initrd.img followkernel', },
}
blocks = [nil]
content = Hash.new {|h,block| h[block] = [] }
block = nil
blocks = Set.new
blocks.add nil
f.each_line do |l|
l.chomp!
case l
when /\A\[([^\]]*)\]\z/
block = $1
blocks.push block
when /\Ainitramfs /
l = replace[block].delete :initramfs
l = nil
blocks.add block
when /\A\s*(\w+)\s*=/i, /\A\s*(initramfs)\s+/
b = $1.to_sym
l = replace[block].delete b if replace[block]&.has_key? b
end
content[block].push l
unless l.nil?
content[block].push l if ''==l or l.start_with?( '#') or not content[block].include?( l)
end
replace.each {|block, rpl| content[block] += rpl.values + [''] unless rpl.empty? }
blocks.flat_map {|block| content[block] }
end
replace.each do |block, rpl|
unless rpl.empty?
content[block] += rpl.values + ['']
blocks.add block
end
end
blocks.flat_map {|block| ["[#{block}]"] + content[block] }
end
addmp[:boot].join( 'ssh').write ''