blog/content/labs/linux/2011-01-02-00001443.md

398 lines
26 KiB
Markdown

---
title: Ubuntu インストールの自動化
author: kazu634
date: 2011-01-02
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5405;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
author:
- kazu634
categories:
- ubuntu
---
<div class="section">
<p>
<a href="http://d.hatena.ne.jp/asin/B004EBH770" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/B004EBH770', 'Software Design');">Software Design</a> で紹介されていたOS構築自動化を試してみました。なお、 <a href="http://d.hatena.ne.jp/asin/B004EBH770" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://d.hatena.ne.jp/asin/B004EBH770', 'Software Design');">Software Design</a> では、 Red Hat Linux の Kickstart の紹介がメインでしたが、ここでは Ubuntu (というかDebian 系)の Preseed の使い方を眺めてみます。ちなみに対象は Ubuntu Server 10.04 です。
</p>
<h4>
OS 構築の自動化
</h4>
<p>
Ubuntu をインストールする際は、言語・キーボードの設定、ネットワークの設定、パーティーションの設定、インストールするパッケージの設定などを行います。
</p>
<p>
<center>
</center>
</p>
<p>
<a href="http://f.hatena.ne.jp/sirocco634/20110102195155" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://f.hatena.ne.jp/sirocco634/20110102195155', '');" class="hatena-fotolife" target="_blank"><img src="http://cdn-ak.f.st-hatena.com/images/fotolife/s/sirocco634/20110102/20110102195155.png" alt="f:id:sirocco634:20110102195155p:image" title="f:id:sirocco634:20110102195155p:image" class="hatena-fotolife" /></a>
</p></p>
<p>
それに対して Preseed を用いた場合、各種設定ファイルを事前に準備することで、対話的な応答を自動応答で済ませることができます。
</p>
<p>
<center>
</center>
</p>
<p>
<a href="http://f.hatena.ne.jp/sirocco634/20110102195156" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://f.hatena.ne.jp/sirocco634/20110102195156', '');" class="hatena-fotolife" target="_blank"><img src="http://cdn-ak.f.st-hatena.com/images/fotolife/s/sirocco634/20110102/20110102195156.png" alt="f:id:sirocco634:20110102195156p:image" title="f:id:sirocco634:20110102195156p:image" class="hatena-fotolife" /></a>
</p></p>
<p>
さらにインストール終了後に、任意のスクリプトを実行することができます。これを行うことで、設定ファイルを書き換えることも可能♪
</p>
<h4>
設定ファイル
</h4>
<p>
設定ファイルは以下のようになります。ちなみに以下の設定ファイルは
</p>
<ul>
<li>
タイムゾーンは「Asia/Tokyo」
</li>
<li>
NTP の設定を行う
</li>
<li>
パーティションは「LVM」で、ガイドに任せて出来る限り LVM として使用する
</li>
<li>
作成するユーザーは「kazu634」、パスワードは「simoom634」
</li>
<li>
SSHサーバを導入する
</li>
<li>
Git, SQLite3 を導入する
</li>
</ul>
<pre class="syntax-highlight">
<span class="synComment">#### Contents of the preconfiguration file (for )</span>
<span class="synComment">### Mirror settings</span>
<span class="synComment"># If you select ftp, the mirror/country string does not need to be set.</span>
<span class="synComment">#d-i mirror/protocol string ftp</span>
d-i mirror/country string manual
d-i mirror/http/hostname string jp.archive.ubuntu.com
d-i mirror/http/directory string /ubuntu/
d-i mirror/http/proxy string
<span class="synComment"># Alternatively: by default, the installer uses CC.archive.ubuntu.com where</span>
<span class="synComment"># CC is the ISO-3166-2 code for the selected country. You can preseed this</span>
<span class="synComment"># so that it does so without asking.</span>
<span class="synComment">#d-i mirror/http/mirror select CC.archive.ubuntu.com</span>
<span class="synComment"># Suite to install.</span>
<span class="synComment">#d-i mirror/suite string </span>
<span class="synComment"># Suite to use for loading installer components (optional).</span>
<span class="synComment">#d-i mirror/udeb/suite string </span>
<span class="synComment"># Components to use for loading installer components (optional).</span>
<span class="synComment">#d-i mirror/udeb/components multiselect main, restricted</span>
<span class="synComment">### Clock and time zone setup</span>
<span class="synComment"># Controls whether or not the hardware clock is set to UTC.</span>
d-i clock-setup/utc boolean true
<span class="synComment"># You may set this to any valid setting for $TZ; see the contents of</span>
<span class="synComment"># /usr/share/zoneinfo/ for valid values.</span>
d-i time/zone string Asia/Tokyo
<span class="synComment"># Controls whether to use NTP to set the clock during the install</span>
d-i clock-setup/ntp boolean true
<span class="synComment"># NTP server to use. The default is almost always fine here.</span>
d-i clock-setup/ntp-server string ntp.ubuntu.com
<span class="synComment">### Partitioning</span>
<span class="synComment"># If the system has free space you can choose to only partition that space.</span>
<span class="synComment"># Alternatives: custom, some_device, some_device_crypto, some_device_lvm.</span>
<span class="synComment">#d-i partman-auto/init_automatically_partition select biggest_free</span>
<span class="synComment"># Alternatively, you can specify a disk to partition. The device name must</span>
<span class="synComment"># be given in traditional non-devfs format.</span>
<span class="synComment"># Note: A disk must be specified, unless the system has only one disk.</span>
<span class="synComment"># For example, to use the first SCSI/SATA hard disk:</span>
d-i partman-auto/disk string /dev/sda
<span class="synComment"># In addition, you'll need to specify the method to use.</span>
<span class="synComment"># The presently available methods are: &#34;regular&#34;, &#34;lvm&#34; and &#34;crypto&#34;</span>
d-i partman-auto/method string lvm
<span class="synComment"># If one of the disks that are going to be automatically partitioned</span>
<span class="synComment"># contains an old LVM configuration, the user will normally receive a</span>
<span class="synComment"># warning. This can be preseeded away...</span>
d-i partman-lvm/device_remove_lvm boolean true
<span class="synComment"># The same applies to pre-existing software RAID array:</span>
d-i partman-md/device_remove_md boolean true
<span class="synComment"># And the same goes for the confirmation to write the lvm partitions.</span>
d-i partman-lvm/confirm boolean true
<span class="synComment"># For LVM partitioning, you can select how much of the volume group to use</span>
<span class="synComment"># for logical volumes.</span>
d-i partman-auto-lvm/guided_size string max
<span class="synComment">#d-i partman-auto-lvm/guided_size string 10GB</span>
<span class="synComment">#d-i partman-auto-lvm/guided_size string 50%</span>
<span class="synComment"># You can choose one of the three predefined partitioning recipes:</span>
<span class="synComment"># - atomic: all files in one partition</span>
<span class="synComment"># - home: separate /home partition</span>
<span class="synComment"># - multi: separate /home, /usr, /var, and /tmp partitions</span>
d-i partman-auto/choose_recipe select atomic
<span class="synComment"># Or provide a recipe of your own...</span>
<span class="synComment"># The recipe format is documented in the file devel/partman-auto-recipe.txt.</span>
<span class="synComment"># If you have a way to get a recipe file into the d-i environment, you can</span>
<span class="synComment"># just point at it.</span>
<span class="synComment">#d-i partman-auto/expert_recipe_file string /hd-media/recipe</span>
<span class="synComment"># If not, you can put an entire recipe into the preconfiguration file in one</span>
<span class="synComment"># (logical) line. This example creates a small /boot partition, suitable</span>
<span class="synComment"># swap, and uses the rest of the space for the root partition:</span>
<span class="synComment">#d-i partman-auto/expert_recipe string \</span>
<span class="synComment"># boot-root :: \</span>
<span class="synComment"># 40 50 100 ext3 \</span>
<span class="synComment"># $primary{ } $bootable{ } \</span>
<span class="synComment"># method{ format } format{ } \</span>
<span class="synComment"># use_filesystem{ } filesystem{ ext3 } \</span>
<span class="synComment"># mountpoint{ /boot } \</span>
<span class="synComment"># . \</span>
<span class="synComment"># 500 10000 1000000000 ext3 \</span>
<span class="synComment"># method{ format } format{ } \</span>
<span class="synComment"># use_filesystem{ } filesystem{ ext3 } \</span>
<span class="synComment"># mountpoint{ / } \</span>
<span class="synComment"># . \</span>
<span class="synComment"># 64 512 300% linux-swap \</span>
<span class="synComment"># method{ swap } format{ } \</span>
<span class="synComment"># .</span>
<span class="synComment"># If you just want to change the default filesystem from ext3 to something</span>
<span class="synComment"># else, you can do that without providing a full recipe.</span>
<span class="synComment">#d-i partman/default_filesystem string ext4</span>
<span class="synComment"># This makes partman automatically partition without confirmation, provided</span>
<span class="synComment"># that you told it what to do using one of the methods above.</span>
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
<span class="synComment">### Controlling how partitions are mounted</span>
<span class="synComment"># The default is to mount by UUID, but you can also choose &#34;traditional&#34; to</span>
<span class="synComment"># use traditional device names, or &#34;label&#34; to try filesystem labels before</span>
<span class="synComment"># falling back to UUIDs.</span>
<span class="synComment">#d-i partman/mount_style select uuid</span>
<span class="synComment">### Base system installation</span>
<span class="synComment"># The kernel image (meta) package to be installed; &#34;none&#34; can be used if no</span>
<span class="synComment"># kernel is to be installed.</span>
d-i base-installer/kernel/image string linux-server
<span class="synComment">### Account setup</span>
<span class="synComment"># Skip creation of a root account (normal user account will be able to</span>
<span class="synComment"># use sudo). The default is false; preseed this to true if you want to set</span>
<span class="synComment"># a root password.</span>
<span class="synComment">#d-i passwd/root-login boolean false</span>
<span class="synComment"># Alternatively, to skip creation of a normal user account.</span>
<span class="synComment">#d-i passwd/make-user boolean false</span>
<span class="synComment"># Root password, either in clear text</span>
<span class="synComment">#d-i passwd/root-password password r00tme</span>
<span class="synComment">#d-i passwd/root-password-again password r00tme</span>
<span class="synComment"># or encrypted using an MD5 hash.</span>
<span class="synComment">#d-i passwd/root-password-crypted password [MD5 hash]</span>
<span class="synComment"># To create a normal user account.</span>
d-i passwd/user-fullname string Kazuhiro MUSASHI
d-i passwd/username string kazu634
<span class="synComment"># Normal user's password, either in clear text</span>
d-i passwd/user-password password simoom634
d-i passwd/user-password-again password simoom634
<span class="synComment"># or encrypted using an MD5 hash.</span>
<span class="synComment">#d-i passwd/user-password-crypted password [MD5 hash]</span>
<span class="synComment"># Create the first user with the specified UID instead of the default.</span>
<span class="synComment">#d-i passwd/user-uid string 1010</span>
<span class="synComment"># The installer will warn about weak passwords. If you are sure you know</span>
<span class="synComment"># what you're doing and want to override it, uncomment this.</span>
d-i user-setup/allow-password-weak boolean true
<span class="synComment"># The user account will be added to some standard initial groups. To</span>
<span class="synComment"># override that, use this.</span>
<span class="synComment">#d-i passwd/user-default-groups string audio cdrom video</span>
<span class="synComment"># Set to true if you want to encrypt the first user's home directory.</span>
d-i user-setup/encrypt-home boolean false
<span class="synComment">### Apt setup</span>
<span class="synComment"># You can choose to install restricted and universe software, or to install</span>
<span class="synComment"># software from the backports repository.</span>
<span class="synComment">#d-i apt-setup/restricted boolean true</span>
<span class="synComment">#d-i apt-setup/universe boolean true</span>
<span class="synComment">#d-i apt-setup/backports boolean true</span>
<span class="synComment"># Uncomment this if you don't want to use a network mirror.</span>
<span class="synComment">#d-i apt-setup/use_mirror boolean false</span>
<span class="synComment"># Select which update services to use; define the mirrors to be used.</span>
<span class="synComment"># Values shown below are the normal defaults.</span>
<span class="synComment">#d-i apt-setup/services-select multiselect security</span>
<span class="synComment">#d-i apt-setup/security_host string security.ubuntu.com</span>
<span class="synComment">#d-i apt-setup/security_path string /ubuntu</span>
<span class="synComment"># Additional repositories, local[0-9] available</span>
<span class="synComment">#d-i apt-setup/local0/repository string \</span>
<span class="synComment"># http://local.server/ubuntu main</span>
<span class="synComment">#d-i apt-setup/local0/comment string local server</span>
<span class="synComment"># Enable deb-src lines</span>
<span class="synComment">#d-i apt-setup/local0/source boolean true</span>
<span class="synComment"># URL to the public key of the local repository; you must provide a key or</span>
<span class="synComment"># apt will complain about the unauthenticated repository and so the</span>
<span class="synComment"># sources.list line will be left commented out</span>
<span class="synComment">#d-i apt-setup/local0/key string http://local.server/key</span>
<span class="synComment"># By default the installer requires that repositories be authenticated</span>
<span class="synComment"># using a known gpg key. This setting can be used to disable that</span>
<span class="synComment"># authentication. Warning: Insecure, not recommended.</span>
<span class="synComment">#d-i debian-installer/allow_unauthenticated string true</span>
<span class="synComment">### Package selection</span>
tasksel tasksel/first multiselect standard, OpenSSH server
<span class="synComment">#tasksel tasksel/first multiselect lamp-server, print-server</span>
<span class="synComment">#tasksel tasksel/first multiselect kubuntu-desktop</span>
<span class="synComment"># Individual additional packages to install</span>
d-i pkgsel/include string build-essential, git-core, unzip, pkg-config, sqlite3, libsqlite3-dev
<span class="synComment"># Whether to upgrade packages after debootstrap.</span>
<span class="synComment"># Allowed values: none, safe-upgrade, full-upgrade</span>
d-i pkgsel/upgrade select safe-upgrade
<span class="synComment"># Language pack selection</span>
d-i pkgsel/language-packs multiselect
<span class="synComment"># Policy for applying updates. May be &#34;none&#34; (no automatic updates),</span>
<span class="synComment"># &#34;unattended-upgrades&#34; (install security updates automatically), or</span>
<span class="synComment"># &#34;landscape&#34; (manage system with Landscape).</span>
d-i pkgsel/update-policy select none
<span class="synComment"># Some versions of the installer can report back on what software you have</span>
<span class="synComment"># installed, and what software you use. The default is not to report back,</span>
<span class="synComment"># but sending reports helps the project determine what software is most</span>
<span class="synComment"># popular and include it on CDs.</span>
<span class="synComment">#popularity-contest popularity-contest/participate boolean false</span>
<span class="synComment"># By default, the system's locate database will be updated after the</span>
<span class="synComment"># installer has finished installing most packages. This may take a while, so</span>
<span class="synComment"># if you don't want it, you can set this to &#34;false&#34; to turn it off.</span>
d-i pkgsel/updatedb boolean false
<span class="synComment">### Boot loader installation</span>
<span class="synComment"># Grub is the default boot loader (for x86). If you want lilo installed</span>
<span class="synComment"># instead, uncomment this:</span>
<span class="synComment">#d-i grub-installer/skip boolean true</span>
<span class="synComment"># To also skip installing lilo, and install no bootloader, uncomment this</span>
<span class="synComment"># too:</span>
<span class="synComment">#d-i lilo-installer/skip boolean true</span>
<span class="synComment"># This is fairly safe to set, it makes grub install automatically to the MBR</span>
<span class="synComment"># if no other operating system is detected on the machine.</span>
d-i grub-installer/only_debian boolean true
<span class="synComment"># This one makes grub-installer install to the MBR if it also finds some other</span>
<span class="synComment"># OS, which is less safe as it might not be able to boot that other OS.</span>
d-i grub-installer/with_other_os boolean true
<span class="synComment"># Alternatively, if you want to install to a location other than the mbr,</span>
<span class="synComment"># uncomment and edit these lines:</span>
<span class="synComment">#d-i grub-installer/only_debian boolean false</span>
<span class="synComment">#d-i grub-installer/with_other_os boolean false</span>
<span class="synComment">#d-i grub-installer/bootdev string (hd0,0)</span>
<span class="synComment"># To install grub to multiple disks:</span>
<span class="synComment">#d-i grub-installer/bootdev string (hd0,0) (hd1,0) (hd2,0)</span>
<span class="synComment"># Optional password for grub, either in clear text</span>
<span class="synComment">#d-i grub-installer/password password r00tme</span>
<span class="synComment">#d-i grub-installer/password-again password r00tme</span>
<span class="synComment"># or encrypted using an MD5 hash, see grub-md5-crypt(8).</span>
<span class="synComment">#d-i grub-installer/password-crypted password [MD5 hash]</span>
<span class="synComment">### Finishing up the installation</span>
<span class="synComment"># During installations from serial console, the regular virtual consoles</span>
<span class="synComment"># (VT1-VT6) are normally disabled in /etc/inittab. Uncomment the next</span>
<span class="synComment"># line to prevent this.</span>
<span class="synComment">#d-i finish-install/keep-consoles boolean true</span>
<span class="synComment"># Avoid that last message about the install being complete.</span>
d-i finish-install/reboot_in_progress note
<span class="synComment"># This will prevent the installer from ejecting the CD during the reboot,</span>
<span class="synComment"># which is useful in some situations.</span>
<span class="synComment">#d-i cdrom-detect/eject boolean false</span>
<span class="synComment"># This is how to make the installer shutdown when finished, but not</span>
<span class="synComment"># reboot into the installed system.</span>
<span class="synComment">#d-i debian-installer/exit/halt boolean true</span>
<span class="synComment"># This will power off the machine instead of just halting it.</span>
<span class="synComment">#d-i debian-installer/exit/poweroff boolean true</span>
<span class="synComment">### X configuration</span>
<span class="synComment"># X can detect the right driver for some cards, but if you're preseeding,</span>
<span class="synComment"># you override whatever it chooses. Still, vesa will work most places.</span>
<span class="synComment">#xserver-xorg xserver-xorg/config/device/driver select vesa</span>
<span class="synComment"># A caveat with mouse autodetection is that if it fails, X will retry it</span>
<span class="synComment"># over and over. So if it's preseeded to be done, there is a possibility of</span>
<span class="synComment"># an infinite loop if the mouse is not autodetected.</span>
<span class="synComment">#xserver-xorg xserver-xorg/autodetect_mouse boolean true</span>
<span class="synComment"># Monitor autodetection is recommended.</span>
<span class="synComment"># xserver-xorg xserver-xorg/autodetect_monitor boolean true</span>
<span class="synComment"># Uncomment if you have an LCD display.</span>
<span class="synComment">#xserver-xorg xserver-xorg/config/monitor/lcd boolean true</span>
<span class="synComment"># X has three configuration paths for the monitor. Here's how to preseed</span>
<span class="synComment"># the &#34;medium&#34; path, which is always available. The &#34;simple&#34; path may not</span>
<span class="synComment"># be available, and the &#34;advanced&#34; path asks too many questions.</span>
<span class="synComment"># xserver-xorg xserver-xorg/config/monitor/selection-method \</span>
<span class="synComment"># select medium</span>
<span class="synComment"># xserver-xorg xserver-xorg/config/monitor/mode-list \</span>
<span class="synComment"># select 1024x768 @ 60 Hz</span>
<span class="synComment">### Preseeding other packages</span>
<span class="synComment"># Depending on what software you choose to install, or if things go wrong</span>
<span class="synComment"># during the installation process, it's possible that other questions may</span>
<span class="synComment"># be asked. You can preseed those too, of course. To get a list of every</span>
<span class="synComment"># possible question that could be asked during an install, do an</span>
<span class="synComment"># installation, and then run these commands:</span>
<span class="synComment"># debconf-get-selections --installer &#62; file</span>
<span class="synComment"># debconf-get-selections &#62;&#62; file</span>
<span class="synComment">#### Advanced options</span>
<span class="synComment">### Running custom commands during the installation</span>
<span class="synComment"># d-i preseeding is inherently not secure. Nothing in the installer checks</span>
<span class="synComment"># for attempts at buffer overflows or other exploits of the values of a</span>
<span class="synComment"># preconfiguration file like this one. Only use preconfiguration files from</span>
<span class="synComment"># trusted locations! To drive that home, and because it's generally useful,</span>
<span class="synComment"># here's a way to run any shell command you'd like inside the installer,</span>
<span class="synComment"># automatically.</span>
<span class="synComment"># This first command is run as early as possible, just after</span>
<span class="synComment"># preseeding is read.</span>
<span class="synComment">#d-i preseed/early_command string anna-install some-udeb</span>
<span class="synComment"># This command is run immediately before the partitioner starts. It may be</span>
<span class="synComment"># useful to apply dynamic partitioner preseeding that depends on the state</span>
<span class="synComment"># of the disks (which may not be visible when preseed/early_command runs).</span>
<span class="synComment">#d-i partman/early_command string debconf-set partman-auto/disk &#34;$(list-devices disk | head -n1)&#34;</span>
<span class="synComment"># This command is run just before the install finishes, but when there is</span>
<span class="synComment"># still a usable /target directory. You can chroot to /target and use it</span>
<span class="synComment"># directly, or use the apt-install and in-target commands to easily install</span>
<span class="synComment"># packages and run commands in the target system.</span>
d-i preseed/late_command string wget http://www3232u.sakura.ne.jp/preseed/preseed.sh -O /target/tmp/preseed.sh; in-target chmod +x /tmp/preseed.sh; in-target /tmp/preseed.sh
</pre>
<h4>
インストーラーに設定ファイルを指定する
</h4>
<p>
下の画面で F6 キーを押して、
</p>
<p>
<center>
</center>
</p>
<p>
<a href="http://f.hatena.ne.jp/sirocco634/20110102175435" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://f.hatena.ne.jp/sirocco634/20110102175435', '');" class="hatena-fotolife" target="_blank"><img src="http://cdn-ak.f.st-hatena.com/images/fotolife/s/sirocco634/20110102/20110102175435.jpg" alt="f:id:sirocco634:20110102175435j:image" title="f:id:sirocco634:20110102175435j:image" class="hatena-fotolife" /></a>
</p></p>
<blockquote>
<p>
FILE=&#8230;
</p>
</blockquote>
<p>
の部分を
</p>
<blockquote>
<p>
url=http://設定ファイルの在り処
</p>
</blockquote>
<p>
に書き換えます。例えば:
</p>
<p>
<center>
</center>
</p>
<p>
<a href="http://f.hatena.ne.jp/sirocco634/20110102175434" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://f.hatena.ne.jp/sirocco634/20110102175434', '');" class="hatena-fotolife" target="_blank"><img src="http://cdn-ak.f.st-hatena.com/images/fotolife/s/sirocco634/20110102/20110102175434.jpg" alt="f:id:sirocco634:20110102175434j:image" title="f:id:sirocco634:20110102175434j:image" class="hatena-fotolife" /></a>
</p></p>
</div>