6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

「実機に Alpine Linux をインストールするときパーティション分けてぇよなぁ」と思ってやったこと

Last updated at Posted at 2020-12-09

はじめに

Linux Advent Calendar 2020 の18日目の記事です。
まさか今年の Advent Calendar でこの記事ともう1本の2本を書くとは思ってなかった Ze!

概要

みなさんは実機に Alpine Linux をインストールした時に「あぁ、HDD まるまる1台分に システム突っ込んじゃったなぁ」となげいたことがあると思います。
Google 先生曰く、「インストールしたあとに gparted や gdisk を使ってパーティションを分けてますね」と教えてくれます。
この記事はそんな Alpine Linux をインストール中にパーティションを分けるための Tips です。

構成

  • FMV-D3260
  • Alpine Linux 3.12
    • x86_64
    • 「How would you like to use it?:」には sys を選択しています。
    • 今回は STANDARD の ISO を microSD カードに焼いて USB ブートするようにしています。
      • 説明のため(仮想環境でシリアルコンソールを使う関係上) VIRTUAL の ISO を使います。
    • NETBOOT の ISO でも busybox の vi が使えれば同じことが出来ると思います。やったことないですけど(真顔
      • パーティション分けた後だったら MINI ROOT を突っ込む感じなんですかね?やったことないのでわからないですけど(真りゃ

自分がやったこと

setup-alpine を実行中に setup-disk が呼び出されるようなので setup-disk に手を加えます。

console
localhost:~# which setup-disk
/sbin/setup-disk
localhost:~# cp /sbin/setup-disk /sbin/setup-disk.orig
localhost:~# vi /sbin/setup-disk
localhost:~# diff /sbin/setup-disk.orig /sbin/setup-disk
--- /sbin/setup-disk.orig
+++ /sbin/setup-disk
@@ -1001,7 +1001,7 @@
    local prep_size=8
    local boot_size=${BOOT_SIZE:-100}
    local swap_size=${SWAP_SIZE}
-   local root_size=${ROOT_SIZE}
+   local root_size=${ROOT_SIZE:-1000}
    local root_dev= boot_dev= swap_dev=
    init_progs $(select_bootloader) || return 1
    confirm_erase $@ || return 1
localhost:~# 

setup-disk の「native_disk_install」内にある root_size に MB 換算で指定します。
ここでは root_size に 1[GB]くらい(1000[MB])に指定して保存しています。
GitHub にある setup-disk.in を見ながら「local root_size」を探して編集するのもあると思います。

console
localhost:~# setup-alpine
Available keyboard layouts:
af     be     cn     fi     hu     it     lk     mm     pl     sy     uz
al     bg     cz     fo     id     jp     lt     mt     pt     th     vn
am     br     de     fr     ie     ke     lv     my     ro     tj
ara    brai   dk     gb     il     kg     ma     ng     rs     tm
at     by     dz     ge     in     kr     md     nl     ru     tr
az     ca     ee     gh     iq     kz     me     no     se     tw
ba     ch     epo    gr     ir     la     mk     ph     si     ua
bd     cm     es     hr     is     latam  ml     pk     sk     us
Select keyboard layout [none]: jp
Available variants: jp-OADG109A jp-dvorak jp-kana jp-kana86 jp-mac jp 
Select variant []: jp
 * Caching service dependencies ...
 [ ok ]
 * Setting keymap ...
 [ ok ]
Enter system hostname (short form, e.g. 'foo') [localhost]: 
Available interfaces are: eth0.
Enter '?' for help on bridges, bonding and vlans.
Which one do you want to initialize? (or '?' or 'done') [eth0] 
Ip address for eth0? (or 'dhcp', 'none', '?') [dhcp] 
Do you want to do any manual network configuration? [no] 
udhcpc: started, v1.31.1
udhcpc: sending discover
udhcpc: sending select for 192.168.1.2
udhcpc: lease of 192.168.1.2 obtained, lease time 86400
Changing password for root
New password: 
Bad password: too short
Retype password: 
passwd: password for root changed by root
Which timezone are you in? ('?' for list) [UTC] Japan
 * Starting busybox acpid ...
 [ ok ]
 * Starting busybox crond ...
 [ ok ]
HTTP/FTP proxy URL? (e.g. 'http://proxy:8080', or 'none') [none] 
Which NTP client to run? ('busybox', 'openntpd', 'chrony' or 'none') [chrony] 
 * service chronyd added to runlevel default
 * Caching service dependencies ...
 [ ok ]
 * Starting chronyd ...
 [ ok ]

Available mirrors:
1) dl-cdn.alpinelinux.org
2) uk.alpinelinux.org
3) dl-2.alpinelinux.org
4) dl-4.alpinelinux.org
5) dl-5.alpinelinux.org
6) dl-8.alpinelinux.org
7) mirror.yandex.ru
8) mirrors.gigenet.com
9) mirror1.hs-esslingen.de
10) mirror.leaseweb.com
11) mirror.fit.cvut.cz
12) alpine.mirror.far.fi
13) alpine.mirror.wearetriple.com
14) mirror.clarkson.edu
15) linorg.usp.br
16) ftp.yzu.edu.tw
17) mirror.aarnet.edu.au
18) speglar.siminn.is
19) mirrors.dotsrc.org
20) ftp.halifax.rwth-aachen.de
21) mirrors.tuna.tsinghua.edu.cn
22) mirrors.ustc.edu.cn
23) mirrors.xjtu.edu.cn
24) mirrors.nju.edu.cn
25) mirror.lzu.edu.cn
26) ftp.acc.umu.se
27) mirror.xtom.com.hk
28) mirror.csclub.uwaterloo.ca
29) alpinelinux.mirror.iweb.com
30) pkg.adfinis-sygroup.ch
31) mirror.ps.kz
32) mirror.rise.ph
33) mirror.operationtulip.com
34) mirrors.ircam.fr
35) alpine.42.fr
36) mirror.math.princeton.edu
37) mirrors.sjtug.sjtu.edu.cn
38) ftp.icm.edu.pl
39) mirror.ungleich.ch
40) alpine.mirror.vexxhost.ca
41) sjc.edge.kernel.org
42) ewr.edge.kernel.org
43) ams.edge.kernel.org
44) download.nus.edu.sg
45) alpine.yourlabs.org
46) mirror.pit.teraswitch.com
47) mirror.reenigne.net
48) quantum-mirror.hu
49) tux.rainside.sk
50) alpine.cs.nctu.edu.tw
51) mirror.ihost.md
52) mirror.ette.biz
53) mirror.lagoon.nc
54) alpinelinux.c3sl.ufpr.br

