LoginSignup
0
0

More than 1 year has passed since last update.

VPS クラウド 仮想環境向け FreeBSDカスタムカーネル自動生成スクリプト

Last updated at Posted at 2021-10-20

このカーネルコンフィグでビルドすれば、
基本的に
KVM、Hyper-V、XENでしか動作しない
最軽量カーネルになります。

Usage

perl makekernelconf.pl NEWKERNELNAME
cd /usr/src
make buildkernel KERNCONF=NEWKERNELNAME
make installkernel KERNCONF=NEWKERNELNAME
makekernelconf.pl
#!/usr/bin/perl
# build by 00264 nano (C)NEET CO ltd.

$arch=`/usr/bin/uname -m`;
$arch=~s/[\r|\n]//g;
$generic="GENERIC";
$name=$ARGV[0];

if($name eq "") {
    print "Usage : $ARGV[0] kernel_name\n";
    exit;
}

&main($name);

sub main {
    my($name)=@_;

    # GENERICカーネルのコンフィグ読み込み
    my $fh=&safe_open("/usr/src/sys/$arch/conf/$generic");

    my $buf;
    foreach(<$fh>) {
        # 整形して、コメントをカットする
        chomp;
#       next if(/\t(^#)/);
#       while(/\t\t/) { s/\t\t/\t/g; }
#       s/\t#/#/g;
        s/#.*//g;

        # カーネル名の設定
        if(/(^ident)/) { s/GENERIC/$name/g; }

        # メイン部分の削除
        next if(/DEBUG\=/);

if(0) { # debug onlly

        next if(/\t(MD_ROOT)\t/);
#       next if(/\t(NFSD)\t/);
#       next if(/\t(NFS_ROOT)\t/);
#       next if(/\t(MSDOSFS)\t/);
#       next if(/\t(CD9660)\t/);
        next if(/\t(GEOM_RAID)\t/);
        next if(/\t(COMPAT_FREEBSD32)/);
        next if(/\t(COMPAT_FREEBSD4)/);
        next if(/\t(COMPAT_FREEBSD5)/);
        next if(/\t(COMPAT_FREEBSD6)/);
        next if(/\t(COMPAT_FREEBSD7)/);
        next if(/\t(COMPAT_FREEBSD8)/);
        next if(/\t(COMPAT_FREEBSD9)/);
        next if(/\t(COMPAT_FREEBSD10)/);
        next if(/\t(MAC)\t/);

        # フロッピーディスクドライブの削除
        next if(/\t(fdc)\t/);

        # サードパーティのATAコントローラーの削除
        next if(/\t(mvc)\t/);
        next if(/\t(siis)\t/);

        # SCSIコントローラの削除
        next if(/\t(ahc|AHC)\t/);
        next if(/\t(ahd|AHD)\t/);
        next if(/\t(esp|hptiop|isp|mpt|mps|ncr|sym|trm|adv|adw|aic|bt|isci)\t/);

        # SCSIの不要なコントローラの削除
        next if(/\t(sa|cd|pass|ses|ctl)\t/);

        # RAIDコントローラの削除
        next if(/\t(amr|arcmsr|asr|ciss|dpt|hptmv|htpnr|hptrr|hpt27xx|iir|ips|mly|twa|tws)\t/);
        next if(/\t(aac|aacp|aacraid|ida|mfi|mlx|pst|twe)\t/);

        # PS/2マウス、画面の不要なものの削除
        next if(/\t(psm|splash|SC_PIXEL_MODE)\t/);

        # PCMCIA/PCCARDの削除
        next if(/\t(cbb|pccard|cardbus)\t/);

        # RS232C/パラレルポートの削除
        next if(/\t(uart|ppc|ppbus|lpt|ppi|vpo|puc)/);
        next if(/ppc/);

        # イーサーネットコントローラの削除 (de emは残す)
        next if(/\t(bxe|igb|ixgbe|le|ti|txp|vx)\t/);

        # miibusイーサーネットコントローラの削除
        next if(/\t(miibus|ae|age|alc|ale|bce|bfe|bge|cas|dc|et|fxp|gem)\t/);
        next if(/\t(hme|jme|lge|msk|nfe|nge|nve|pcn|re|rl|sf|sge|sis|sk)\t/);
        next if(/\t(ste|stge|tl|tx|vge|vr|wb|xl)\t/);
        next if(/\t(cs|ed|ex|ep|fe|sn|xe)\t/);
        next if(/\t(wlan|wlan_web|wlan_ccmp|wlan_tkip|wlan_amrr|IEEE80211_DEBUG|IEEE80211_AMPDU_AGE|IEEE80211_SUPPORT_MESH|an|ath|ath_pci|ath_hal|AH_SUPPORT_AR5416|AH_AR5416_INTERRUPT_MITIGATION|ATH_ENABLE_11N|ath_rate_sample|ATH|AH_AR|bwi|bwn|ipw|iwi|iwn)\t/);
        next if(/\t(malo|mwl|ral|wi|wpi)\t/);
        next if(/\t(padlock_rng|rdrand_rng|vlan|tun|md|gif|faith|firmware)\t/);
        next if(/\t(USB_DEBUG|uhci|ohci|ehci|xhci|usb|ukbd|umass)\t/);
        next if(/\t(sound|snd)/);
        next if(/\t(mmc|mmcsd|sdhci)\t/);
        next if(/\t(virtio|vtnet|hyperv|XEN|xen|vmx)/);
} # debug onlly
        $buf.="$_\n";
    }

    while($buf=~/\t\t/) { $buf=~s/\t\t/\t/g; }
    while($buf=~/\n\n/) { $buf=~s/\n\n/\n/g; }
    $buf.=<<EOM;
device  virtio
device  virtio_pci
device  vtnet
device  virtio_blk
device  virtio_scsi
device  virtio_balloon
device  hyperv
#options XENHVM
#device xenpci
#device vmx
options ACCEPT_FILTER_DATA
options ACCEPT_FILTER_DNS
options ACCEPT_FILTER_HTTP

options IPFIREWALL
options IPFIREWALL_VERBOSE
options IPFIREWALL_VERBOSE_LIMIT=10000
options IPFIREWALL_DEFAULT_TO_ACCEPT
EOM

    close($fh);

    my $bunner=<<EOM;
# @{[`uname|perl -pe 's/\n//g'`]} @{[`uname -r|perl -pe 's/\n//g'`]}/@{[`uname -m|perl -pe 's/\n//g'`]} for $name
EOM
    my $fh=&safe_open(">/usr/src/sys/$arch/conf/$name");
    print $fh $bunner;
    print $fh $buf;
    close($fh);
}

sub safe_open {
    my($fn)=@_;
    open my $fh, $fn ||die "$fh can't open";
    print "Open $fn\n";
    $fh;
}

補足

カーネルのメジャーアップグレードがされると動作しなくなりますので、適時変更して下さい。

このカーネルではNFSやMSDOSFS、ISO9660も有効になっていますが、要らなければ取り除いて下さい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0