HPCメモ

HPC(High Performance Computing)に関連したりしなかったりすることのメモ書き

VMwareからUSBメモリへの移行

ここ1年くらいは、Windowsマシン上でVMware playerを立ちあげて、その中でArch Linuxを使ってたんですが、ホストのPCがメモリ4GBしか積んでいない上にWindows用に1.5GBくらい使われるので、そろそろネイティブ環境に移行しようと思ってました。
しかし、普通にHDDに入れたのでは面白くない可搬性が失なわれてよろしくない。
さて、どうしようかな~と思っていた時にこんなものを見つけました。

サンディスク Sandisk USBメモリ 32GB Extreme USB3.0 高速190MB/s 純正品 並行輸入品 パッケージ品 SDCZ80-032Gmedium]

こいつに、Archを入れてついでにNTFSのパーティションを作ってそっちにvirtual boxでも放り込んでおけば、快適なんじゃなかろうか?
ということで、さっそくポチっとしてセットアップしてみました。

事前情報

Arch linux wikiには、USBへのインストールに関する記事があります。
https://wiki.archlinux.org/index.php/Installing_Arch_Linux_on_a_USB_key
ここの情報によると、次の3点に気をつけておけば特に問題は無さそうです。

  • ファイルシステムext4にしてジャーナルを無効にしておく
  • noatimeオプションを指定してマウントする
  • パーティションの指定にUUIDを使う


作業の流れはこんな感じです。

  1. USBメモリのパーティショニング
  2. 既存のArchからchrootしてインストール
  3. インストール後の環境設定
  4. VMware playerから起動するための設定
  5. Virtual Boxのインストール&設定

USBメモリのパーティショニング

USBメモリをPCに挿して、VMware playerのメニューから
「取り外し可能デバイス」→「SanDisk Extream」→「接続(ホストから切断)」を選びます。
VMware 内のArchLinuxから

# fdisk  -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x10f42013

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    41943039    20970496   83  Linux

Disk /dev/sdb: 32.0 GB, 32017047552 bytes, 62533296 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              32    62530623    31265296    7  HPFS/NTFS/exFAT

で、fdisk /dev/sdbすると

Command (m for help): p

Disk /dev/sdb1: 32.0 GB, 32015663104 bytes, 62530592 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e697373

This doesn't look like a partition table
Probably you selected the wrong device.

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb1p1   ?  1936269394  3772285809   918008208   4f  QNX4.x 3rd part
/dev/sdb1p2   ?  1917848077  2462285169   272218546+  73  Unknown
/dev/sdb1p3   ?  1818575915  2362751050   272087568   2b  Unknown
/dev/sdb1p4   ?  2844524554  2844579527       27487   61  SpeedStor

Partition table entries are not in disk order

なんて言われる。
良くある状態らしいので、気にせずに全部のパーティションを削除したうえで
先頭から4GBをNTFSで確保して、残りをLinux パーティションで確保

Command (m for help): p

Disk /dev/sdb1: 32.0 GB, 32015663104 bytes, 62530592 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e697373

     Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62530591, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62530591, default 62530591): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): p

Disk /dev/sdb1: 32.0 GB, 32015663104 bytes, 62530592 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e697373

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb1p1            2048     8390655     4194304   83  Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 7
Changed type of partition 'Linux' to 'HPFS/NTFS/exFAT'

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (8390656-62530591, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-62530591, default 62530591):
Using default value 62530591
Partition 2 of type Linux and of size 25.8 GiB is set

Command (m for help): p

Disk /dev/sdb1: 32.0 GB, 32015663104 bytes, 62530592 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6e697373

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb1p1            2048     8390655     4194304    7  HPFS/NTFS/exFAT
/dev/sdb1p2         8390656    62530591    27069968   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 22: Invalid argument.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

この状態で、一旦VMware側から切り離してホストのWindows側で見ると・・・なぜか全領域がNTFSに。
あれれ?
とりあえずgpartedで切り直せば大丈夫だったので放置して先に進みます。

インストールスクリプトのインストール

Arch Linuxのインストーラはただのlive CD+スクリプトのisoイメージなので、スクリプトをインストールしてしまえば、既存のArch Linuxのsystemから新規インストールをすることができます。
当然ながら、既存のシステムの設定は一切反映されないので勘違いしないように*1

# pacman -S arch-install-scripts
resolving dependencies...
looking for inter-conflicts...

Packages (1): arch-install-scripts-11-1

Total Download Size:    0.01 MiB
Total Installed Size:   0.05 MiB

