FreeBSD をゼロから設定するには

USBメモリが大容量化し、最大容量2TBに対応したCF SSDアダプタなんて製品が出てきた今(July 2007)、USBメモリから起動するFreeBSDについてGoogleで調べていたらFreeBSD をゼロから設定するにはがヒットした。

FreeBSD From Scratch on 6.2

sliceとbsdlabel

ベースとなるシステムは、ATA200GBのディスクに80GBのスライスを作成してインストールした。

SilverStone> df
Filesystem  1K-blocks    Used    Avail Capacity  Mounted on
/dev/ad0s1a    507630   66646   400374    14%    /
devfs               1       1        0   100%    /dev
/dev/ad0s1e    507630     692   466328     0%    /tmp
/dev/ad0s1f  77726566 3311050 68197392     5%    /usr
/dev/ad0s1d   1505166   27332  1357422     2%    /var

残りの120GBのディスクのうち、20GBの新しいスライスを作成した。 同じディスク上なので起動中はFdiskをできない。 なので、FreeBSD6.1(とりあえず)のBoot onlyディスクで起動し、sysinstall > Configuration > FDiskで新しいスライスを作成

この状態でdfしてもマウントしていないので特に何も表示されない。 /devを見ると

ad0s2
ad0s2c

が追加されているのみ。

ステップ1を実行する前にstage_1.conf.defaultを修正する。 サンプルでは、TIMEZONEがEurope/Berlinとなっているので、Asia/Tokyoに変更する。

TIMEZONE="Europe/Berlin"
   ↓
TIMEZONE="Asia/Tokyo"

create_file_systems ()でデバイスを修正する。

ステージ1

ステージ1のシェルスクリプトを実行する。

# ./stage_1.sh default

ファイルシステムを作成し、ファイルのコピーが実行される。
エラーがなければ終わり。

<<実際に使用したスクリプトと設定ファイルを記述すること>>

さて、このあとステージ2、ステージ3へ進みportsのインストール、インストールしたアプリケーションの自動カスタマイズを実施していく。のだが、ここでFFSで作った環境で起動してみる。
というのは、ステージ1の終了後、ステージ2のコンフィグファイルを編集し必要なportsの記述を行いステージ2のスクリプトを起動した。

# ./stage_2.sh default

各portsのmakeとinstallが始まり動作状況を見ていると、どうやら元のディスクにportsのインストールを行っているようである。
なので途中で止めて新環境でブートしてみた。

ブートは、sysinstallのfdisk時にbootmgrをインストールしたので、F2で起動した。

起動後は、元の環境のrootでログインし/usr/bin /usr/sbinにもそれなりのツールがそろっていて普通に使える。

実験はここまで。

USBメモリへのインストール [2007-10-02]

実験からすぐにUSBにインストールしたんだけど、やり方を忘れてしまった(爆)
ささっ気を取り直してやってみよ

USBメモリにスライスを作成

ベースとなるマシンにUSBメモリを挿してみる。

BASE > umass0: I-O DATA USB Flash Disk, rev 2.0/bb.03, add 2
da0 at umass-sim0 bus 0 target 0 lun 0
da0: <I-O DATA USB Flash Disk BB03> Removable Direct Access SCSI-0 device
da0: 1.000MB/s transfers
da0: 1943MB (3980288 512 byte sectors: 255H 63S/T 247C)

とりあえずSCSIデバイスとして認識されているようです。
使ったUSBメモリは、IOデータのUSB 2.0/1.1対応 フラッシュメモリー「ToteBag」Bシリーズ。2Gの物を使いました。最初のは1Gだった。

ここからはrootで作業。ベースシステムを消さないように!

BASE # sysinstall

Configure > Fdisk

Select Drive(s)
メッセージ達

Use [TAB] to get to the buttons and leave this menu.
    [ ] ad0 ad0
    [ ] da0 da0

da0をスペースで選択するとFdisk Partition Editorのおなじみの画面が表示される。
最初からあるFAT32をD = Delete Slice で削除し、C = Create Sliceで全部を165のFreeBSDスライスとして確保する。
W = Write Changesでライトし、Boot ManagerはとりあえずStandardを選択しておく。
QでFdiskを終了し、Selece Drive(s)もCancelで抜けていく。

