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
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 def task name, &exe
STDERR.puts "\e[30;1m***\e[0m #{name} \e[30;1m#{'*' * [0,80-name.length].max}\e[0m" STDERR.puts "\e[30;1m***\e[0m #{name} \e[30;1m#{'*' * [0,80-name.length].max}\e[0m"
if yield 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" STDERR.puts "\n\e[31;1m|||\e[0m #{name} \e[31;1m#{'|' * [0,80-name.length].max}\e[0m\n"
raise raise
end end
end
class Base
include OutputHelper
def self.run *args, &exe def self.run *args, &exe
new( *args).instance_eval &exe new( *args).instance_eval &exe
end 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 def kpartx image, read_only: nil
sh.kpartx "-sa#{read_only ? :r : ''}", image sh.kpartx "-sa#{read_only ? :r : ''}", image
lpartx image lpartx image
@ -103,10 +107,10 @@ class Base
msg :manipulate, file msg :manipulate, file
File.open file.to_s, flags do |file| File.open file.to_s, flags do |file|
lines = [] lines = []
exe.call file, &lines.method(:push) exe.call file, &lines.method( :push)
file.truncate 0 file.truncate 0
file.pos = 0 file.pos = 0
lines.each &file.method(:puts) lines.each &file.method( :puts)
end end
end end
@ -158,22 +162,22 @@ class Base
#end #end
#STDERR.puts x.inspect #STDERR.puts x.inspect
#exit 1 #exit 1
@mounted, @activated_vgs = [], [] @mounted, @activated_vgs = Hash.new.tap {|h| h.default = 0 }, []
@looped = Hash.new {|h,k| h[k] = [] } @looped = Hash.new {|h,k| h[k] = [] }
@base = Image.new self, 'base', image: @baseimage @base = Image.new self, 'base', image: @baseimage
@dest = Image.new self, 'dest', image: @destination @dest = Image.new self, 'dest', image: @destination
@qemu_bin = dest.root.join 'usr/bin/qemu-arm-static' @qemu_bin = dest.root.join 'usr/bin/qemu-arm-static'
STDERR.print <<EOF STDERR.print <<-EOF.gsub( /^\s+#/, '')
Settings: #Settings:
username: #{@username || '(default)'} # username: #{@username || '(default)'}
password: #{@password ? '*********' : '(default)'} # password: #{@password ? '*********' : '(default)'}
baseimage: #{@base.image} # baseimage: #{@base.image}
destination: #{@dest.image} # destination: #{@dest.image}
vgname: #{@vgname} # vgname: #{@vgname}
hostname: #{@hostname || '(default)'} # hostname: #{@hostname || '(default)'}
EOF 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[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] 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 :mkvfat, *%w[mkfs.vfat]
sh.alias_command :rsync_all, *%w[rsync --archive --hard-links --acls --xattrs --sparse] sh.alias_command :rsync_all, *%w[rsync --archive --hard-links --acls --xattrs --sparse]
sh.alias_command :mount_ro, *%w[mount -oro] 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_uuid, *%w[blkid -ovalue -sUUID], return: :line
sh.alias_command :fs_type, *%w[blkid -o value -s TYPE], return: :line sh.alias_command :fs_type, *%w[blkid -ovalue -sTYPE], return: :line
end end
def getopts opts def getopts opts
@ -241,12 +245,12 @@ EOF
def cleanup &exe def cleanup &exe
@cleanup ||= [] @cleanup ||= []
@cleanup.shift exe @cleanup.unshift exe
end end
def after &exe def after &exe
@after ||= [] @after ||= []
@after.shift exe @after.unshift exe
end end
def run def run
@ -304,31 +308,38 @@ EOF
end end
end end
def mount from, to, *opts, &exe def mount dev, mp, *opts, &exe
@mounted.push to @mounted[mp] += 1
sh.mount *opts, from, to sh.mount *opts, dev, mp
if block_given? if block_given?
begin begin
yield yield
ensure ensure
umount to umount mp
end end
end end
end end
def umount mp, *opts def umount mp, *opts
r = sh.umount *opts, mp if 0 < @mounted[mp]
@mounted.delete to @mounted[mp] -= 1
r if 0 == @mounted[mp]
r = sh.umount *opts, mp
@mounted.delete mp
r
end
end
end end
def umount_all ignore_exceptions: nil def umount_all ignore_exceptions: nil
until @mounted.empty? until @mounted.empty?
m = @mounted.keys.first
if ignore_exceptions if ignore_exceptions
capsulated_rescue { sh.umount @mounted.pop } capsulated_rescue { sh.umount m }
else else
sh.umount @mounted.pop sh.umount m
end end
@mounted.delete m
end end
end end

View File

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