├── .gitignore ├── debian ├── compat ├── rules ├── control └── changelog ├── files ├── etc │ ├── hostname │ ├── fstab │ ├── mtab │ ├── hosts │ ├── dhclient-exit-hooks.d │ │ └── hostname │ └── inittab ├── sbin │ ├── ducklogin │ ├── duckinstall │ └── duckdb ├── lib │ ├── python-duck │ │ └── duck │ │ │ ├── __init__.py │ │ │ ├── log.py │ │ │ └── db.py │ ├── duck.d │ │ ├── 98-remove-policy-rc.d │ │ ├── 41-update-hostname │ │ ├── 41-add-policy-rc.d │ │ ├── 99-reboot │ │ ├── 40-debootstrap │ │ ├── 02-setup-network │ │ └── 00-splash │ └── libduck.sh └── init ├── lib ├── duck │ ├── version.rb │ ├── enter.rb │ ├── logging.rb │ ├── qemu.rb │ ├── chroot_utils.rb │ ├── module_helper.rb │ ├── spawn_utils.rb │ ├── pack.rb │ └── build.rb └── duck.rb ├── bin └── duck ├── fixes ├── clear-persistent-udev ├── squeeze-fix ├── busybox-links └── kernel-boot-fix ├── README ├── duck.gemspec ├── duck.yaml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | tmp 2 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /files/etc/hostname: -------------------------------------------------------------------------------- 1 | duck 2 | -------------------------------------------------------------------------------- /files/etc/fstab: -------------------------------------------------------------------------------- 1 | # empty fstab 2 | -------------------------------------------------------------------------------- /files/etc/mtab: -------------------------------------------------------------------------------- 1 | # empty mtab 2 | -------------------------------------------------------------------------------- /lib/duck/version.rb: -------------------------------------------------------------------------------- 1 | module Duck 2 | VERSION = "0.2.2" 3 | end 4 | -------------------------------------------------------------------------------- /files/etc/hosts: -------------------------------------------------------------------------------- 1 | 127.0.0.1 localhost duckinstall 2 | ::1 localhost 3 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | %: 4 | dh $@ --buildsystem=ruby --with ruby 5 | -------------------------------------------------------------------------------- /files/sbin/ducklogin: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | read -p "(press 'enter' to login)" 3 | exec /bin/login -f root 4 | -------------------------------------------------------------------------------- /files/lib/python-duck/duck/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = (0, 3, 0) 2 | __version_string__ = ".".join(map(str, __version__)) 3 | -------------------------------------------------------------------------------- /files/lib/python-duck/duck/log.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | 4 | def setup(): 5 | logging.basicConfig(level=logging.INFO) 6 | -------------------------------------------------------------------------------- /files/init: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | echo "Invoking INIT" 4 | 5 | exec /sbin/init 6 | 7 | # this will only be run if init cannot be run for some reason. 8 | echo "System init failed, starting /bin/sh" 9 | exec /bin/sh 10 | -------------------------------------------------------------------------------- /files/lib/duck.d/98-remove-policy-rc.d: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # vim: filetype=sh 3 | . /lib/libduck.sh 4 | a_get_into target duck/target 5 | 6 | info "Removing policy-rc.d" 7 | 8 | policy_rcd=$target/usr/sbin/policy-rc.d 9 | rm -f $policy_rcd 10 | -------------------------------------------------------------------------------- /bin/duck: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | if __FILE__ == $0 4 | lib = File.expand_path File.join('..', '..', 'lib'), $0 5 | $:.insert 0, lib if File.file? File.join(lib, 'duck.rb') 6 | end 7 | 8 | require 'duck' 9 | exit Duck::main(ARGV) 10 | -------------------------------------------------------------------------------- /files/lib/duck.d/41-update-hostname: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . /lib/libduck.sh 3 | a_get_into hostname hostname 4 | a_get_into target duck/target 5 | 6 | info "duckdb: Setting hostname in target" 7 | echo $hostname > $target/etc/hostname 8 | 9 | exit 0 10 | -------------------------------------------------------------------------------- /files/lib/duck.d/41-add-policy-rc.d: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # vim: filetype=sh 3 | . /lib/libduck.sh 4 | a_get_into target duck/target 5 | 6 | policy_rcd=$target/usr/sbin/policy-rc.d 7 | 8 | ( 9 | set -e 10 | echo "#!/bin/sh" 11 | echo "exit 101" 12 | ) > $policy_rcd 13 | 14 | chmod +x $policy_rcd 15 | -------------------------------------------------------------------------------- /fixes/clear-persistent-udev: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Remove any persistent udev rules 3 | # If the host system runs it's own udev daemon, there is a chance that installation 4 | # hooks will trigger the generation of persistent files. 5 | 6 | set -e 7 | 8 | case "$1" in 9 | "final") rm -f /etc/udev/rules.d/*; ;; 10 | esac 11 | -------------------------------------------------------------------------------- /fixes/squeeze-fix: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # fix for dash hooks that does not run properly on it's own. 3 | # http://wiki.debian.org/Multistrap#Steps_for_Squeeze_and_later 4 | 5 | set -e 6 | 7 | case "$1" in 8 | "pre-bootstrap-configure") 9 | mkdir -p /usr/share/man/man1 10 | ;; 11 | *) ;; 12 | esac 13 | 14 | exit 0 15 | -------------------------------------------------------------------------------- /fixes/busybox-links: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Make busybox applets available in the PATH for general use 3 | 4 | set -e 5 | 6 | case "$1" in 7 | "post-packages-configure") 8 | for a in $(/bin/busybox --list) ; do 9 | which $a >/dev/null || ln -s busybox /bin/$a 10 | done 11 | ;; 12 | esac 13 | 14 | exit 0 15 | -------------------------------------------------------------------------------- /files/lib/duck.d/99-reboot: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . /lib/libduck.sh 3 | a_get_into reboot_enabled "reboot/enabled" "True" 4 | 5 | if [ "$DUCK_MODE" = "testing" ]; then 6 | warning "Not rebooting, duck/mode=testing" 7 | exit 0 8 | fi 9 | 10 | info "Installation done, rebooting!" 11 | 12 | if [ "$reboot_enabled" = "True" ]; then 13 | reboot 14 | else 15 | info "Not rebooting, reboot/enabled=$reboot_enabled" 16 | fi 17 | 18 | exit 0 19 | -------------------------------------------------------------------------------- /files/lib/duck.d/40-debootstrap: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . /lib/libduck.sh 3 | a_get_into target duck/target 4 | a_get_into suite debootstrap/suite 5 | a_get_into mirror debootstrap/mirror 6 | 7 | if [ ! -d "$target" ]; then 8 | error "Target does not exist: $target" 9 | exit 1 10 | fi 11 | 12 | debootstrap $suite $target $mirror 13 | 14 | mount --bind /dev $target/dev 15 | mount -t proc none $target/proc 16 | mount -t sysfs none $target/sys 17 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: ruby-duck 2 | Section: ruby 3 | Priority: optional 4 | Maintainer: Jeremy Huntwork 5 | DM-Upload-Allowed: yes 6 | Build-Depends: debhelper (>= 7.0.50~), gem2deb (>= 0.2.3~) 7 | Standards-Version: 3.9.1 8 | Homepage: https://github.com/spotify/duck 9 | XS-Ruby-Versions: all 10 | 11 | Package: ruby-duck 12 | Architecture: all 13 | XB-Ruby-Versions: ${ruby:Versions} 14 | Depends: ${shlibs:Depends}, ${misc:Depends}, ruby | ruby-interpreter 15 | Description: A minimalist installer system generator 16 | -------------------------------------------------------------------------------- /files/etc/dhclient-exit-hooks.d/hostname: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \ 4 | && [ "$reason" != REBIND ] && [ "$reason" != REBOOT ] 5 | then 6 | return 7 | fi 8 | 9 | if [ ! -z $new_host_name ] ; then 10 | hn="$new_host_name" 11 | [ ! -z $new_domain_name ] && hn="$hn.$new_domain_name" 12 | 13 | # update the hostname 14 | echo $hn > /etc/hostname 15 | hostname $hn 16 | 17 | # restart syslog to use the correct hostname. 18 | invoke-rc.d syslog-ng restart 19 | fi 20 | -------------------------------------------------------------------------------- /lib/duck/enter.rb: -------------------------------------------------------------------------------- 1 | require 'duck/chroot_utils' 2 | require 'duck/logging' 3 | 4 | module Duck 5 | class Enter 6 | include ChrootUtils 7 | include Logging 8 | 9 | def self.doc 10 | "Enter the build chroot" 11 | end 12 | 13 | def initialize(options) 14 | @target = options[:target] 15 | @shell = options[:shell] 16 | @chroot_env = options[:env] || {} 17 | end 18 | 19 | def execute 20 | log.info "Entering #{@target}" 21 | chroot [@target, @shell], :env => @chroot_env 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/duck/logging.rb: -------------------------------------------------------------------------------- 1 | require 'logger' 2 | 3 | module Logging 4 | def log 5 | @log ||= Logging.logger_for(self.class.name) 6 | end 7 | 8 | @loggers = {} 9 | @log_level = Logger::INFO 10 | 11 | class << self 12 | def logger_for(name) 13 | @loggers[name] ||= setup_logger_for(name) 14 | end 15 | 16 | def setup_logger_for(name) 17 | log = Logger.new(STDOUT) 18 | log.progname = name 19 | log.level = @log_level 20 | log 21 | end 22 | 23 | def set_level(level) 24 | @loggers.each do |key, value| 25 | value.level = level 26 | end 27 | 28 | @log_level = level 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /files/sbin/duckinstall: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | . /lib/libduck.sh 3 | a_get_into log duck/log "$DEFAULT_LOG" 4 | a_get_into error_command 'duck/error-command' "$DUCK_LOGIN" 5 | a_get_into success_command 'duck/success-command' "$DUCK_LOGIN" 6 | 7 | trap $error_command INT TERM 8 | 9 | info "Duck Installer $DUCK_VERSION" 10 | 11 | if [[ -f $INSTALLER_STATUS ]]; then 12 | info "Not running installation, $INSTALLER_STATUS exists" 13 | exec $DUCK_LOGIN 14 | fi 15 | 16 | info "Running installation, logging to $log and syslog" 17 | 18 | if ! run_installer 2>&1 | tee -a $log | logger -t duckinstall -s; then 19 | exec $error_command 20 | fi 21 | 22 | touch $INSTALLER_STATUS 23 | exec $success_command 24 | -------------------------------------------------------------------------------- /files/lib/duck.d/02-setup-network: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . /lib/libduck.sh 3 | 4 | if [ "$DUCK_MODE" = "testing" ]; then 5 | warning "Skipping network configuration, duck/mode=testing" 6 | exit 0 7 | fi 8 | 9 | info "network: Setting up loopback interface" 10 | 11 | info "network: lo: address" 12 | ip addr add 127.0.0.1/8 dev lo || true 13 | info "network: lo: route" 14 | ip route add 127.0.0.0/8 dev lo || true 15 | info "network: lo: link up" 16 | ip link set lo up 17 | 18 | info "network: eth0: Configuring using dhcp" 19 | 20 | if ! dhclient eth0; then 21 | error "Network setup failed" 22 | exit 1 23 | fi 24 | 25 | info "network: eth0: link up" 26 | 27 | # Set hostname from DHCP 28 | a_set hostname $(hostname) 29 | 30 | exit 0 31 | -------------------------------------------------------------------------------- /files/lib/duck.d/00-splash: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | . /lib/libduck.sh 3 | 4 | cat - < Mon, 20 Oct 2014 17:23:25 -0500 6 | 7 | ruby-duck (0.4.1-1) precise; urgency=low 8 | 9 | * Use install with explicit permissions to copy the kernel 10 | 11 | -- Jeremy Huntwork Wed, 16 Apr 2014 15:53:32 -0500 12 | 13 | ruby-duck (0.4.0-1) precise; urgency=low 14 | 15 | * add --compression option 16 | * set some force defaults for files installed with dpkg --configure 17 | 18 | -- Jeremy Huntwork Wed, 16 Apr 2014 14:20:32 -0500 19 | 20 | ruby-duck (0.3.0-1) precise; urgency=low 21 | 22 | * add --builddir and --vmlinuz options 23 | 24 | -- Nathan Ferch Wed, 30 Oct 2013 17:46:45 +0000 25 | 26 | ruby-duck (0.2.1-1) precise; urgency=low 27 | 28 | * Initial release. 29 | 30 | -- Jeremy Huntwork Fri, 27 Sep 2013 18:59:18 +0000 31 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | duck 2 | ---- 3 | 4 | A minimalist installer system generator. 5 | 6 | about 7 | ----- 8 | 9 | Duck will help you generate installer system images setup as an initrd, similar 10 | to how debian-installer works. 11 | 12 | There are however a few goals with duck that are different. 13 | 14 | * No memory restrictions (debian-installer is geared towards 32M) 15 | * Less rigorous space restrictions 16 | - No special packaging necessary, but encouraged: http://www.emdebian.org/ 17 | - No library stripping. 18 | 19 | All in all, you get a slightly bigger installer image, but a lot more 20 | flexibility. 21 | 22 | usage 23 | ----- 24 | 25 | Duck has two steps in creating an initrd. 26 | 27 | setup - Setup base configuration. 28 | 29 | build - Build the base system. 30 | pack - Pack the resulting initrd.img 31 | 32 | For testing/tweaking there are two other commands available. 33 | 34 | enter - Enter the chroot that is used to build the initrd. 35 | qemu - Run the resulting initrd in a qemu-based virtual environment. 36 | -------------------------------------------------------------------------------- /duck.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib/', __FILE__) 3 | $:.unshift lib unless $:.include?(lib) 4 | 5 | require 'duck/version' 6 | 7 | Gem::Specification.new do |s| 8 | s.name = "duck-installer" 9 | s.version = Duck::VERSION 10 | s.platform = Gem::Platform::RUBY 11 | s.authors = ["John-John Tedro", "Nathan Ferch", "Martin Parm"] 12 | s.email = ["johnjohn.tedro@gmail.com"] 13 | s.homepage = "http://github.com/udoprog/duck" 14 | s.summary = "Tool for generating a minimalistic initramfs installer system" 15 | s.description = "Duck takes a configuration and generates a bootable initramfs that gives over to a minimalistic installation environment based on (em)debian." 16 | 17 | s.required_rubygems_version = ">= 1.3.6" 18 | s.rubyforge_project = "duck" 19 | 20 | s.add_development_dependency "rspec" 21 | 22 | s.files = Dir.glob("{lib,files,fixes}/**/*") + %w(LICENSE README duck.yaml) 23 | s.executables << 'duck' 24 | s.require_path = 'lib' 25 | end 26 | -------------------------------------------------------------------------------- /files/etc/inittab: -------------------------------------------------------------------------------- 1 | # The default runlevel. 2 | id:2:initdefault: 3 | # Boot-time system configuration/initialization script. 4 | # This is run first except when booting in emergency (-b) mode. 5 | si::sysinit:/etc/init.d/rcS 6 | # What to do in single-user mode. 7 | ~~:S:wait:/sbin/sulogin 8 | 9 | l0:0:wait:/etc/init.d/rc 0 10 | l1:1:wait:/etc/init.d/rc 1 11 | l2:2:wait:/etc/init.d/rc 2 12 | l6:6:wait:/etc/init.d/rc 6 13 | # Normally not reached, but fallthrough in case of emergency. 14 | z6:6:respawn:/sbin/sulogin 15 | # What to do when CTRL-ALT-DEL is pressed. 16 | ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now 17 | # What to do when the power fails/returns. 18 | pf::powerwait:/etc/init.d/powerfail start 19 | pn::powerfailnow:/etc/init.d/powerfail now 20 | po::powerokwait:/etc/init.d/powerfail stop 21 | 22 | 1:23:respawn:/sbin/agetty -n -l /sbin/duckinstall 38400 tty1 23 | 2:23:respawn:/sbin/agetty -n -l /sbin/ducklogin 38400 tty2 24 | 3:23:respawn:/sbin/agetty -n -l /sbin/ducklogin 38400 tty3 25 | 4:23:respawn:/sbin/agetty -n -l /sbin/ducklogin 38400 tty4 26 | -------------------------------------------------------------------------------- /lib/duck/qemu.rb: -------------------------------------------------------------------------------- 1 | require 'duck/spawn_utils' 2 | 3 | module Duck 4 | class Qemu 5 | include SpawnUtils 6 | include Logging 7 | 8 | def self.doc 9 | "Spawn a qemu instance using the packed image" 10 | end 11 | 12 | def initialize(options) 13 | @target = options[:target] 14 | @kernel = options[:kernel] 15 | @initrd = options[:initrd] 16 | @append = options[:append] 17 | raise "No kernel specified" unless @kernel 18 | raise "Specified kernel does not exist: #{@kernel}" unless File.file? @kernel 19 | raise "No initrd specified" unless @initrd 20 | raise "Specified initrd does not exist: #{@initrd}" unless File.file? @initrd 21 | end 22 | 23 | def execute 24 | append = 'console=ttyS0 duck/mode=testing' 25 | append = "#{append} #{@append}" if @append 26 | 27 | opts = ['-serial', 'stdio', '-m', '1024', '-append', append] 28 | 29 | args = [ 30 | '-kernel', @kernel, 31 | '-initrd', @initrd, 32 | ] + opts 33 | 34 | log.info "Executing QEMU on #{@initrd}" 35 | qemu *args 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/duck/chroot_utils.rb: -------------------------------------------------------------------------------- 1 | require 'duck/spawn_utils' 2 | 3 | module ChrootUtils 4 | include SpawnUtils 5 | 6 | CHROOT = 'chroot' 7 | APT_GET = 'apt-get' 8 | APT_KEY = 'apt-key' 9 | DPKG = 'dpkg' 10 | UPDATE_RCD = 'update-rc.d' 11 | SH = 'bash' 12 | 13 | CHROOT_ENV = { 14 | 'DEBIAN_FRONTEND' => 'noninteractive', 15 | 'DEBCONF_NONINTERACTIVE_SEEN' => 'true', 16 | 'LC_ALL' => 'C', 17 | 'LANGUAGE' => 'C', 18 | 'LANG' => 'C', 19 | } 20 | 21 | def chroot(args, options={}) 22 | spawn [CHROOT] + args, options 23 | end 24 | 25 | # for doing automated tasks inside of the chroot. 26 | def auto_chroot(args, opts={}) 27 | log.debug "chroot: #{args.join ' '}" 28 | opts[:env] = (opts[:env] || {}).update(@chroot_env || {}).merge(CHROOT_ENV) 29 | chroot [@target] + args, opts 30 | end 31 | 32 | def in_apt_get(*args) 33 | auto_chroot [APT_GET, '-y', '--force-yes'] + args 34 | end 35 | 36 | def in_apt_key(args, opts) 37 | auto_chroot [APT_KEY] + args, opts 38 | end 39 | 40 | def in_dpkg(*args) 41 | auto_chroot [DPKG] + args 42 | end 43 | 44 | def in_shell(command) 45 | auto_chroot [SH, '-c', command] 46 | end 47 | 48 | def in_update_rcd(*args) 49 | auto_chroot [UPDATE_RCD] + args 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/duck/module_helper.rb: -------------------------------------------------------------------------------- 1 | module ModuleHelper 2 | class StepError < Exception 3 | end 4 | 5 | class Step 6 | attr_accessor :name, :disable_hook 7 | 8 | def initialize(name, params={}) 9 | @name = name 10 | @disable_hook = params[:disable_hook] || false 11 | end 12 | end 13 | 14 | module ClassMethods 15 | def step(name, params={}) 16 | @steps << Step.new(name, params) 17 | end 18 | 19 | def steps 20 | @steps 21 | end 22 | end 23 | 24 | def self.included(mod) 25 | mod.extend ClassMethods 26 | mod.instance_variable_set :@steps, [] 27 | end 28 | 29 | def pre_hook(name); end 30 | def post_hook(name); end 31 | def final_hook(); end 32 | 33 | def execute 34 | self.class.steps.each do |step| 35 | name = step.name.to_s.gsub '_', '-' 36 | 37 | log.info "#{name}: running" 38 | pre_hook name unless step.disable_hook 39 | 40 | begin 41 | self.method(step.name).call 42 | rescue StepError 43 | log.error "#{name}: #{$!}" 44 | return 45 | end 46 | 47 | post_hook name unless step.disable_hook 48 | log.info "#{name}: done" 49 | end 50 | 51 | final_hook 52 | 53 | # run fixes for finalizing the setup. 54 | return 0 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /duck.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | packages: 3 | - isc-dhcp-client 4 | - syslog-ng 5 | - udev 6 | - vim-tiny 7 | - busybox 8 | - python-argparse 9 | 10 | files: 11 | # init file 12 | - from: /* 13 | to: / 14 | mode: 0755 15 | - from: /sbin/* 16 | to: /sbin/ 17 | mode: 0755 18 | # configuration files 19 | - from: /etc/* 20 | to: /etc/ 21 | - from: /etc/dhclient-exit-hooks.d/* 22 | to: /etc/dhcp/dhclient-exit-hooks.d/ 23 | mode: 0755 24 | # installer library 25 | - from: /lib/libduck.sh 26 | to: /lib/ 27 | mode: 0755 28 | - from: /lib/python-duck/duck/* 29 | to: /lib/python-duck/duck/ 30 | # installation steps 31 | - from: /lib/duck.d/* 32 | to: /lib/duck.d/ 33 | mode: 0755 34 | # hooks 35 | - from: /lib/duck-hooks.d/* 36 | to: /lib/duck-hooks.d/ 37 | mode: 0755 38 | 39 | fixes: 40 | # file contains scripts that should be run to fix the target environment. 41 | # scripts are located under /fixes 42 | - squeeze-fix 43 | # disable initrd generation and grub setup for installed kernels. 44 | - kernel-boot-fix 45 | # remove persistent udev rules. 46 | - clear-persistent-udev 47 | # add busybox links 48 | - busybox-links 49 | 50 | keyring: 51 | keyserver: "subkeys.pgp.net" 52 | keys: 53 | # debian archive keys 54 | - "473041FA" 55 | - "B98321F9" 56 | -------------------------------------------------------------------------------- /files/lib/python-duck/duck/db.py: -------------------------------------------------------------------------------- 1 | import dbm 2 | import json 3 | import contextlib 4 | 5 | DEFAULT_DB = "/var/duck" 6 | DEFAULT_ENCODING = 'utf-8' 7 | 8 | 9 | class DBSession(object): 10 | def __init__(self, db, encoding): 11 | self._db = db 12 | self._encoding = encoding 13 | 14 | def get(self, key, default=None): 15 | try: 16 | value = self._db[key] 17 | except KeyError: 18 | return default 19 | 20 | if value is None: 21 | return None 22 | 23 | value = value.decode(self._encoding) 24 | value = json.loads(value) 25 | return value 26 | 27 | def set(self, key, value): 28 | value = json.dumps(value) 29 | value = value.encode(self._encoding) 30 | self._db[key] = value 31 | 32 | def keys(self): 33 | return self._db.keys() 34 | 35 | 36 | class DB(object): 37 | def __init__(self, path=None, encoding=None): 38 | if path is None: 39 | path = DEFAULT_DB 40 | if encoding is None: 41 | encoding = DEFAULT_ENCODING 42 | self._full_path = "{0}.dbm".format(path) 43 | self._encoding = encoding 44 | 45 | @contextlib.contextmanager 46 | def open(self): 47 | with contextlib.closing(dbm.open(self._full_path, "c")) as db: 48 | yield DBSession(db, self._encoding) 49 | -------------------------------------------------------------------------------- /fixes/kernel-boot-fix: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Make sure that the kernel does not generate an initramfs. 3 | # Clear /boot before packages are being configured. 4 | 5 | set -e 6 | 7 | status_code=0 8 | 9 | case "$1" in 10 | "pre-packages-configure") 11 | echo "Disabling generation of an initramfs" 12 | 13 | rm -rf /etc/kernel/postinst.d 14 | rm -rf /etc/kernel/postrm.d 15 | 16 | dpkg --get-selections | while read name _; do 17 | case "$name" in 18 | linux-image-*-dbg) continue ;; 19 | linux-image-*) ;; 20 | *) continue ;; 21 | esac 22 | 23 | version=${name#linux-image-*} 24 | 25 | path="/var/lib/dpkg/info/$name.postinst" 26 | 27 | [[ ! -f $path ]] && continue 28 | 29 | if ! grep -E 'my \$initrd\s+=\s+"YES";' $path; then 30 | echo "initrd already disabled: $name" 31 | continue 32 | fi 33 | 34 | cp -a $path $path.original 35 | 36 | echo "Disabling initrd for: $name" 37 | 38 | if ! sed -r -i 's/my \$initrd\s+=\s+"YES";/my $initrd = "";/' $path; then 39 | echo "failed to patch: $path" 40 | exit 1 41 | fi 42 | 43 | echo "Generating modules.dep" 44 | depmod $version 45 | done 46 | ;; 47 | "final") 48 | echo "Clearing /boot" 49 | rm -rf /boot 50 | ;; 51 | esac 52 | 53 | exit 0 54 | -------------------------------------------------------------------------------- /lib/duck/spawn_utils.rb: -------------------------------------------------------------------------------- 1 | require 'shellwords' 2 | 3 | module SpawnUtils 4 | SH = 'sh' 5 | DEBOOTSTRAP = 'debootstrap' 6 | GPG = 'gpg' 7 | QEMU = 'qemu-system-x86_64' 8 | 9 | class ExitError < Exception 10 | attr_accessor :exitcode 11 | 12 | def initialize(message, exitcode=1) 13 | @exitcode = exitcode 14 | super message 15 | end 16 | end 17 | 18 | def spawn(args, options={}) 19 | env = options[:env] || {} 20 | 21 | repr = args.map do |i| 22 | if i.nil? 23 | '' 24 | else 25 | Shellwords.escape i 26 | end 27 | end.join " " 28 | 29 | log.debug "exec: #{repr}" 30 | 31 | stdin_r = nil 32 | stdout_w = nil 33 | 34 | if options[:input_file] 35 | stdin_r = options[:input_file] 36 | end 37 | 38 | if options[:output_file] 39 | stdout_w = options[:output_file] 40 | end 41 | 42 | child_pid = fork do 43 | ENV.update env 44 | $stdin.reopen stdin_r unless stdin_r.nil? 45 | $stdout.reopen stdout_w unless stdout_w.nil? 46 | exec *args 47 | exit 255 48 | end 49 | 50 | Process.wait child_pid 51 | exit_status = $?.exitstatus 52 | 53 | if exit_status != 0 54 | raise ExitError.new "#{repr}: Subprocess returned non-zero exit status #{exit_status}" 55 | end 56 | 57 | exit_status 58 | end 59 | 60 | def shell(command) 61 | spawn [SH, '-c', command] 62 | end 63 | 64 | def debootstrap(suite, target, options={}) 65 | # Extra arguments have to be early when running debootstrap. 66 | args = Array.new(options[:extra] || []) 67 | args << suite << target 68 | args << options[:mirror] if options.include? :mirror 69 | spawn [DEBOOTSTRAP] + args 70 | end 71 | 72 | def gpg(args, options={}) 73 | gpg_args = [] 74 | gpg_args << "--homedir" << options[:homedir] if options[:homedir] 75 | gpg_args << "--keyserver" << options[:keyserver] if options[:keyserver] 76 | gpg_args += args 77 | spawn [GPG] + gpg_args 78 | end 79 | 80 | def qemu(*args) 81 | spawn [QEMU] + args 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /lib/duck/pack.rb: -------------------------------------------------------------------------------- 1 | require 'duck/chroot_utils' 2 | require 'duck/logging' 3 | 4 | module Duck 5 | class Pack 6 | include ChrootUtils 7 | include Logging 8 | 9 | def self.doc 10 | "Pack the chroot into an archive" 11 | end 12 | 13 | def initialize(options) 14 | @target = options[:target] 15 | @temp = options[:temp] 16 | @original_target = @target 17 | @target_min = "#{@target}.min" 18 | @chroot_env = options[:env] 19 | @initrd = options[:initrd] 20 | @initrd_kernel = options[:initrd_kernel] 21 | @no_minimize = options[:no_minimize] 22 | @keep_minimized = options[:keep_minimized] 23 | @keep_builddir = options[:keep_builddir] 24 | @strip = options[:strip] 25 | @compression = options[:compression] 26 | end 27 | 28 | def minimize_target 29 | log.info "Minimizing Target" 30 | spawn ['rm', '-rf', @target_min] if File.directory? @target_min 31 | spawn ['cp', '-a', @target, @target_min] 32 | 33 | @target = @target_min 34 | 35 | in_apt_get "clean" 36 | in_shell "rm -rf /boot /usr/share/doc /var/cache/{apt,debconf}/* /vmlinuz" 37 | in_shell "find /var/lib/apt/lists/ -type f ! -name lock -delete" 38 | end 39 | 40 | def execute 41 | minimize_target unless @no_minimize 42 | 43 | Dir.chdir @target 44 | if @strip 45 | log.info "Stripping contents of #{@target}" 46 | shell "find . -type f -exec strip --strip-unneeded -R .comment -R .note '{}' + >/dev/null 2>&1 || true" 47 | end 48 | log.info "Packing #{@target} into #{@initrd}" 49 | shell "find . | cpio -o -H newc | #{@compression} > #{@initrd}" 50 | 51 | spawn ['rm', '-r', '-f', @target] unless @keep_minimized 52 | 53 | log.info "Copying kernel from #{@original_target}/vmlinuz to #{@initrd_kernel}" 54 | spawn ['install', '-m', '0644', "#{@original_target}/vmlinuz", @initrd_kernel] 55 | 56 | spawn ['rm', '-r', '-f', @temp] unless @keep_builddir 57 | 58 | log.info "Done building initramfs image: #{@initrd}" 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /files/lib/libduck.sh: -------------------------------------------------------------------------------- 1 | # static variables 2 | export DUCK_VERSION="0.1" 3 | export DUCKDB_CONF="/duckdb.conf" 4 | export DUCKDB_JSON="/duckdb.json" 5 | # if this file exists, the installation loop should not run. 6 | export INSTALLER_STATUS="/.installer_status" 7 | # default logging location. 8 | export DEFAULT_LOG="/var/log/duckinstall.log" 9 | export DUCK_LOGIN="/sbin/ducklogin" 10 | export DUCK_HOOKS="/lib/duck-hooks.d" 11 | export DUCK_PYTHONLIB="/lib/python-duck" 12 | export PYTHONPATH="$DUCK_PYTHONLIB" 13 | 14 | invoke_hook() { 15 | name=$1 16 | shift 17 | path=$DUCK_HOOKS/$name 18 | [ -x $path ] && ( $path "$@" || true ) 19 | } 20 | 21 | info() { 22 | echo "INFO : $@"; 23 | invoke_hook log info "$@" 24 | } 25 | 26 | warning() { 27 | echo "WARNING : $@"; 28 | invoke_hook log warning "$@" 29 | } 30 | 31 | error() { 32 | echo "ERROR : $@"; 33 | invoke_hook log error "$@" 34 | } 35 | 36 | setup_duckdb() { 37 | info "duckdb: Loading Static Variables" 38 | 39 | if [ -f "$DUCKDB_CONF" ]; then 40 | info "duckdb: Loading $DUCKDB_CONF" 41 | duckdb url file://$DUCKDB_CONF 42 | fi 43 | 44 | if [ -f "$DUCKDB_JSON" ]; then 45 | info "duckdb: Loading $DUCKDB_JSON" 46 | duckdb url --json file://$DUCKDB_JSON 47 | fi 48 | 49 | info "duckdb: Loading /proc/cmdline" 50 | 51 | if ! duckdb url --cmdline file:///proc/cmdline; then 52 | error "duckdb: Failed to load kernel arguments" 53 | return 1 54 | fi 55 | 56 | # Time to enable hooks. 57 | duckdb set --json duck/hooks-enabled true 58 | duckdb set --json duck/log-hook-enabled true 59 | } 60 | 61 | run_installer() { 62 | if ! setup_duckdb; then 63 | return 1 64 | fi 65 | 66 | for script in /lib/duck.d/[0-9][0-9]-*; do 67 | [ ! -x "$script" ] && continue 68 | 69 | info "Running: $script" 70 | logger -t installer "running: $script" 71 | 72 | if ! $script; then 73 | error "Failed: $script" 74 | return 1 75 | fi 76 | done 77 | 78 | return 0 79 | } 80 | 81 | # Run a command in the target environment. 82 | # required duckdb variables: 83 | # - duck/target 84 | in_target() { 85 | # run chroot invocation inside of a subshell 86 | # this allows us to override some useful environment variables 87 | # at leisure. 88 | 89 | a_get_into target duck/target 90 | 91 | command="$@" 92 | 93 | info "in-target: $command" 94 | 95 | ( 96 | export DEBIAN_FRONTEND=noninteractive 97 | export DEBCONF_NONINTERACTIVE_SEEN=true 98 | export LC_ALL=C 99 | export LANGUAGE=C 100 | export LANG=C 101 | exec chroot $target $command 102 | ) 103 | 104 | return $? 105 | } 106 | 107 | 108 | # get single duckdb variable 109 | # exports the RET variable containing the value of the requested variable 110 | # or invokes 'exit 1' if it was unable to fetch the value from duckdb. 111 | # 112 | # a_get duck/mode 113 | # duck_mode="$RET" 114 | # 115 | # duckdb supprts the notion of default values, in that case, two arguments 116 | # should be provided, as follows. 117 | # 118 | # a_get duck/mode testing 119 | # duck_mode="$RET" 120 | a_get() { 121 | export DUCK_RETURN="" 122 | export DUCK_OK="no" 123 | 124 | eval $(duckdb get --sh "$@") 125 | 126 | if [ "$DUCK_OK" != "yes" ]; then 127 | error "Missing required duckdb variable: $1" 128 | exit 1 129 | fi 130 | 131 | export RET=$DUCK_RETURN 132 | } 133 | 134 | # This function was introduced because the common idiom of assigning RET 135 | # resulted in code which was error prone. 136 | a_get_into() { 137 | name=$1 138 | shift 139 | a_get "$@" 140 | export "$name"="$RET" 141 | } 142 | 143 | # set single duckdb variable 144 | a_set() { 145 | eval $(duckdb set "$@"); 146 | } 147 | 148 | # Dynamic Variables 149 | a_get_into DUCK_MODE duck/mode "testing" 150 | -------------------------------------------------------------------------------- /files/sbin/duckdb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # 3 | # AutoDB is used to store duckinstaller variables. 4 | # 5 | 6 | import duck.db as duck_db 7 | import duck 8 | 9 | import argparse 10 | import urllib2 11 | import json 12 | import contextlib 13 | 14 | VERSION = duck.__version_string__ 15 | 16 | 17 | def update_db(function): 18 | def inner(ns): 19 | with ns.db.open() as S: 20 | for key, value in function(S, ns): 21 | if ns.ns: 22 | key = "%s/%s" % (ns.ns, key) 23 | S.set(key, value) 24 | 25 | return 0 26 | 27 | return inner 28 | 29 | 30 | def parse_text(fd): 31 | for line in fd: 32 | line = line.strip() 33 | 34 | if line.startswith("#") or not line: 35 | continue 36 | 37 | key, value = line.split(" ", 2) 38 | yield key, value 39 | 40 | 41 | def flatten_dict(doc, keys=[]): 42 | result = dict() 43 | 44 | for key, value in doc.items(): 45 | this_keys = keys + [key] 46 | 47 | if isinstance(value, dict): 48 | result.update(flatten_dict(value, this_keys)) 49 | else: 50 | result["/".join(this_keys)] = value 51 | 52 | return result 53 | 54 | 55 | def parse_json(fd): 56 | doc = json.load(fd) 57 | 58 | for key, value in doc.items(): 59 | yield key, value 60 | 61 | 62 | def parse_cmdline(fd): 63 | cmdline = fd.read() 64 | 65 | for c in cmdline.split(" "): 66 | c = c.strip() 67 | 68 | if not c or '=' not in c: 69 | continue 70 | 71 | key, value = c.split("=", 2) 72 | yield key, value 73 | 74 | 75 | def read_dict(mode, fd): 76 | if mode == 'cmdline': 77 | return parse_cmdline(fd) 78 | 79 | if mode == 'json': 80 | return parse_json(fd) 81 | 82 | if mode == 'text': 83 | return parse_text(fd) 84 | 85 | raise Exception("Unknown file mode: {0}".format(mode)) 86 | 87 | 88 | def read(ns, fd): 89 | value = dict(read_dict(ns.mode, fd)) 90 | 91 | if ns.flatten: 92 | return flatten_dict(value) 93 | 94 | return value 95 | 96 | 97 | def action_get(ns): 98 | with ns.db.open() as S: 99 | value = S.get(ns.key, ns.default) 100 | 101 | if value is None: 102 | ok = False 103 | value = "" 104 | else: 105 | ok = True 106 | 107 | if ns.sh: 108 | print "DUCK_RETURN=\'%s\';" % (value,) 109 | print "DUCK_OK=\"%s\"" % ("yes" if ok else "no",) 110 | return 0 111 | 112 | if not ok: 113 | return 1 114 | 115 | if ns.raw: 116 | value = repr(value) 117 | 118 | print value 119 | 120 | return 0 121 | 122 | 123 | @update_db 124 | def action_set(S, ns): 125 | if ns.json: 126 | value = json.loads(ns.value) 127 | elif ns.value == '-': 128 | value = sys.stdin.read() 129 | else: 130 | value = ns.value 131 | 132 | yield ns.key, value 133 | 134 | 135 | @update_db 136 | def action_url(S, ns): 137 | with contextlib.closing(urllib2.urlopen(ns.url)) as fd: 138 | for key, value in read(ns, fd).items(): 139 | yield key, value 140 | 141 | 142 | def action_list(ns): 143 | with ns.db.open() as S: 144 | for key in S.keys(): 145 | print key, repr(S.get(key)) 146 | 147 | 148 | def main(args): 149 | parser = argparse.ArgumentParser( 150 | usage="usage: %(prog)s [options] [action-options]") 151 | 152 | parser.add_argument("-v", "--version", action='version', version=VERSION) 153 | 154 | parser.add_argument( 155 | "--db", 156 | metavar="", 157 | default=None) 158 | 159 | parser.add_argument( 160 | "--ns", 161 | metavar="", 162 | default=None) 163 | 164 | parsers = parser.add_subparsers() 165 | 166 | get_parser = parsers.add_parser("get", help="Get a value") 167 | get_parser.add_argument("key") 168 | get_parser.add_argument("default", nargs='?', default=None) 169 | get_parser.add_argument("--sh", 170 | help=("Output return value as a shell " 171 | "evaluable string"), 172 | default=False, 173 | action='store_true') 174 | get_parser.add_argument("--raw", 175 | help=("Output raw (repr) value"), 176 | default=False, 177 | action='store_true') 178 | get_parser.set_defaults(action=action_get) 179 | 180 | set_parser = parsers.add_parser("set", help="Set a value") 181 | set_parser.add_argument("--json", 182 | help="treat argument as json", 183 | default=False, 184 | action='store_true') 185 | set_parser.add_argument("key") 186 | set_parser.add_argument("value") 187 | set_parser.set_defaults(action=action_set) 188 | 189 | list_parser = parsers.add_parser("list", help="List all values") 190 | list_parser.set_defaults(action=action_list) 191 | 192 | url_parser = parsers.add_parser("url", 193 | help="Read values from an url") 194 | url_parser.add_argument("url", help="Fetch values from specified url") 195 | url_parser.add_argument("--json", dest='mode', 196 | help="Treat input as a json document", 197 | action='store_const', const='json') 198 | url_parser.add_argument("--cmdline", dest='mode', 199 | help="Treat input as a /proc/cmdline file", 200 | action='store_const', const='cmdline') 201 | url_parser.add_argument("--flatten", 202 | help="Flatten a nested dictionary", 203 | default=False, action='store_true') 204 | url_parser.set_defaults(action=action_url, mode='file') 205 | 206 | ns = parser.parse_args(args) 207 | ns.db = duck_db.DB(path=ns.db) 208 | return ns.action(ns) 209 | 210 | if __name__ == "__main__": 211 | import sys 212 | sys.exit(main(sys.argv[1:])) 213 | -------------------------------------------------------------------------------- /lib/duck.rb: -------------------------------------------------------------------------------- 1 | require 'optparse' 2 | require 'fileutils' 3 | require 'yaml' 4 | require 'find' 5 | require 'logger' 6 | 7 | require 'duck/logging' 8 | require 'duck/build' 9 | require 'duck/enter' 10 | require 'duck/pack' 11 | require 'duck/qemu' 12 | require 'duck/version' 13 | 14 | module Duck 15 | class << self 16 | include Logging 17 | end 18 | 19 | # environment to prevent tasks from being interactive. 20 | DEFAULT_SHELL = '/bin/bash' 21 | CONFIG_NAME = 'duck.yaml' 22 | CONFIG_ARRAYS = [:files, :packages, :transports, :preferences, :fixes, :services, :sources] 23 | 24 | ACTIONS = { 25 | :build => Duck::Build, 26 | :enter => Duck::Enter, 27 | :pack => Duck::Pack, 28 | :qemu => Duck::Qemu, 29 | } 30 | 31 | def self.resource_path(path) 32 | File.expand_path File.join('..', '..', path), __FILE__ 33 | end 34 | 35 | def self.parse_options(args) 36 | o = Hash.new 37 | 38 | working_directory = Dir.pwd 39 | 40 | o[:temp] = File.join working_directory, 'tmp' 41 | o[:target] = File.join o[:temp], 'initrd' 42 | o[:initrd] = File.join working_directory, 'duck-initrd.img' 43 | o[:initrd_kernel] = File.join working_directory, 'duck-vmlinuz' 44 | o[:gpg_homedir] = File.join o[:temp], 'gpg' 45 | o[:kernel] = File.join working_directory, 'vmlinuz' 46 | o[:no_minimize] = false 47 | o[:append] = nil 48 | o[:keep_minimized] = false 49 | o[:keep_builddir] = false 50 | o[:compression] = 'gzip' 51 | o[:shell] = DEFAULT_SHELL 52 | o[:_configs] = [] 53 | o[:_roots] = [] 54 | o[:strip] = false 55 | 56 | CONFIG_ARRAYS.each do |array| 57 | o[array] = [] 58 | end 59 | 60 | action_names = [:build, :pack] 61 | 62 | opts = OptionParser.new do |opts| 63 | opts.banner = 'Usage: duck [action] [options]' 64 | 65 | opts.separator "Actions:" 66 | 67 | ACTIONS.each do |k, klass| 68 | opts.separator " #{k}: #{klass.doc}" 69 | end 70 | 71 | opts.separator "Options:" 72 | 73 | opts.on('-b ', '--builddir ', 74 | 'Use the following directory for the build ') do |dir| 75 | unless dir =~ /^\// 76 | dir=File.join working_directory+'/'+dir 77 | end 78 | puts "dir is #{dir}" 79 | o[:temp] = dir 80 | o[:target] = File.join o[:temp], 'initrd' 81 | o[:gpg_homedir] = File.join o[:temp], 'gpg' 82 | end 83 | 84 | opts.on('-t ', '--target ', 85 | 'Build in the specified target directory') do |dir| 86 | o[:target] = dir 87 | end 88 | 89 | opts.on('--no-minimize', 90 | 'Do not minimize the installation right before packing') do |dir| 91 | o[:no_minimize] = true 92 | end 93 | 94 | opts.on('--keep-minimized', 95 | 'Keep the minimized version of the initrd around') do |dir| 96 | o[:keep_minimized] = true 97 | end 98 | 99 | opts.on('--keep-builddir', 100 | 'Keep the build directory around') do |dir| 101 | o[:keep_builddir] = true 102 | end 103 | 104 | opts.on('--compression ', 105 | 'Specify the compression command to use, default is gzip') do |cmd| 106 | o[:compression] = cmd 107 | end 108 | 109 | opts.on('--debug', 110 | 'Switch on debug logging') do |dir| 111 | Logging::set_level Logger::DEBUG 112 | end 113 | 114 | opts.on('-o ', '--output ', 115 | 'Output initrd to , default is ./duck-initrd.img') do |path| 116 | o[:initrd] = File.expand_path(path) 117 | end 118 | 119 | opts.on('-z ', '--vmlinuz ', 120 | 'Copy the initrd\'s kernel to , default is ./duck-vmlinuz') do |path| 121 | o[:initrd_kernel] = File.expand_path(path) 122 | end 123 | 124 | opts.on('-k ', '--kernel ', 125 | 'Specify kernel to use when running qemu') do |path| 126 | o[:kernel] = File.expand_path(path) 127 | end 128 | 129 | opts.on('-a ', '--append ', 130 | 'Specify kernel options to append') do |append| 131 | o[:append] = append 132 | end 133 | 134 | opts.on('-c ', '--config ', 135 | 'Use the specified configuration path') do |path| 136 | o[:_configs] << File.expand_path(path) 137 | end 138 | 139 | opts.on('-s ', '--shell ', 140 | 'Set the shell to use when chrooting') do |shell| 141 | o[:shell] = shell 142 | end 143 | 144 | opts.on('-x', '--strip', 'Strip files in the initrd') do 145 | raise "No strip utility found." unless system("which strip >/dev/null") 146 | o[:strip] = true 147 | end 148 | 149 | opts.on('-h', '--help', 'Show this message') do 150 | puts opts 151 | return nil 152 | end 153 | 154 | opts.on('-v', '--version', 'Show version') do 155 | puts "duck: version #{VERSION}" 156 | return nil 157 | end 158 | end 159 | 160 | args = opts.parse! args 161 | 162 | unless args.empty? 163 | action_names = args.map{|a| a.to_sym} 164 | end 165 | 166 | # add default configuration if none is specified. 167 | if o[:_configs].empty? 168 | o[:_configs] << File.join(working_directory, CONFIG_NAME) 169 | end 170 | 171 | o[:_configs] = [resource_path(CONFIG_NAME)] + o[:_configs] 172 | 173 | o[:_configs].uniq! 174 | o[:_configs].reject!{|i| not File.file? i} 175 | return action_names, o 176 | end 177 | 178 | def self.deep_symbolize(o) 179 | return o.map{|i| deep_symbolize(i)} if o.is_a? Array 180 | return o unless o.is_a? Hash 181 | c = o.clone 182 | c.keys.each {|k| c[k.to_sym] = deep_symbolize(c.delete(k))} 183 | return c 184 | end 185 | 186 | def self.prepare_options(o) 187 | raise "No configuration found" if o[:_configs].empty? 188 | 189 | [:target].each do |s| 190 | next if File.directory? o[s] 191 | log.info "Creating directory '#{s}' on #{o[s]}" 192 | FileUtils.mkdir_p o[s] 193 | end 194 | 195 | unless File.directory? o[:gpg_homedir] 196 | log.info "Creating directory GPG home directory on #{o[:gpg_homedir]}" 197 | FileUtils.mkdir_p o[:gpg_homedir] 198 | FileUtils.chmod 0700, o[:gpg_homedir] 199 | end 200 | 201 | o[:_configs].each do |config_path| 202 | log.info "Loading configuration from #{config_path}" 203 | config = deep_symbolize YAML.load_file(config_path) 204 | root = File.dirname config_path 205 | # Special keys treated as accumulated arrays over all configurations. 206 | 207 | CONFIG_ARRAYS.each do |n| 208 | o[n] += (config.delete(n) || []).map{|i| [root, i]} 209 | end 210 | 211 | # Merge (overwrite) the rest. 212 | o.merge! config 213 | o[:_roots] << root 214 | end 215 | end 216 | 217 | def self.main(args) 218 | action_names, o = parse_options args 219 | return 0 if o.nil? 220 | prepare_options o 221 | 222 | action_names.each do |action_name| 223 | action_class = ACTIONS[action_name] 224 | 225 | if action_class.nil? 226 | log.error "No such action: #{action_name}" 227 | return 1 228 | end 229 | 230 | action_instance = action_class.new o 231 | action_instance.execute 232 | end 233 | 234 | return 0 235 | end 236 | end 237 | 238 | if __FILE__ == $0 239 | exit Duck::main(ARGV) 240 | end 241 | -------------------------------------------------------------------------------- /lib/duck/build.rb: -------------------------------------------------------------------------------- 1 | require 'fileutils' 2 | 3 | require 'duck/chroot_utils' 4 | require 'duck/logging' 5 | require 'duck/module_helper' 6 | 7 | module Duck 8 | class Build 9 | include Logging 10 | include ChrootUtils 11 | include ModuleHelper 12 | 13 | def self.doc 14 | "Build the chroot" 15 | end 16 | 17 | FixesDir = 'fixes' 18 | FilesDir = 'files' 19 | KeysDir = 'keys' 20 | KeysRingsDir = 'keyrings' 21 | BootstrapStatus = '.bootstrap' 22 | DefaultSourceType = 'deb' 23 | DefaultComponents = ['main'] 24 | DefaultSuite = 'squeeze' 25 | 26 | def initialize(options) 27 | @_roots = options[:_roots] 28 | @target = options[:target] 29 | @temp = options[:temp] 30 | @chroot_env = options[:env] || {} 31 | @packages = options[:packages] || [] 32 | @fixes = options[:fixes] || [] 33 | @sources = options[:sources] 34 | @transports = options[:transports] 35 | @bootstrap = validate_bootstrap options[:bootstrap] 36 | @keyring = validate_keyring options[:keyring] 37 | @files = validate_array [:from, :to], options[:files] 38 | @services = validate_array [:name], options[:services] 39 | @preferences = validate_array [:package, :pin, :priority], options[:preferences] 40 | 41 | if @bootstrap[:tarball] 42 | @bootstrap_tarball = File.join @temp, @bootstrap[:tarball] 43 | end 44 | 45 | @fixes_target = File.join @target, FixesDir 46 | @bootstrap_status = File.join @target, BootstrapStatus 47 | end 48 | 49 | def validate_keyring(opts) 50 | return nil unless opts 51 | raise "Missing required keyring option 'keyserver'" unless opts[:keyserver] 52 | opts[:keys] = [] unless opts[:keys] 53 | opts 54 | end 55 | 56 | def validate_bootstrap(opts) 57 | raise "Missing bootstrap options" unless opts 58 | opts[:suite] = DefaultSuite unless opts[:suite] 59 | return opts 60 | end 61 | 62 | def validate_item(keys, item) 63 | keys.each {|k| raise "Missing '#{k}' declaration" unless item[k]} 64 | end 65 | 66 | def validate_array(keys, items) 67 | items.each {|root, item| validate_item keys, item} 68 | end 69 | 70 | def copy_fixes 71 | FileUtils.mkdir_p @fixes_target unless File.directory? @fixes_target 72 | 73 | @fixes.each do |root, fix_name| 74 | source = File.join root, FixesDir, fix_name 75 | target = File.join @fixes_target, fix_name 76 | 77 | next unless File.file? source 78 | next if File.file? target and File.mtime(source) > File.mtime(target) 79 | 80 | log.debug "copying fix #{source} to #{target}" 81 | FileUtils.cp source, target 82 | FileUtils.chmod 0755, target 83 | end 84 | end 85 | 86 | def clear_fixes 87 | FileUtils.rm_rf @fixes_target 88 | end 89 | 90 | def run_fixes(stage) 91 | return unless File.directory? @fixes_target 92 | 93 | log.info "fixes: #{stage}" 94 | 95 | @fixes.each do |root, fix_name| 96 | log.debug "fix: #{fix_name} #{stage}" 97 | executable = File.join '/', FixesDir, fix_name 98 | exitcode = chroot [@target, executable, stage] 99 | raise "fix failed: #{fix_name} #{stage}" if exitcode != 0 100 | end 101 | end 102 | 103 | def check_keyring 104 | return unless @keyring 105 | 106 | missing_keys = [] 107 | 108 | (@keyring[:keys] || []).each do |key| 109 | key_path = File.join KeysDir, "#{key}.gpg" 110 | next if File.file? key_path 111 | missing_keys << {:id => key, :path => key_path} 112 | end 113 | 114 | return if missing_keys.empty? 115 | 116 | log.error "Some required keys are missing from your keys directory" 117 | 118 | missing_keys.each do |key| 119 | log.error "Missing key: id: #{key[:id]}, path: #{key[:path]}" 120 | end 121 | 122 | raise StepError.new "Some required keys are missing from the keys directory" 123 | end 124 | 125 | def bootstrap_options 126 | opts = { 127 | :mirror => @bootstrap[:mirror], 128 | :extra => [ 129 | "--variant=minbase", 130 | ] + (@bootstrap[:extra] || []), 131 | } 132 | 133 | unless @transports.empty? 134 | transports = @transports.map{|r, t| "apt-transport-#{t}"} 135 | log.debug "Installing extra transports: #{transports.join ' '}" 136 | opts[:extra] << '--include' << transports.join(',') 137 | end 138 | 139 | if @bootstrap[:keyringfile] 140 | key_path = File.join KeysRingsDir, "#{@bootstrap[:keyringfile]}" 141 | 142 | if File.file? key_path 143 | opts[:extra] << '--keyring' << key_path 144 | else 145 | log.error "Can't find key #{@bootstrap[:keyring]}" 146 | end 147 | end 148 | 149 | opts 150 | end 151 | 152 | def bootstrap_tarball 153 | return if File.file? @bootstrap_status 154 | return unless @bootstrap_tarball 155 | return if File.file? @bootstrap_tarball 156 | 157 | log.debug "Building tarball: #{@bootstrap_tarball}" 158 | 159 | opts = bootstrap_options 160 | opts[:extra] << '--make-tarball' << @bootstrap_tarball 161 | debootstrap @bootstrap[:suite], @target, opts 162 | end 163 | 164 | def bootstrap_install 165 | return if File.file? @bootstrap_status 166 | 167 | log.debug "Early stage bootstrap in #{@target}" 168 | 169 | opts = bootstrap_options 170 | 171 | if @bootstrap_tarball 172 | opts[:extra] << "--unpack-tarball" << @bootstrap_tarball 173 | end 174 | 175 | opts[:extra] << "--foreign" 176 | 177 | debootstrap @bootstrap[:suite], @target, opts 178 | end 179 | 180 | def bootstrap_configure 181 | return if File.file? @bootstrap_status 182 | 183 | log.debug "Late stage bootstrap in #{@target}" 184 | chroot [@target, '/debootstrap/debootstrap', '--second-stage'] 185 | end 186 | 187 | def bootstrap_end 188 | FileUtils.touch @bootstrap_status 189 | end 190 | 191 | def read_file(source_dir, file) 192 | from = file[:from] 193 | to = file[:to] 194 | mode = file[:mode] || 0644 195 | mode = mode.to_i(8) if mode.is_a? String 196 | 197 | files_pattern = File.join source_dir, from 198 | source_files = Dir.glob files_pattern 199 | 200 | return nil if source_files.empty? 201 | 202 | target_to = File.join @target, to 203 | 204 | {:files => source_files, :to => target_to, :mode => mode} 205 | end 206 | 207 | def files_copy 208 | return if @files.empty? 209 | 210 | @_roots.each do |root| 211 | @files.each do |local_root, file| 212 | source_dir = File.join root, FilesDir 213 | file = read_file(source_dir, file) 214 | next if file.nil? 215 | 216 | FileUtils.mkdir_p file[:to] 217 | 218 | file[:files].each do |source| 219 | next unless File.file? source 220 | 221 | target = File.join file[:to], File.basename(source) 222 | 223 | # Skip if target already exists and is identical to source. 224 | next if File.file? target and FileUtils.compare_file source, target 225 | 226 | log.debug "Copying File: #{source} -> #{target}" 227 | 228 | FileUtils.cp source, target 229 | FileUtils.chmod file[:mode], target 230 | end 231 | end 232 | end 233 | end 234 | 235 | def packages_install 236 | return if @packages.empty? 237 | 238 | options = [] 239 | options << 'DPkg::NoTriggers=true' 240 | options << 'PackageManager::Configure=no' 241 | options << 'DPkg::ConfigurePending=false' 242 | options << 'DPkg::TriggersPending=false' 243 | options << 'APT::Install-Recommends=false' 244 | 245 | options = options.map{|option| ['-o', option]}.flatten 246 | 247 | packages = @packages.map{|r, p| p} 248 | 249 | log.debug "Installing Packages" 250 | packages_repr = packages.join ' ' 251 | 252 | log.debug "Installing Packages: #{packages_repr}" 253 | in_apt_get *(options + ['install', '--'] + packages) 254 | end 255 | 256 | def packages_configure 257 | log.debug "Configuring Packages" 258 | in_dpkg '--configure', '-a', '--force-confdef', '--force-confold' 259 | end 260 | 261 | def sources_list(name, sources) 262 | sources_dir = File.join @target, 'etc', 'apt', 'sources.list.d' 263 | sources_list = File.join sources_dir, "#{name}.list" 264 | 265 | log.debug "Writing Sources #{sources_list}" 266 | 267 | File.open(sources_list, 'w', 0644) do |f| 268 | sources.each do |source| 269 | type = source[:type] || DefaultSourceType 270 | components = source[:components] || DefaultComponents 271 | suite = source[:suite] 272 | url = source[:url] 273 | 274 | raise "Missing 'url' in source" unless url 275 | raise "Missing 'suite' in source" unless suite 276 | 277 | f.write "#{type} #{url} #{suite} #{components.join ' '}\n" 278 | end 279 | end 280 | end 281 | 282 | def write_apt_preferences 283 | apt_preferences = File.join @target, 'etc', 'apt', 'preferences' 284 | 285 | return if File.file? apt_preferences 286 | 287 | log.debug "Writing Preferences #{apt_preferences}" 288 | 289 | File.open(apt_preferences, 'w', 0644) do |f| 290 | f.write "# generated by duck\n" 291 | 292 | @preferences.each do |root, pin| 293 | f.write "Package: #{pin[:package]}\n" 294 | f.write "Pin: #{pin[:pin]}\n" 295 | f.write "Pin-Priority: #{pin[:priority]}\n" 296 | f.write "\n" 297 | end 298 | end 299 | end 300 | 301 | def add_apt_keys 302 | log.debug "Adding APT keys" 303 | 304 | (@keyring[:keys] || []).each do |key| 305 | log.debug "Adding key'#{key}'" 306 | key_path = File.join KeysDir, "#{key}.gpg" 307 | 308 | File.open key_path, 'r' do |f| 309 | in_apt_key ['add', '-'], {:input_file => f} 310 | end 311 | end 312 | end 313 | 314 | def prepare_apt 315 | add_apt_keys if @keyring 316 | 317 | sources_list 'main', @sources.map{|r,s| s} 318 | in_apt_get 'update' 319 | write_apt_preferences 320 | end 321 | 322 | def add_policy_rcd 323 | policy_rcd = File.join @target, 'usr', 'sbin', 'policy-rc.d' 324 | 325 | if File.file? policy_rcd 326 | log.debug "Policy OK: #{policy_rcd}" 327 | return 328 | end 329 | 330 | log.debug "Writing Folicy: #{policy_rcd}" 331 | 332 | File.open(policy_rcd, 'w', 0755) do |f| 333 | f.write("#/bin/sh\n") 334 | f.write("exit 101\n") 335 | end 336 | end 337 | 338 | # Remove the policy-rc.d from within the chroot. 339 | def remove_policy_rcd 340 | policy_rcd = File.join @target, 'usr', 'sbin', 'policy-rc.d' 341 | log.debug "Removing Policy: #{policy_rcd}" 342 | FileUtils.rm_f policy_rcd 343 | end 344 | 345 | # Completely disable the specified runlevel. 346 | def disable_runlevel(runlevel) 347 | runlevel_dir = File.join @target, 'etc', "rc#{runlevel}.d" 348 | raise "No such runlevel: #{runlevel}" unless File.directory? runlevel_dir 349 | 350 | Find.find(runlevel_dir) do |path| 351 | name = File.basename path 352 | 353 | if name =~ /^S..(.+)$/ 354 | service = $1 355 | log.debug "Disabling Service '#{service}'" 356 | in_update_rcd '-f', service, 'remove' 357 | end 358 | end 359 | end 360 | 361 | # Make sure that the specified boot service (and only those specified) are 362 | # enabled. 363 | def configure_boot_services 364 | disable_runlevel '2' 365 | disable_runlevel 'S' 366 | 367 | @services.each do |root, service| 368 | args = [service[:name]] 369 | args += ['start'] + service[:start].split(' ') if service[:start] 370 | args += ['stop'] + service[:stop].split(' ') if service[:stop] 371 | in_update_rcd '-f', *args 372 | end 373 | end 374 | 375 | # define all the different steps involved. 376 | step :check_keyring, :disable_hooks => true 377 | step :bootstrap_tarball, :disable_hooks => true 378 | step :bootstrap_install, :disable_hooks => true 379 | step :copy_fixes, :disable_hooks => true 380 | step :bootstrap_configure 381 | step :bootstrap_end 382 | step :add_policy_rcd 383 | step :prepare_apt 384 | step :packages_install 385 | step :packages_configure 386 | step :files_copy 387 | step :configure_boot_services 388 | step :remove_policy_rcd 389 | 390 | def pre_hook(name) 391 | run_fixes "pre-#{name}" 392 | end 393 | 394 | def post_hook(name) 395 | run_fixes "post-#{name}" 396 | end 397 | 398 | def final_hook 399 | run_fixes "final" 400 | clear_fixes 401 | end 402 | end 403 | end 404 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------