LabelでDisklabelの作成

sysinstall > Configure > Labelでda0を選択しAでAutoとしたいところだけど、もともと容量が少ないので分けずに単一にする。
C = Createを押して全部を割り当てる。
マウントポイントは/
W = Write でライトしQで終了
とするとsysinstallを終了すると何もできなくなってしまうんだな。
何ででしょ。
とりあえず電源ボタンを押して(軽くね)ACPIでシャットダウンしてる。(本当は何かがちがうんだろうな)

はい、違いました。 角川様からご指摘いただきました。 何もできなくなってしまうのは、

vvvご指摘内容vvv
disklabel書いた後に/に自動的にマウントされてしまうからです。

sysinstallの時は/mntに一時的にマウントしておいて、手で/mnt/etc/fstabを
書き換えるのが正しい手順かと思います。 
^^^ここまで^^^

なるほど。確かに参考にした「FreeBSDをゼロから設定するには」では/newrootという場所にインストールを行ってfstabを書き換えている。

検証してからここに記載しようと思っていたけど、しばらく時間が取れそうにないのでご指摘いただいた内容のみ記載。 角川様ありがとうございました。

FreeBSD from Scratch (改)のスクリプトを走らせる!

もう一度ベースシステムがブートしたら、FreeBSD from Scratch (改)のスクリプトを走らせる。

BASE # ./stage_1.sh default

実際に使ったスクリプトとコンフィグレーションファイルはそれなりに長いので最後に紹介することにする。

USBで起動する

スクリプト中でfstabの設定が/dev/da0s2aとなっているのでこれをエディットし/dev/da0s1aに変更する。なんで/dev/da0s2aにしてたかはなぞ。
まだrootのはずなので、そのままリブートしUSBメモリから起動すれば作業はお終い。
あっBIOSでHDDよりUSB HDDが優先になっていないと新しいシステムは立ち上がりまへん。念のため。

起動後の状態

USB BOOT > df
Filesystem  1K-blocks   Used   Avail Capacity Mounted on
/dev/da0s1a   1951242 176736 1618408    10%   /
devfs               1      1       0   100%   /dev

スクリプトとコンフィグレーションファイル

添付しておくけどさらしておく

スクリプト

#!/bin/sh
#
# stage_1.sh - FreeBSD From Scratch, Stage 1: System Installation.
#              Usage: ./stage_1.sh profile
#              will read ./stage_1.conf.profile
#              and write ./stage_1.log.profile
#
# Author:      Jens Schweikhardt
# $Id: stage_1.sh,v 1.7 2004/01/03 13:50:41 toor Exp toor $
# $FreeBSD: doc/en_US.ISO8859-1/articles/fbsd-from-scratch/stage_1.sh,v 1.5 2004/07/19 21:02:26 schweikh Exp $

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# Prerequisites:
#
# a) Successfully completed "make buildworld" and "make buildkernel"
# b) Unused partitions (at least one for the root fs, probably more for
#    the new /usr and /var, to your liking.)
# c) A customized stage_1.conf.profile file.

if test $# -ne 1; then
  echo "usage: stage_1.sh profile" 1>&2
  exit 1
fi

# ---------------------------------------------------------------------------- #
# Step 1: Create an empty directory tree below $DESTDIR.
# ---------------------------------------------------------------------------- #

step_one () {
  create_file_systems
  # Now create all the other directories. Mandatory.
  cd ${SRC}/etc; make distrib-dirs DESTDIR=${DESTDIR}
}

# ---------------------------------------------------------------------------- #
# Step 2: Fill the empty /etc directory tree and put a few files in /.
# ---------------------------------------------------------------------------- #

