Skip to content

Commit

Permalink
refacorting + use the new Btrfs code in running system
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Sep 17, 2014
1 parent b7f8e37 commit fb1d357
Showing 1 changed file with 71 additions and 94 deletions.
165 changes: 71 additions & 94 deletions src/modules/SpaceCalculation.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <map <string, any>>"
)
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
Expand All @@ -554,9 +566,7 @@ def get_partition_info
:to => "map <string, 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(
Expand Down Expand Up @@ -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 &&
Expand All @@ -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

Expand All @@ -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"),
Expand Down Expand Up @@ -650,44 +657,27 @@ 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 <map <string, string>>"
)
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(
path(".target.bash"),
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
Expand All @@ -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
Expand All @@ -712,44 +698,35 @@ 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
js = ReiserJournalSize(part)
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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fb1d357

Please sign in to comment.