r) Add random from the above list
f) Detect and add fastest mirror from above list
e) Edit /etc/apk/repositories with text editor

Enter mirror number (1-54) or URL to add (or r/f/e/done) [1]: 
Added mirror dl-cdn.alpinelinux.org
Updating repository indexes... done.
Which SSH server? ('openssh', 'dropbear' or 'none') [openssh] 
 * service sshd added to runlevel default
 * Caching service dependencies ...
 [ ok ]
ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 
 * Starting sshd ...
 [ ok ]
Available disks are:
  sda   (4.3 GB ATA      QEMU HARDDISK   )
Which disk(s) would you like to use? (or '?' for help or 'none') [none] ?  

The disk you select can be used for a traditional disk install or for a
data-only install.

The disk will be erased.

Enter 'none' if you want to run diskless.

Available disks are:
  sda   (4.3 GB ATA      QEMU HARDDISK   )
Which disk(s) would you like to use? (or '?' for help or 'none') [none] sda
The following disk is selected:
  sda   (4.3 GB ATA      QEMU HARDDISK   )
How would you like to use it? ('sys', 'data', 'lvm' or '?' for help) [?] sys
WARNING: The following disk(s) will be erased:
  sda   (4.3 GB ATA      QEMU HARDDISK   )
WARNING: Erase the above disk(s) and continue? [y/N]: y
Creating file systems...
Installing system on /dev/sda3:
/mnt/boot is device /dev/sda1
100% ############################################==> initramfs: creating /boot/initramfs-virt
/boot is device /dev/sda1

Installation is complete. Please reboot.
localhost:~# 

インストールが終わったので、fdisk でパーティションを覗いてみます。

console
localhost:~# fdisk -l /dev/sda
Disk /dev/sda: 4096 MB, 4294967296 bytes, 8388608 sectors
522 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device  Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
/dev/sda1 *  0,32,33     12,223,19         2048     206847     204800  100M 83 Linux
/dev/sda2    12,223,20   40,202,34       206848     655359     448512  219M 82 Linux swap
/dev/sda3    40,202,35   168,70,30       655360    2703359    2048000 1000M 83 Linux
localhost:~# reboot

きちんと 1000[MB]が割り当てられていますね!
パーティションの確認ができたら起動確認も兼ねて再起動します。

console
localhost:~# fdisk -v
fdisk: unrecognized option: v
BusyBox v1.31.1 () multi-call binary.

Usage: fdisk [-ul] [-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] DISK