step_two () {
  copy_files

  # Delete mergemaster's temproot, if any.
  TEMPROOT=/var/tmp/temproot.stage1
  if test -d ${TEMPROOT}; then
    chflags -R 0 ${TEMPROOT}
    rm -rf ${TEMPROOT}
  fi
  export MAKEDEVPATH="/bin:/sbin:/usr/bin"
  mergemaster -i -m ${SRC}/etc -t ${TEMPROOT} -D ${DESTDIR}
  cap_mkdb ${DESTDIR}/etc/login.conf
  pwd_mkdb -d ${DESTDIR}/etc -p ${DESTDIR}/etc/master.passwd

  # Mergemaster does not create empty files, e.g. in /var/log. Do so now,
  # but do not clobber files that may have been copied with copy_files.
  cd ${TEMPROOT}
  find . -type f | sed 's,^\./,,' |
  while read f; do
    if test -r ${DESTDIR}/${f}; then
      echo "${DESTDIR}/${f} already exists; not copied"
    else
      echo "Creating empty ${DESTDIR}/${f}"
      cp -p ${f} ${DESTDIR}/${f}
    fi
  done
  chflags -R 0 ${TEMPROOT}
  rm -rf ${TEMPROOT}
}

# ---------------------------------------------------------------------------- #
# Step 3: Install world.
# ---------------------------------------------------------------------------- #

step_three () {
  cd ${SRC}
  make installworld DESTDIR=${DESTDIR}
  # Install additional compatibility libraries (optional). Use this if you
  # have programs dynamically linked against libc.so.4, i.e. if you see
  # /usr/libexec/ld-elf.so.1: Shared object "libc.so.4" not found
  # cd lib/compat/compat4x.i386
  # make all install DESTDIR=${DESTDIR}
}

# ---------------------------------------------------------------------------- #
# Step 4: Install kernel and modules.
# ---------------------------------------------------------------------------- #

step_four () {
  cd ${SRC}
  # The loader.conf and device.hints are required by the installkernel target.
  # If you have not copied them in Step 2, cp them as shown in the next 2 lines.
  #   cp sys/boot/forth/loader.conf ${DESTDIR}/boot/defaults
  #   cp sys/i386/conf/GENERIC.hints ${DESTDIR}/boot/device.hints
  make installkernel DESTDIR=${DESTDIR} KERNCONF=${KERNCONF}
}

# ---------------------------------------------------------------------------- #
# Step 5: Install /etc/fstab and time zone info.
# ---------------------------------------------------------------------------- #

step_five () {
  create_etc_fstab

  # Setup time zone info; pretty much mandatory.
  cp ${DESTDIR}/usr/share/zoneinfo/${TIMEZONE} ${DESTDIR}/etc/localtime
  if test -r /etc/wall_cmos_clock; then
    cp -p /etc/wall_cmos_clock ${DESTDIR}/etc/wall_cmos_clock
  fi
}

# ---------------------------------------------------------------------------- #
# Step 6: All remaining customization.
# ---------------------------------------------------------------------------- #

step_six () {
  all_remaining_customization
}

do_steps () {
  echo "PROFILE=${PROFILE}"
  echo "DESTDIR=${DESTDIR}"
  echo "SRC=${SRC}"
  echo "KERNCONF=${KERNCONF}"
  echo "TIMEZONE=${TIMEZONE}"
  echo "TYPE=${TYPE}"
  echo "REVISION=${REVISION}"
  echo "BRANCH=${BRANCH}"
  echo "RELDATE=${RELDATE}"
  step_one
  step_two
  step_three
  step_four
  step_five
  step_six
}

# ---------------------------------------------------------------------------- #
# The ball starts rolling here.
# ---------------------------------------------------------------------------- #

PROFILE="$1"
set -x -e -u # Stop for any error or use of an undefined variable.
. ./stage_1.conf.${PROFILE}

# Determine a few variables from the sources that were used to make the
# world. The variables can be used to modify actions, e.g. depending on
# whether we install a 4.x or 5.x system. The __FreeBSD_version numbers
# for RELDATE are documented in the Porter's Handbook,
# doc/en_US.ISO8859-1/books/porters-handbook/freebsd-versions.html.
# Scheme is:  <major><two digit minor><0 if release branch, otherwise 1>xx
# The result will be something like
#
#   TYPE="FreeBSD"
#   REVISION="4.9"
#   BRANCH="RC"      { "CURRENT", "STABLE", "RELEASE" }
#   RELDATE="502101"
#
eval $(awk '/^(TYPE|REVISION|BRANCH)=/' ${SRC}/sys/conf/newvers.sh)
RELDATE=$(awk '/^[ \t]*#[ \t]*define[ \t][ \t]*__FreeBSD_version[ \t]/ {
                print $3
              }' ${SRC}/sys/sys/param.h)

