diff --git a/builder/Makefile.am b/builder/Makefile.am index 1bcaa4343..1eaa9769f 100644 --- a/builder/Makefile.am +++ b/builder/Makefile.am @@ -57,6 +57,7 @@ OBJECTS = \ $(top_builddir)/mllib/common_gettext.cmx \ $(top_builddir)/mllib/common_utils.cmx \ $(top_builddir)/mllib/random_seed.cmx \ + $(top_builddir)/mllib/hostname.cmx \ $(top_builddir)/mllib/firstboot.cmx \ $(top_builddir)/mllib/crypt-c.o \ $(top_builddir)/mllib/crypt.cmx \ diff --git a/builder/builder.ml b/builder/builder.ml index 9a3b13c1d..56e79fb36 100644 --- a/builder/builder.ml +++ b/builder/builder.ml @@ -41,7 +41,7 @@ let cachedir = let mode, arg, attach, cache, check_signature, curl, debug, fingerprint, firstboot, run, - format, gpg, install, list_long, network, output, + format, gpg, hostname, install, list_long, network, output, password_crypto, quiet, root_password, size, source, upload = let display_version () = @@ -96,6 +96,9 @@ let mode, arg, let format = ref "" in let gpg = ref "gpg" in + let hostname = ref None in + let set_hostname s = hostname := Some s in + let install = ref [] in let add_install pkgs = let pkgs = string_nsplit "," pkgs in @@ -180,6 +183,7 @@ let mode, arg, "--get-kernel", Arg.Unit get_kernel_mode, "image" ^ " " ^ s_"Get kernel from image"; "--gpg", Arg.Set_string gpg, "gpg" ^ " " ^ s_"Set GPG binary/command"; + "--hostname", Arg.String set_hostname, "hostname" ^ " " ^ s_"Set the hostname"; "--install", Arg.String add_install, "pkg,pkg" ^ " " ^ s_"Add package(s) to install"; "-l", Arg.Unit list_mode, " " ^ s_"List available templates"; "--list", Arg.Unit list_mode, ditto; @@ -231,6 +235,7 @@ read the man page virt-builder(1). let run = List.rev !run in let format = match !format with "" -> None | s -> Some s in let gpg = !gpg in + let hostname = !hostname in let install = !install in let list_long = !list_long in let network = !network in @@ -283,7 +288,7 @@ read the man page virt-builder(1). mode, arg, attach, cache, check_signature, curl, debug, fingerprint, firstboot, run, - format, gpg, install, list_long, network, output, + format, gpg, hostname, install, list_long, network, output, password_crypto, quiet, root_password, size, source, upload @@ -571,6 +576,48 @@ let root = (* Set the random seed. *) let () = ignore (Random_seed.set_random_seed g root) +(* Set the hostname. *) +let () = + match hostname with + | None -> () + | Some hostname -> + ignore (Hostname.set_hostname g root hostname) + +(* Root password. + * Note 'None' means that we randomize the root password. + *) +let () = + let make_random_password () = + (* Get random characters from the set [A-Za-z0-9] *) + let chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" in + let nr_chars = String.length chars in + + let chan = open_in "/dev/urandom" in + let buf = String.create 16 in + for i = 0 to 15 do + buf.[i] <- chars.[Char.code (input_char chan) mod nr_chars] + done; + close_in chan; + + msg "Random root password: %s [did you mean to use --root-password?]" buf; + + buf + in + + let root_password = + match root_password with + | Some pw -> pw + | None -> make_random_password () in + + match g#inspect_get_type root with + | "linux" -> + let h = Hashtbl.create 1 in + Hashtbl.replace h "root" root_password; + set_linux_passwords ~prog ?password_crypto g root h + | _ -> + () + (* Useful wrapper for scripts. *) let do_run cmd = (* Add a prologue to the scripts: @@ -628,41 +675,6 @@ let () = do_run cmd; ) -(* Root password. - * Note 'None' means that we randomize the root password. - *) -let () = - let make_random_password () = - (* Get random characters from the set [A-Za-z0-9] *) - let chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" in - let nr_chars = String.length chars in - - let chan = open_in "/dev/urandom" in - let buf = String.create 16 in - for i = 0 to 15 do - buf.[i] <- chars.[Char.code (input_char chan) mod nr_chars] - done; - close_in chan; - - msg "Random root password: %s [did you mean to use --root-password?]" buf; - - buf - in - - let root_password = - match root_password with - | Some pw -> pw - | None -> make_random_password () in - - match g#inspect_get_type root with - | "linux" -> - let h = Hashtbl.create 1 in - Hashtbl.replace h "root" root_password; - set_linux_passwords ~prog ?password_crypto g root h - | _ -> - () - (* Upload files. *) let () = List.iter ( diff --git a/builder/test-virt-builder.sh b/builder/test-virt-builder.sh index 18169e115..6f3f615c0 100755 --- a/builder/test-virt-builder.sh +++ b/builder/test-virt-builder.sh @@ -38,6 +38,7 @@ rm -f phony-fedora.qcow2 ./virt-builder phony-fedora \ --no-cache --no-check-signature \ --size 2G --format qcow2 \ + --hostname test.example.com \ --root-password password:123456 \ --upload Makefile:/Makefile \ --firstboot Makefile --firstboot-command 'echo "hello"' \ diff --git a/builder/virt-builder.pod b/builder/virt-builder.pod index d4314269a..bf9703a05 100644 --- a/builder/virt-builder.pod +++ b/builder/virt-builder.pod @@ -8,8 +8,9 @@ virt-builder - Build virtual machine images quickly virt-builder [-o|--output DISKIMAGE] [--size SIZE] [--format raw|qcow2] [--attach ISOFILE] - [--install PKG,[PKG...]] [--root-password ...] + [--hostname HOSTNAME] + [--install PKG,[PKG...]] [--upload FILE:DEST] [--run SCRIPT] [--run-command 'CMD ARGS ...'] [--firstboot SCRIPT] [--firstboot-command 'CMD ARGS ...'] @@ -94,6 +95,12 @@ a I root password. You can also create user accounts. See L below. +=head2 Set the hostname + + virt-builder fedora-20 --hostname virt.example.com + +Set the hostname to C. + =head2 Installing software To install packages from the ordinary (guest) software repository @@ -276,6 +283,11 @@ alternate home directory: virt-builder --gpg "gpg --homedir /tmp" [...] +=item B<--hostname> HOSTNAME + +Set the hostname of the guest to C. You can use a +dotted hostname.domainname (FQDN) if you want. + =item B<--install> PKG[,PKG,...] Install the named packages (a comma-separated list). These are @@ -595,7 +607,7 @@ A new random seed is generated for the guest. =item * -Packages are installed (I<--install>). +The hostname is set (I<--hostname>). =item * @@ -603,6 +615,10 @@ The root password is changed (I<--root-password>). =item * +Packages are installed (I<--install>). + +=item * + Files are uploaded (I<--upload>). =item *