:: Proceed with installation? [Y/n] Y
:: Retrieving packages ...
arch-install-script...     6.1 KiB  42.0K/s 00:00 [######################] 100%
(1/1) checking keys in keyring                     [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
(1/1) checking available disk space                [######################] 100%
(1/1) installing arch-install-scripts              [######################] 100%

ファイルシステムの作成

ジャーナリング機能をオフにするために、-O ^has_journalを指定してext4で1/dev/sdbをフォーマットします。

# mkfs -t ext4 -O ^has_journal /dev/sdb2
mke2fs 1.42.7 (21-Jan-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1692432 inodes, 6767616 blocks
338380 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
207 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done

あとは、だいたいinstallation guideのとおりなので、コマンドだけ並べときます。
行頭が#だけの行は既存のArchで、sh-4.2が付いた行は、chrootした環境下で実行したコマンドです。

# mount /dev/sdb2 /mnt
# pacstrap /mnt base base-devel
# genfstab -p -U /mnt >>/mnt/etc/fstab
# arch-chroot /mnt
sh-4.2# echo usbkey >/etc/hostname
sh-4.2# cat /etc/hostname
usbkey
sh-4.2# ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
sh-4.2# ls -l /etc/localtime
lrwxrwxrwx 1 root root 30 Jul 17 06:14 /etc/localtime -> /usr/share/zoneinfo/Asia/Tokyo
sh-4.2# sed -i /ja_JP.UTF-8/s/^#// /etc/locale.gen
sh-4.2# locale-gen
Generating locales...
  ja_JP.UTF-8... done
Generation complete.
sh-4.2# locale -a
C
POSIX
ja_JP.utf8
sh-4.2# echo 'LANG="'`locale -a 2>/dev/null |tail -1|sed -e's/$/"/'`>/etc/locale.conf
sh-4.2# cat /etc/locale.conf
LANG="ja_JP.utf8"
sh-4.2# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

sh-4.2# mkinitcpio -p linux
==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'
  -> -k /boot/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/initramfs-linux.img
==> Starting build: 3.9.9-1-ARCH
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [autodetect]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
==> Generating module dependencies
==> Creating gzip initcpio image: /boot/initramfs-linux.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'fallback'
  -> -k /boot/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-fallback.img -S autodetect
==> Starting build: 3.9.9-1-ARCH
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
==> WARNING: Possibly missing firmware for module: aic94xx
==> WARNING: Possibly missing firmware for module: bfa
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
==> Generating module dependencies
==> Creating gzip initcpio image: /boot/initramfs-linux-fallback.img
==> Image generation successful

なんかWARNが出てるけど問題無いらしい。
http://www.gauthampdas.com/blog/tech/linux/archlinux-mkinitcpio-and-warnings-about-missing-firmware-modules



そういえば、前述のArch wikiには

Before creating the initial RAM disk # mkinitcpio -p linux, in /etc/mkinitcpio.conf add the block hook to the hooks array right after udev. This is necessary for appropriate module loading in early userspace.

って書かれてたけど、

sh-4.2# sed -n /^HOOKS/p /etc/mkinitcpio.conf
HOOKS="base udev autodetect modconf block filesystems keyboard fsck"

ってことで、既にmkinitcpio.confの有効なHOOKS行にblockが含まれてたので、この変更は不要なようです。

ブートローダの設定

最後にgrubをインストールして終わり

sh-4.2# pacman -S grub
sh-4.2# grub-install --recheck /dev/sdb
/usr/bin/grub-bios-setup: warning: your embedding area is unusually small.  core.img won't fit in it..
/usr/bin/grub-bios-setup: warning: Embedding is not possible.  GRUB can only be installed in this setup by using blocklists.  However, blocklists are UNRELIABLE and their use is discouraged..
/usr/bin/grub-bios-setup: error: will not proceed with blocklists.

と思いきや・・・うーむ??

なんじゃコリャと思ったけど、Arch Linuxのwikiページに答えが書いてました。

https://wiki.archlinux.org/index.php/GRUB2#Master_Boot_Record_.28MBR.29_specific_instructions
a post-MBR gap of about 1 to 2 MiB is recommended to provide sufficient room for embedding GRUB's core.img

ってことらしいんですが、fdiskすると先頭パーティションが32sectorから始まってるので、16KBしか無いのが問題みたいです。

sh-4.2# fdisk -l /dev/sdb

Disk /dev/sdb: 32.0 GB, 32017047552 bytes, 62533296 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00006ed8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              32     8390655     4195312    7  HPFS/NTFS/exFAT
/dev/sdb2         8390656    62531583    27070464   83  Linux

gparted を起動して先頭に2MBほど空きを作ります。

f:id:n_so5:20130719001641p:plain

で再びgrub-installすると

sh-4.2# grub-install --recheck /dev/sdb
Installation finished. No error reported.
sh-4.2# grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-linux
Found initrd image: /boot/initramfs-linux.img
done

この状態でPCを(VMwareではなく)再起動すると、無事にGRUBが呼ばれてUSBからLinuxが起動できました。
長くなったので、今日はここまで。

*1:最初はてっきりクローンを作れる仕組みなのかと思ってたんだけど、単に既存のシステムでmini rootの代わりをするだけのようです。