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

master
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
@ -103,10 +107,10 @@ class Base
msg :manipulate, file
File.open file.to_s, flags do |file|
lines = []
exe.call file, &lines.method(:push)
exe.call file, &lines.method( :push)
file.truncate 0
file.pos = 0
lines.each &file.method(:puts)
lines.each &file.method( :puts)
end
end
@ -158,22 +162,22 @@ 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)'}
EOF
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]
sh.def_system_commands *%i[losetup dmsetup lvcreate vgcreate pvcreate vgchange mkswap vgscan]
@ -188,8 +192,8 @@ EOF
sh.alias_command :mkvfat, *%w[mkfs.vfat]
sh.alias_command :rsync_all, *%w[rsync --archive --hard-links --acls --xattrs --sparse]
sh.alias_command :mount_ro, *%w[mount -oro]
sh.alias_command :fs_uuid, *%w[blkid -o value -s UUID], return: :line
sh.alias_command :fs_type, *%w[blkid -o value -s TYPE], return: :line
sh.alias_command :fs_uuid, *%w[blkid -ovalue -sUUID], return: :line
sh.alias_command :fs_type, *%w[blkid -ovalue -sTYPE], return: :line
end
def getopts opts
@ -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
r = sh.umount *opts, mp
@mounted.delete to
r
if 0 < @mounted[mp]
@mounted[mp] -= 1
if 0 == @mounted[mp]
r = sh.umount *opts, mp
@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
unless l.nil?
content[block].push l if ''==l or l.start_with?( '#') or not content[block].include?( l)
end
content[block].push l
end
replace.each {|block, rpl| content[block] += rpl.values + [''] unless rpl.empty? }
blocks.flat_map {|block| content[block] }
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 ''