Browse Source

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

master
Denis Knauf 3 years ago
parent
commit
6307b9a273
  1. 57
      lib/to_lvm_xfs/base.rb
  2. 34
      lib/to_lvm_xfs/raspbian.rb

57
lib/to_lvm_xfs/base.rb

@ -113,9 +113,11 @@ class Base
def capsulated_rescue
yield
rescue Object => e
STDERR.puts "\e[31;1m#{e} (#{e.class}):"
e.backtrace.each {|s| STDERR.puts " #{s}" }
STDERR.print "\e[0m"
STDERR.puts "\e[0;1mTraceback\e[0m (most recent call last):"
e.backtrace[1..-1].
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
def check_programm cmd
@ -175,19 +177,19 @@ 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]
sh.alias_command :losetup_list, *%w[losetup --list --json], return: :json, could_be_empty: true
sh.alias_command :pvs, *%w[pvs --noheadings]
sh.alias_command :blkid, 'blkid', return: :line
sh.alias_command :lsblk, *%w[lsblk --noheadings --paths --list], return: :lines
sh.alias_command :parted, *%w[parted --machine --script]
sh.alias_command :mkxfs, *%w[mkfs.xfs -f]
sh.alias_command :mkext2fs, *%w[mkfs.ext2]
sh.alias_command :mkext4fs, *%w[mkfs.ext4]
sh.alias_command :mkvfat, *%w[mkfs.vfat]
sh.alias_command :rsync_all, *%w[rsync --archive --hard-links --acls --xattrs]
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 :losetup_list, *%w[losetup --list --json], return: :json, could_be_empty: true
sh.alias_command :pvs, *%w[pvs --noheadings]
sh.alias_command :blkid, 'blkid', return: :line
sh.alias_command :lsblk, *%w[lsblk --noheadings --paths --list], return: :lines
sh.alias_command :parted, *%w[parted --machine --script]
sh.alias_command :mkxfs, *%w[mkfs.xfs -f]
sh.alias_command :mkext2fs, *%w[mkfs.ext2]
sh.alias_command :mkext4fs, *%w[mkfs.ext4]
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
end
def getopts opts
@ -237,13 +239,29 @@ EOF
end
end
def cleanup &exe
@cleanup ||= []
@cleanup.shift exe
end
def after &exe
@after ||= []
@after.shift exe
end
def run
@after ||= []
@cleanup ||= []
nok = false
build
if @qemu_bin.exist?
msg :remove, "/usr/bin/qemu-arm-static"
@qemu_bin.unlink
task "After run" do
@after.each {|exe| exe.call }
if @qemu_bin.exist?
#msg :remove, "/usr/bin/qemu-arm-static"
@qemu_bin.unlink
end
end
rescue ProgrammError
@ -252,6 +270,7 @@ EOF
raise
ensure
STDERR.puts "\e[1;36m#{"<"*80}\e[0m"
@cleanup.each {|exe| exe.call }
umount_all ignore_exceptions: true
umount dest.root, -:R rescue Object
umount base.root, -:R rescue Object

34
lib/to_lvm_xfs/raspbian.rb

@ -116,25 +116,22 @@ class Raspbian < Base
@volumes.each {|_name, vol| sh.lvcreate "-n#{vol.name}", "-L#{vol.size}", vgname }
sh.vgchange -:ae, vgname
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
addmp = {run_udev: dest.root+'run/udev'}
task "Mount all filesystems" do
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.mkdir
d.mkdir unless d.exist?
end
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|
path = vol.builder_mp
next if dest.root == path || addmp.values.include?( path)
path.mkdir
path.mkdir unless path.exist?
mount @vgpath+vol.name, path
end
end
@ -143,6 +140,16 @@ class Raspbian < Base
sh.rsync_all "#{base.root}/", dest.root
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
install_authorized_keys
rename_user
@ -155,7 +162,7 @@ class Raspbian < Base
addmp[:boot].join( 'config.txt').replace_i do |f|
replace = {
'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', },
'pi1' => { 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'
task "Prepare to chroot to raspbian" do
@qemu_bin.copy @qemu_bin_src, preserve: true
after { @qemu_bin.unlink }
preload.rename preload_x
if preload.exist?
preload.rename preload_x
after { preload_x.rename preload }
end
end
task "update, upgrade and install" do
@ -254,10 +265,5 @@ class Raspbian < Base
# generates implicite initramfs
ish.system *%w[dpkg-reconfigure raspberrypi-kernel]
end
task "Reverse hacking for chrooting" do
preload_x.rename preload
@qemu_bin.unlink
end
end
end

Loading…
Cancel
Save