Change partition table

    -u      Start and End are in sectors (instead of cylinders)
    -l      Show partition table for each DISK, then exit
    -b 2048     (for certain MO disks) use 2048-byte sectors
    -C CYLINDERS    Set number of cylinders/heads/sectors
    -H HEADS    Typically 255
    -S SECTORS  Typically 63
localhost:~# apk add util-linux
(1/12) Installing libcap-ng (0.7.10-r1)
(2/12) Installing setpriv (2.35.2-r0)
(3/12) Installing libmount (2.35.2-r0)
(4/12) Installing libsmartcols (2.35.2-r0)
(5/12) Installing findmnt (2.35.2-r0)
(6/12) Installing mcookie (2.35.2-r0)
(7/12) Installing hexdump (2.35.2-r0)
(8/12) Installing lsblk (2.35.2-r0)
(9/12) Installing libfdisk (2.35.2-r0)
(10/12) Installing sfdisk (2.35.2-r0)
(11/12) Installing cfdisk (2.35.2-r0)
(12/12) Installing util-linux (2.35.2-r0)
Executing busybox-1.31.1-r19.trigger
OK: 99 MiB in 67 packages
localhost:~# fdisk -v
fdisk from util-linux 2.35.2
localhost:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.35.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda: 4 GiB, 4294967296 bytes, 8388608 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x70aa4810

Device     Boot  Start     End Sectors  Size Id Type
/dev/sda1  *      2048  206847  204800  100M 83 Linux
/dev/sda2       206848  655359  448512  219M 82 Linux swap / Solaris
/dev/sda3       655360 2703359 2048000 1000M 83 Linux

Command (m for help): n
Partition type
   p   primary (3 primary, 0 extended, 1 free)
   e   extended (container for logical partitions)
Select (default e): p

Selected partition 4
First sector (2703360-8388607, default 2703360): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2703360-8388607, default 8388607): 

Created a new partition 4 of type 'Linux' and of size 2.7 GiB.

Command (m for help): p
Disk /dev/sda: 4 GiB, 4294967296 bytes, 8388608 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x70aa4810

Device     Boot   Start     End Sectors  Size Id Type
/dev/sda1  *       2048  206847  204800  100M 83 Linux
/dev/sda2        206848  655359  448512  219M 82 Linux swap / Solaris
/dev/sda3        655360 2703359 2048000 1000M 83 Linux
/dev/sda4       2703360 8388607 5685248  2.7G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

localhost:~# mkfs.ext4 /dev/sda4 
mke2fs 1.45.6 (20-Mar-2020)
Discarding device blocks: done                            
Creating filesystem with 710656 4k blocks and 177760 inodes
Filesystem UUID: 69a65f9a-ab72-4b25-834c-25c09b01f264
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

localhost:~# cp /etc/fstab /etc/fstab.orig
localhost:~# vi /etc/fstab
localhost:~# diff /etc/fstab.orig /etc/fstab
--- /etc/fstab.orig
+++ /etc/fstab
@@ -1,5 +1,6 @@
 UUID=630443c1-401c-47c3-8ff5-45f0cc454fa8  /   ext4    rw,relatime 0 1
 UUID=651df3d3-738b-4631-aed9-4a08b13fc535  /boot   ext4    rw,relatime 0 2
+UUID=69a65f9a-ab72-4b25-834c-25c09b01f264  /home   ext4    rw,relatime 0 2
 UUID=6bcc1a81-7f1c-4f78-b2c5-2172aafd25cb  swap    swap    defaults    0 0
 /dev/cdrom /media/cdrom    iso9660 noauto,ro 0 0
 /dev/usbdisk   /media/usb  vfat    noauto  0 0
localhost:~# reboot

他のディストリビューション同様 fdisk でパーティション切って ext4 でフォーマットし、表示されている UUID を fstab に記載します。
ただ BusyBox に付属している fdisk だと思うようにパーティションを切ることができなかったので util-linux のパッケージに付属している fdisk で実行しました。

console
localhost:~# mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,relatime,size=10240k,nr_inodes=12804,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime)
/dev/sda3 on / type ext4 (rw,relatime)
tmpfs on /run type tmpfs (rw,nodev,relatime,size=11236k,mode=755)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
tracefs on /sys/kernel/debug/tracing type tracefs (rw,nosuid,nodev,noexec,relatime)
/dev/sda1 on /boot type ext4 (rw,relatime)
/dev/sda4 on /home type ext4 (rw,relatime)
localhost:~# 

:thumbsup:

まとめ

setup-***** には宝があったぞ!!はざっくり読めば何やってるかわかるスクリプト群なのでインストール中にカスタムしたい人は GitHub のリポジトリを覗いてみると良かですよ!

6
3
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
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?