echo "=> Logging to stage_1.log.${PROFILE}"
do_steps 2>&1 | tee stage_1.log.${PROFILE}

# vim: tabstop=2:expandtab:shiftwidth=2:
# EOF $RCSfile: stage_1.sh,v $

コンフィグレーションファイル

# This file: stage_1.conf.default, sourced by stage_1.sh.
#
# $Id: stage_1.conf.default,v 1.2 2004/01/03 13:55:06 toor Exp toor $
# $FreeBSD: doc/en_US.ISO8859-1/articles/fbsd-from-scratch/stage_1.conf.default,v 1.3 2004/07/19 21:02:26 schweikh Exp $

# Root mount point where you create the new system. Because it is only
# used as a mount point, no space will be used on that file system as all
# files are of course written to the mounted file system(s).
DESTDIR="/newroot"

# Where your src tree is.
SRC="/usr/src"

# Your kernel config name as from make buildkernel KERNCONF=...
#KERNCONF="HAL9000"
KERNCONF="GENERIC"

# Available time zones are those under /usr/share/zoneinfo.
TIMEZONE="Asia/Tokyo"

#
# The create_file_systems function must create the mountpoints under
# DESTDIR, create the file systems, and then mount them under DESTDIR.
#
create_file_systems () {
  # The new root file system. Mandatory.
  # Change DEVICE names or risk foot shooting.
  # You must use newfs -O 1 for the root fs if you want to boot it from grub.
  DEVICE=/dev/da0s1a
  mkdir -m 755 -p ${DESTDIR}
  chown root:wheel ${DESTDIR}
  newfs -U -O 1 ${DEVICE}
  mount -o noatime ${DEVICE} ${DESTDIR}

  # Additional file systems and initial mount points. Optional.
  ## DEVICE=/dev/ad0s2d
  ## mkdir -m 755 -p ${DESTDIR}/var
  ## chown root:wheel ${DESTDIR}/var
  ## newfs -U ${DEVICE}
  ## mount -o noatime ${DEVICE} ${DESTDIR}/var

  ## DEVICE=/dev/ad0s2e
  ## mkdir -m 755 -p ${DESTDIR}/tmp
  ## chown root:wheel ${DESTDIR}/tmp
  ## newfs -U ${DEVICE}
  ## mount -o noatime ${DEVICE} ${DESTDIR}/tmp

  ## DEVICE=/dev/ad0s2f
  ## mkdir -m 755 -p ${DESTDIR}/usr
  ## chown root:wheel ${DESTDIR}/usr
  ## newfs -U ${DEVICE}
  ## mount -o noatime ${DEVICE} ${DESTDIR}/usr
}

#
# The create_etc_fstab function must create an fstab matching the
# file systems created in create_file_systems.
#
create_etc_fstab () {
  cat <<EOF >${DESTDIR}/etc/fstab
# Device         Mountpoint          FStype    Options              Dump Pass#
/dev/da0s2b      none                swap      sw                   0    0
/dev/da0s2a      /                   ufs       rw,noatime           1    1
##/dev/ad0s2d      /var                ufs       rw,noatime           1    1
##/dev/ad0s2e      /tmp                ufs       rw,noatime           1    1
##/dev/ad0s2f      /usr                ufs       rw,noatime           1    1
##/dev/vinum/Share /share              ufs       rw,noatime           0    2
##/dev/vinum/home  /home               ufs       rw,noatime           0    2
##/dev/vinum/ncvs  /home/ncvs          ufs       rw,noatime           0    2
##/dev/vinum/ports /usr/ports          ufs       rw,noatime           0    2
#
#/dev/cd0         /cdrom              cd9660    ro,noauto            0    0
EOF
  chmod 644 ${DESTDIR}/etc/fstab
  chown root:wheel ${DESTDIR}/etc/fstab
}

