From fb1d3572b2c3321a50138c33c748a0fad3ad8b33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ladislav=20Slez=C3=A1k?= Date: Wed, 17 Sep 2014 17:18:25 +0200 Subject: [PATCH] refacorting + use the new Btrfs code in running system --- src/modules/SpaceCalculation.rb | 165 ++++++++++++++------------------ 1 file changed, 71 insertions(+), 94 deletions(-) diff --git a/src/modules/SpaceCalculation.rb b/src/modules/SpaceCalculation.rb index fd2052cfe..7e82f2c60 100644 --- a/src/modules/SpaceCalculation.rb +++ b/src/modules/SpaceCalculation.rb @@ -348,23 +348,23 @@ def JfsJournalSize(part) def EstimateTargetUsage(parts) parts = deep_copy(parts) Builtins.y2milestone("EstimateTargetUsage(%1)", parts) - mb = 1 << 10 # sizes are in kB, 1MB is 1024 kB + mb = 1 << 10 # sizes are in kiB, 1MiB is 1024 kiB # invalid or empty input - if parts == nil || Builtins.size(parts) == 0 + if parts == nil || parts.empty? Builtins.y2error("Invalid input: %1", parts) return [] end # the numbers are from openSUSE-11.4 default KDE installation used_mapping = { - "/var/lib/rpm" => Ops.multiply(42, mb), # RPM database - "/var/log" => Ops.multiply(14, mb), # system logs (YaST logs have ~12MB) - "/var/adm/backup" => Ops.multiply(10, mb), # backups - "/var/cache/zypp" => Ops.multiply(38, mb), # zypp metadata cache after refresh (with OSS + update repos) - "/etc" => Ops.multiply(2, mb), # various /etc config files not belonging to any package - "/usr/share" => Ops.multiply(1, mb), # some files created by postinstall scripts - "/boot/initrd" => Ops.multiply(11, mb) + "/var/lib/rpm" => 42 * mb, # RPM database + "/var/log" => 14 * mb, # system logs (YaST logs have ~12MB) + "/var/adm/backup" => 10 * mb, # backups + "/var/cache/zypp" => 38 * mb, # zypp metadata cache after refresh (with OSS + update repos) + "/etc" => 2 * mb, # various /etc config files not belonging to any package + "/usr/share" => 1 * mb, # some files created by postinstall scripts + "/boot/initrd" => 11 * mb } # depends on HW but better than nothing Builtins.y2milestone("Adding target size mapping: %1", used_mapping) @@ -511,36 +511,48 @@ def get_partition_info if !Stage.initial # read /proc/mounts as a list of maps # $["file":"/boot", "freq":0, "mntops":"rw", "passno":0, "spec":"/dev/sda1", "vfstype":"ext2"] - mounts = Convert.convert( - SCR.Read(path(".proc.mounts")), - :from => "any", - :to => "list >" - ) - Builtins.y2milestone("mounts %1", mounts) + mounts = SCR.Read(path(".proc.mounts")) + log.info "mounts #{mounts}" partitions = [] - Builtins.foreach(mounts) do |mpoint| - name = Ops.get_string(mpoint, "file", "") - if Builtins.substring(name, 0, 1) == "/" && - Builtins.substring(name, 0, 5) != "/dev/" && # filter out /dev/pts etc. - Ops.get_string(mpoint, "vfstype", "") != "rootfs" # filter out duplicate "/" entry + mounts.each do |mpoint| + name = mpoint["file"] + filesystem = mpoint["vfstype"] + + if name.start_with?("/") && + # filter out /dev/pts etc. + !name.start_with?("/dev/") && + # filter out duplicate "/" entry + filesystem != "rootfs" + capacity = Pkg.TargetCapacity(name) + if capacity != 0 # dont look at pseudo-devices (proc, shmfs, ...) used = Pkg.TargetUsed(name) - partitions = Builtins.add( - partitions, - { - "name" => name, - "free" => Ops.subtract(capacity, used), - "used" => used - } - ) + growonly = false + + if filesystem == "btrfs" + log.info "Btrfs file system detected at #{name}" + growonly = btrfs_snapshots?(name) + log.info "Snapshots detected: #{growonly}" + new_used = btrfs_used_size(name) + log.info "Updated the used size by 'btrfs' utility from #{used} to #{new_used} (diff: #{new_used - used})" + used = new_used + end + + partitions << { + "name" => name, + "free" => capacity - used, + "used" => used, + "filesystem" => filesystem, + "growonly" => growonly + } end end end Pkg.TargetInitDU(partitions) Builtins.y2milestone("get_partition_info: %1", partitions) - return deep_copy(partitions) + return partitions end # !Stage::initial () # remove the previous failures @@ -554,9 +566,7 @@ def get_partition_info :to => "map " ) - if targets == nil - Builtins.y2error("Target map is nil, Storage:: is probably missing") - end + log.error "Target map is nil, Storage:: is probably missing" unless targets if Mode.test targets = Convert.convert( @@ -585,7 +595,7 @@ def get_partition_info free_size = 0 if Ops.get(part, "mount") != nil && - Builtins.substring(Ops.get_string(part, "mount", ""), 0, 1) == "/" + part["mount"].start_with("/") if Ops.get(part, "create") == true || Ops.get(part, "delete") == false || Ops.get(part, "create") == nil && @@ -596,15 +606,12 @@ def get_partition_info ) # get free_size on partition in kBytes - free_size = Ops.multiply( - Ops.get_integer(part, "size_k", 0), - 1024 - ) - free_size = Ops.subtract(free_size, min_spare) + free_size = part["size_k"] * 1024 + free_size -= min_spare # free_size smaller than min_spare, fix negative value - if Ops.less_than(free_size, 0) - Builtins.y2milestone("Fixing free size: %1 to 0", free_size) + if free_size < 0 + log.info "Fixing free size: #{free_size} to 0" free_size = 0 end @@ -617,7 +624,7 @@ def get_partition_info # information for devices (even caching the information). # This part should be refactored to rely on libstorage. - tmpdir = Convert.to_string(SCR.Read(path(".target.tmpdir"))) + tmpdir = SCR.Read(path(".target.tmpdir")) tmpdir = Ops.add(tmpdir, "/diskspace_mount") SCR.Execute( path(".target.bash"), @@ -650,33 +657,19 @@ def get_partition_info tmpdir ) - Builtins.y2milestone( - "Executing mount command: %1", - mount_command - ) + log.info "Executing mount command: #{mount_command}" - result = Convert.to_integer( - SCR.Execute(path(".target.bash"), mount_command) - ) - Builtins.y2milestone("Mount result: %1", result) + result = SCR.Execute(path(".target.bash"), mount_command) + log.info "Mount result: #{result}" if result == 0 - partition = Convert.convert( - SCR.Read(path(".run.df")), - :from => "any", - :to => "list >" - ) + partition = SCR.Read(path(".run.df")) + Builtins.foreach(partition) do |p| - if Ops.get_string(p, "name", "") == tmpdir - Builtins.y2milestone("P: %1", p) - free_size = Ops.multiply( - Builtins.tointeger(Ops.get_string(p, "free", "0")), - 1024 - ) - used = Ops.multiply( - Builtins.tointeger(Ops.get_string(p, "used", "0")), - 1024 - ) + if p["name"] == tmpdir + log.info "Partition: #{p}" + free_size = p["free"].to_i * 1024 + used = p["used"].to_i * 1024 end end SCR.Execute( @@ -684,10 +677,7 @@ def get_partition_info Builtins.sformat("/bin/umount %1", tmpdir) ) else - Builtins.y2error( - "Mount failed, ignoring partition %1", - device - ) + log.error "Mount failed, ignoring partition #{device}" @failed_mounts = Builtins.add(@failed_mounts, part) next @@ -697,12 +687,8 @@ def get_partition_info # compute fs overhead used = EstimateFsOverhead(part) - if Ops.greater_than(used, 0) - Builtins.y2milestone( - "Partition %1: assuming fs overhead: %2kB", - Ops.get_string(part, "device", ""), - Ops.divide(used, 1024) - ) + if used > 0 + log.info "Partition #{part["device"]}: assuming fs overhead: #{used / 1024}kiB" end # journal size @@ -712,9 +698,7 @@ def get_partition_info js = ExtJournalSize(part) reserved = ReservedSpace(part) - if Ops.greater_than(reserved, 0) - used = Ops.add(used, reserved) - end + used += reserved if reserved > 0 elsif used_fs == :xfs js = XfsJournalSize(part) elsif used_fs == :reiser @@ -722,34 +706,27 @@ def get_partition_info elsif used_fs == :jfs js = JfsJournalSize(part) else - Builtins.y2warning( - "Unknown journal size for filesystem: %1", - used_fs - ) + log.warn "Unknown journal size for filesystem: #{used_fs}" end - if Ops.greater_than(js, 0) - Builtins.y2milestone( - "Partition %1: assuming journal size: %2kB", - Ops.get_string(part, "device", ""), - Ops.divide(js, 1024) - ) - used = Ops.add(used, js) + if js > 0 + log.info "Partition #{part["device"]}: assuming journal size: #{js / 1024}kiB", + used += js end # decrease free size - free_size = Ops.subtract(free_size, used) + free_size -= used # check for underflow - if Ops.less_than(free_size, 0) - Builtins.y2milestone("Fixing free size: %1 to 0", free_size) + if free_size < 0 + log.info "Fixing free size: #{free_size} to 0" free_size = 0 end end # convert into kB for TargetInitDU - free_size = Ops.divide(free_size, 1024) - used = Ops.divide(used, 1024) + free_size = free_size / 1024 + used = used / 1024 Builtins.y2milestone( "available partition: mount: %1, free: %2 KB, used: %3 KB", @@ -1095,7 +1072,7 @@ def filesystem(directory) match[1] end - def on_btrfs_with_snapshots?(directory) + def btrfs_snapshots?(directory) return false unless filesystem(directory) == "btrfs" # list available snapshot subvolumes