after/cleanup-blocks for running code after successful run or cleanup.

This commit is contained in:
Denis Knauf 2020-06-10 14:11:17 +02:00
parent 090b011853
commit 6307b9a273
2 changed files with 58 additions and 33 deletions

View file

@ -113,9 +113,11 @@ class Base
def capsulated_rescue def capsulated_rescue
yield yield
rescue Object => e rescue Object => e
STDERR.puts "\e[31;1m#{e} (#{e.class}):" STDERR.puts "\e[0;1mTraceback\e[0m (most recent call last):"
e.backtrace.each {|s| STDERR.puts " #{s}" } e.backtrace[1..-1].
STDERR.print "\e[0m" each_with_index.
reverse_each {|s,i| STDERR.puts "\t#{i}: #{s}" }
STDERR.puts "#{e.backtrace}: \e[31;1m#{e} (#{e.class})\e[0m"
end end
def check_programm cmd def check_programm cmd
@ -184,7 +186,7 @@ EOF
sh.alias_command :mkext2fs, *%w[mkfs.ext2] sh.alias_command :mkext2fs, *%w[mkfs.ext2]
sh.alias_command :mkext4fs, *%w[mkfs.ext4] sh.alias_command :mkext4fs, *%w[mkfs.ext4]
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] 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 -o value -s UUID], return: :line
sh.alias_command :fs_type, *%w[blkid -o value -s TYPE], return: :line sh.alias_command :fs_type, *%w[blkid -o value -s TYPE], return: :line
@ -237,14 +239,30 @@ EOF
end end
end end
def cleanup &exe
@cleanup ||= []
@cleanup.shift exe
end
def after &exe
@after ||= []
@after.shift exe
end
def run def run
@after ||= []
@cleanup ||= []
nok = false nok = false
build build
task "After run" do
@after.each {|exe| exe.call }
if @qemu_bin.exist? if @qemu_bin.exist?
msg :remove, "/usr/bin/qemu-arm-static" #msg :remove, "/usr/bin/qemu-arm-static"
@qemu_bin.unlink @qemu_bin.unlink
end end
end
rescue ProgrammError rescue ProgrammError
nok = true nok = true
@ -252,6 +270,7 @@ EOF
raise raise
ensure ensure
STDERR.puts "\e[1;36m#{"<"*80}\e[0m" STDERR.puts "\e[1;36m#{"<"*80}\e[0m"
@cleanup.each {|exe| exe.call }
umount_all ignore_exceptions: true umount_all ignore_exceptions: true
umount dest.root, -:R rescue Object umount dest.root, -:R rescue Object
umount base.root, -:R rescue Object umount base.root, -:R rescue Object

View file

@ -116,25 +116,22 @@ class Raspbian < Base
@volumes.each {|_name, vol| sh.lvcreate "-n#{vol.name}", "-L#{vol.size}", vgname } @volumes.each {|_name, vol| sh.lvcreate "-n#{vol.name}", "-L#{vol.size}", vgname }
sh.vgchange -:ae, vgname sh.vgchange -:ae, vgname
sh.mkvfat -:nboot, dest_parts[0] sh.mkvfat -:nboot, dest_parts[0]
@volumes.each {|_name, vol| sh.mkxfs "-L#{vol.name}", vol.device } @volumes.each {|_name, vol| sh.mkxfs "-mreflink=1", "-L#{vol.name}", vol.device }
end end
addmp = {run_udev: dest.root+'run/udev'} addmp = {run_udev: dest.root+'run/udev'}
task "Mount all filesystems" do task "Mount all filesystems" do
mount @vgpath+'root', dest.root mount @vgpath+'root', dest.root
(%i[boot dev proc sys]).each do |n| (%i[boot]).each do |n|
d = addmp[n] = dest.root+n.to_s d = addmp[n] = dest.root+n.to_s
d.mkdir d.mkdir unless d.exist?
end end
mount dest_parts[0], addmp[:boot] mount dest_parts[0], addmp[:boot]
mount '/dev', addmp[:dev], --:bind
mount 'proc', addmp[:proc], -:tproc
mount 'sysfs', addmp[:sys], -:tsysfs
@volumes.each do |_name, vol| @volumes.each do |_name, vol|
path = vol.builder_mp path = vol.builder_mp
next if dest.root == path || addmp.values.include?( path) next if dest.root == path || addmp.values.include?( path)
path.mkdir path.mkdir unless path.exist?
mount @vgpath+vol.name, path mount @vgpath+vol.name, path
end end
end end
@ -143,6 +140,16 @@ class Raspbian < Base
sh.rsync_all "#{base.root}/", dest.root sh.rsync_all "#{base.root}/", dest.root
end end
task "Mount all special filesystems" do
(%i[dev proc sys]).each do |n|
d = addmp[n] = dest.root+n.to_s
d.mkdir unless d.exist?
end
mount '/dev', addmp[:dev], --:bind
mount 'proc', addmp[:proc], -:tproc
mount 'sysfs', addmp[:sys], -:tsysfs
end
task "Prepare users" do task "Prepare users" do
install_authorized_keys install_authorized_keys
rename_user rename_user
@ -155,7 +162,7 @@ class Raspbian < Base
addmp[:boot].join( 'config.txt').replace_i do |f| addmp[:boot].join( 'config.txt').replace_i do |f|
replace = { replace = {
'pi4' => { initramfs: 'initramfs initrd8.img followkernel', arm_64bit: 'arm_64bit=1' }, 'pi4' => { initramfs: 'initramfs initrd8.img followkernel', arm_64bit: 'arm_64bit=1' },
'pi3' => { initramfs: 'initramfs initrd7l.img followkernel', }, 'pi3' => { initramfs: 'initramfs initrd8.img followkernel', arm_64bit: 'arm_64bit=1' },
'pi2' => { initramfs: 'initramfs initrd7.img followkernel', }, 'pi2' => { initramfs: 'initramfs initrd7.img followkernel', },
'pi1' => { initramfs: 'initramfs initrd.img followkernel', }, 'pi1' => { initramfs: 'initramfs initrd.img followkernel', },
'pi0' => { initramfs: 'initramfs initrd.img followkernel', }, 'pi0' => { initramfs: 'initramfs initrd.img followkernel', },
@ -206,8 +213,12 @@ class Raspbian < Base
preload, preload_x = dest.root+'etc/ld.so.preload', dest.root+'etc/ld.so.preload.tp' preload, preload_x = dest.root+'etc/ld.so.preload', dest.root+'etc/ld.so.preload.tp'
task "Prepare to chroot to raspbian" do task "Prepare to chroot to raspbian" do
@qemu_bin.copy @qemu_bin_src, preserve: true @qemu_bin.copy @qemu_bin_src, preserve: true
after { @qemu_bin.unlink }
if preload.exist?
preload.rename preload_x preload.rename preload_x
after { preload_x.rename preload }
end
end end
task "update, upgrade and install" do task "update, upgrade and install" do
@ -254,10 +265,5 @@ class Raspbian < Base
# generates implicite initramfs # generates implicite initramfs
ish.system *%w[dpkg-reconfigure raspberrypi-kernel] ish.system *%w[dpkg-reconfigure raspberrypi-kernel]
end end
task "Reverse hacking for chrooting" do
preload_x.rename preload
@qemu_bin.unlink
end
end end
end end