#
# The copy_files function is used to copy files before mergemaster is run.
#
copy_files () {
  # Add or remove from this list at your discretion. Mostly mandatory.
  for f in \
    /.profile \
    /etc/group \
    /etc/hosts \
    /etc/inetd.conf \
    /etc/make.conf \
    /etc/master.passwd \
    /etc/nsswitch.conf \
    /etc/printcap \
    /etc/profile \
    /etc/rc.conf \
    /etc/resolv.conf \
    /etc/ttys \
    /etc/ppp/* \
    /etc/mail/aliases \
    /etc/mail/aliases.db \
    /etc/ssh/*key* \
    /etc/ssh/*_config \
    /etc/X11/xorg.conf \
    /root/.profile \
    /boot/loader.conf \
    /boot/device.hints ; do
    cp -p ${f} ${DESTDIR}${f}
  done
}

# remove from default config
#    /etc/ipfw.conf \
#    /etc/ntp.conf \
#    /etc/start_if.xl0 \
#    /etc/mail/hal9000.mc \
#    /etc/mail/service.switch \
#    /etc/X11/XF86Config-4 \
#    /var/files \
#    /boot/*.bmp \
#    /var/cron/tabs/* \
#

#
# Everything else you want to tune in the new system.
# NOTE: Do not install too many binaries here. With the old system running and
# the new binaries and headers installed you are likely to run into bootstrap
# problems. Ports should be compiled after you have booted in the new system.
#
all_remaining_customization () {
  # Without the compat symlink the linux_base files end up on the root fs:
  cd ${DESTDIR}
  mkdir -m 755 usr/compat; chown root:wheel usr/compat; ln -s usr/compat
  mkdir -m 755 usr/compat/linux;      chown root:wheel usr/compat/linux
  mkdir -m 755 src;                   chown root:wheel src
  mkdir -m 755 share;                 chown root:wheel share
  mkdir -m 755 dvd cdrom flash;       chown root:wheel dvd cdrom flash
  mkdir -m 755 home;                  chown root:wheel home
  mkdir -m 755 usr/ports;             chown root:wheel usr/ports

  # My personal preference is to symlink tmp -> var/tmp. Optional.
  ##cd ${DESTDIR}; rmdir tmp; ln -s var/tmp

  # Make spooldirs for the printers in my /etc/printcap.
  cd ${DESTDIR}/var/spool/output/lpd; mkdir -p as od ev te lp da
  touch ${DESTDIR}/var/log/lpd-errs

  # If you do not have /home on a shared partition, you may want to copy it:
  # mkdir -p ${DESTDIR}/home
  # cd /home; tar cf - . | (cd ${DESTDIR}/home; tar xpvf -)

  case ${REVISION} in
  4.*)
    # 4.x without devfs: create non-standard devices to match your hardware.
    cd ${DESTDIR}/dev
    ./MAKEDEV all
    ./MAKEDEV da0 da0s1h da0s2h da0s3h da0s4h
    ./MAKEDEV da1 da1s1h da1s2h da1s3h da1s4h
    ./MAKEDEV da2 da2s1h da2s2h da2s3h da2s4h
    ./MAKEDEV da3 da3s1h da3s2h da3s3h da3s4h
    ./MAKEDEV bktr0 cd1
    if test -d /dev/vinum; then
      # 'vinum makedev' can only create devices in /dev, thus use cpio.
      cd /dev; find vinum -print | cpio -pv ${DESTDIR}/dev
    fi

    # Make the floppy group wheel writable.
    chown root:wheel ${DESTDIR}/dev/fd0*
    chmod g+w ${DESTDIR}/dev/fd0*
    ;;

  5.*)
    # Make the floppy group wheel writable.
    printf '%s\n' 'own  fd0 root:wheel' >> ${DESTDIR}/etc/devfs.conf
    printf '%s\n' 'perm fd0 0660'       >> ${DESTDIR}/etc/devfs.conf
    ;;

  6.*)
    ;;

  *)
    printf '%s\n' "REVISION ${REVISION} not supported"
    exit 1
    ;;

  esac
}

# vim: tabstop=2:expandtab:shiftwidth=2:syntax=sh:
# EOF $RCSfile: stage_1.conf.default,v $

添付ファイル: filestage_1.conf.default 547件 [詳細] filestage_1.sh 523件 [詳細]

Last-modified: 2022-05-05 (木) 22:27:10