cvsextras ab393c7
9dc51e2
if confget("RPM::Upgrade-Virtual/b", "false") == "false" then
cvsextras ab393c7
    return
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
knames = { "hugemem", "bigmem", "enterprise", "smp", "BOOT" }
cvsextras ab393c7
cvsextras ab393c7
-- get kernel package name for running kernel (kernel-smp, kernel, etc)
cvsextras ab393c7
function get_kerneltype()
cvsextras ab393c7
	for i, ktype in ipairs(knames) do
cvsextras ab393c7
		if string.find(posix.uname("%r"), ktype) then
9dc51e2
			return ktype
cvsextras ab393c7
		end
cvsextras ab393c7
	end
cvsextras ab393c7
	-- it's either unknown type or normal UP system
9dc51e2
	return ""
9dc51e2
end
9dc51e2
9dc51e2
-- get package name
9dc51e2
function get_kernelname()
9dc51e2
	suffix = get_kerneltype()
9dc51e2
	if suffix == "" then
9dc51e2
		return "kernel"
9dc51e2
	else
9dc51e2
		return "kernel-"..suffix
9dc51e2
	end
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
-- if grubby is available set default kernel to version
cvsextras ab393c7
function set_default(version)
9dc51e2
	if not posix.access('/sbin/grubby', 'x') then
9dc51e2
		return
9dc51e2
	end
9dc51e2
	grubby = io.popen("LANG=C /sbin/grubby --default-kernel")
9dc51e2
	for line in grubby.lines(grubby) do
9dc51e2
		-- don't touch the default if it's not a linux system
9dc51e2
		if string.find(line, "vmlinuz") then
9dc51e2
			ktype = get_kerneltype()
9dc51e2
			vmlinuz = '/boot/vmlinuz-'..version..ktype
9dc51e2
			if posix.access(vmlinuz, 'f') then
9dc51e2
				print(_("Setting "..vmlinuz.." as new default kernel."))
9dc51e2
				os.execute('/sbin/grubby --set-default='..vmlinuz)
9dc51e2
			else
9dc51e2
				aptwarning(_("Unable to set new default kernel "..vmlinuz))
9dc51e2
			end
9dc51e2
		end
cvsextras ab393c7
	end
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
-- find + mark for install any packages where uname -r is part of pkg name
cvsextras ab393c7
-- eg external kernel modules
cvsextras ab393c7
function upgrade_kernel_modules(new_version)
cvsextras ab393c7
    modprefix = confgetlist("Kernel::Module-Prefix")
cvsextras ab393c7
	if not new_version or not modprefix then
cvsextras ab393c7
		return
cvsextras ab393c7
	end
cvsextras ab393c7
9dc51e2
	seenpkgs = {}
cvsextras ab393c7
	pkgs = pkglist()
cvsextras ab393c7
	for i, pkg in ipairs(pkgs) do
cvsextras ab393c7
		-- oh fun.. loop through all installed packages and see there are
cvsextras ab393c7
		-- any which provide module-prefix, try to install version matching
cvsextras ab393c7
		-- our new kernel
cvsextras ab393c7
    	if not pkgisvirtual(pkg) and pkgvercur(pkg) then
cvsextras ab393c7
        	ver = pkgvercur(pkg)
cvsextras ab393c7
        	for j, prov in ipairs(verprovlist(ver)) do
cvsextras ab393c7
				for x, prefix in ipairs(modprefix) do
cvsextras ab393c7
            		if string.sub(prov.name, 1, string.len(prefix)) == prefix then
cvsextras ab393c7
						inst = pkgfind(prov.name.."-"..new_version)
cvsextras ab393c7
						if inst then
cvsextras ab393c7
							markinstall(inst)
9dc51e2
						elseif not seenpkgs[prov.name] == true then
9dc51e2
							print(_("WARNING: "..prov.name.." not available for kernel "..new_version.."!"))
cvsextras ab393c7
                    	end                                         
9dc51e2
						seenpkgs[prov.name] = true
cvsextras ab393c7
					end
cvsextras ab393c7
            	end
cvsextras ab393c7
        	end
cvsextras ab393c7
    	end
cvsextras ab393c7
	end
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
cvsextras ab393c7
-- find all virtualized packages, their versions and if they're installed
cvsextras ab393c7
function find_instonly_pkgs()
cvsextras ab393c7
	pkgs = {}
cvsextras ab393c7
	for i, pkg in ipairs(pkglist()) do
cvsextras ab393c7
		idx = string.find(pkgname(pkg), "#")
cvsextras ab393c7
		ver = pkgvercur(pkg)
cvsextras ab393c7
		if idx and not pkgisvirtual(pkg) then
cvsextras ab393c7
			name = string.sub(pkgname(pkg), 1, idx-1)
cvsextras ab393c7
			if not ver then
cvsextras ab393c7
				ver = pkgvercand(pkg)
cvsextras ab393c7
			end
cvsextras ab393c7
			-- new entry
cvsextras ab393c7
			if not pkgs[name] then
cvsextras ab393c7
				tmp = {}
cvsextras ab393c7
				tmp.pkg = pkg
cvsextras ab393c7
				tmp.ver = ver
cvsextras ab393c7
				tmp.inst = false
cvsextras ab393c7
				pkgs[name] = tmp
cvsextras ab393c7
			end
cvsextras ab393c7
			if pkgvercur(pkg) then
cvsextras ab393c7
				pkgs[name].inst = true
cvsextras ab393c7
			end
cvsextras ab393c7
			-- mark it as latest if newer than what already known
cvsextras ab393c7
			if ver and verstrcmp(verstr(ver), verstr(pkgs[name].ver)) > 0 then
cvsextras ab393c7
				pkgs[name].ver = ver
cvsextras ab393c7
				pkgs[name].pkg = pkg
cvsextras ab393c7
			end
cvsextras ab393c7
		end
cvsextras ab393c7
	end
cvsextras ab393c7
	return pkgs
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
-- mark installed virtualized packages for "upgrade"
cvsextras ab393c7
function mark_upgrade(name, pkg)
9dc51e2
	kname = get_kernelname()
9dc51e2
	if pkg.inst and not pkgvercur(pkg.pkg) then
cvsextras ab393c7
		markinstall(pkg.pkg)
9dc51e2
		if name == kname and not pkgvercur(pkg.pkg) then
cvsextras ab393c7
			confset('Kernel::New-Version', verstr(pkg.ver))
cvsextras ab393c7
		end
cvsextras ab393c7
	end
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
-- find the latest version of given virtual pkg for installation
cvsextras ab393c7
function mark_install(virtualpkg)
9dc51e2
	kname = get_kernelname()
cvsextras ab393c7
	pkgs = find_instonly_pkgs()
cvsextras ab393c7
	for name in pkgs do
cvsextras ab393c7
		if name == virtualpkg then
cvsextras ab393c7
			selected = pkgs[name].pkg
cvsextras ab393c7
		end
cvsextras ab393c7
	end
9dc51e2
	if virtualpkg == kname and not pkgvercur(pkgs[virtualpkg].pkg) then
cvsextras ab393c7
		confset('Kernel::New-Version', verstr(pkgs[virtualpkg].ver))
cvsextras ab393c7
	end
cvsextras ab393c7
end
cvsextras ab393c7
cvsextras ab393c7
-- normal install operation
cvsextras ab393c7
if script_slot == 'Scripts::AptGet::Install::SelectPackage' then
cvsextras ab393c7
	modprefix = confgetlist('Kernel::Module-Prefix')
cvsextras ab393c7
	for i, prefix in ipairs(modprefix) do
cvsextras ab393c7
		if string.find(virtualname, prefix, 1, true) then
cvsextras ab393c7
			moduname = virtualname.."-"..posix.uname("%r")
cvsextras ab393c7
			modpkg = pkgfind(moduname)
cvsextras ab393c7
			if modpkg then
cvsextras ab393c7
				selected = modpkg
cvsextras ab393c7
			else
cvsextras ab393c7
				apterror(_("Couldn't find package "..moduname))
cvsextras ab393c7
				return
cvsextras ab393c7
			end
cvsextras ab393c7
		end
cvsextras ab393c7
	end
cvsextras ab393c7
	mark_install(virtualname)
cvsextras ab393c7
	return
cvsextras ab393c7
elseif script_slot == 'Scripts::AptGet::Install::PreResolve' then
9dc51e2
	kname = get_kernelname()
cvsextras ab393c7
	if not confexists('Kernel::New-Version') then
cvsextras ab393c7
		for i, pkg in ipairs(pkglist()) do
cvsextras ab393c7
			idx = string.find(pkgname(pkg), "#")
cvsextras ab393c7
			if statinstall(pkg) and idx then
cvsextras ab393c7
				name = string.sub(pkgname(pkg), 1, idx-1)
9dc51e2
				if kname == name and not pkgvercur(pkg) then
cvsextras ab393c7
					confset('Kernel::New-Version', verstr(pkgvercand(pkg)))
cvsextras ab393c7
				end
cvsextras ab393c7
			end
cvsextras ab393c7
		end	
cvsextras ab393c7
	end
cvsextras ab393c7
	if confexists('Kernel::New-Version') then
cvsextras ab393c7
		upgrade_kernel_modules(confget('Kernel::New-Version'))
cvsextras ab393c7
	end
9dc51e2
-- if there's no virtual provide without the version in the name
9dc51e2
-- we need to match it here instead of selectpackage
9dc51e2
elseif script_slot == 'Scripts::AptGet::Install::TranslateArg' then
9dc51e2
    modprefix = confgetlist('Kernel::Module-Prefix')
9dc51e2
    for i, prefix in ipairs(modprefix) do
9dc51e2
		if string.find(argument, prefix, 1, true) then
9dc51e2
			translated = argument.."-"..posix.uname("%r")
9dc51e2
		end
9dc51e2
	end
9dc51e2
	return
cvsextras ab393c7
elseif script_slot == 'Scripts::AptGet::Upgrade' or 
cvsextras ab393c7
   script_slot == 'Scripts::AptGet::DistUpgrade' then
cvsextras ab393c7
	pkgs = find_instonly_pkgs()
cvsextras ab393c7
	table.foreach(pkgs, mark_upgrade)
9dc51e2
	if confexists('Kernel::New-Version') then
9dc51e2
		upgrade_kernel_modules(confget('Kernel::New-Version'))
9dc51e2
	end
cvsextras ab393c7
elseif script_slot == 'Scripts::PM::Post' then
9dc51e2
	if confget('Kernel::Set-Default/b', "false") == "true" and 
9dc51e2
       confexists('Kernel::New-Version') then
cvsextras ab393c7
		set_default(confget('Kernel::New-Version'))
cvsextras ab393c7
	end
cvsextras ab393c7
end
cvsextras ab393c7
		
9dc51e2
-- vim:ts=4:sw=4