前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >制作属于自己的Linux系统

制作属于自己的Linux系统

作者头像
用户1456517
发布2019-03-05 16:14:33
3.1K0
发布2019-03-05 16:14:33
举报
文章被收录于专栏:芝麻实验室芝麻实验室

自制Linux首先得满足一定的条件,除了物理主机的配置外,我们还需要准备一块干净的磁盘。这里,为了更好更直观地体现实验效果,笔者使用VMWare做实验,并准备了1块名为"LinuxDIY"的虚拟磁盘,磁盘大小为10GB。关于VMware的使用及系统安装,可以参看这篇文章。

注:VMware® Workstation 12 Pro是 VMware, Inc 的商业产品,点此下载试用版

1. 首先,查看我们当前实验主机的磁盘使用情况:

代码语言:javascript
复制
[root@Centos6 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   30G  0 disk
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 27.8G  0 part /
sdb      8:16   0   10G  0 disk
sr0     11:0    1 1024M  0 rom

这里的sdb即用于自制目标Linux系统的磁盘

2. 现在我们对/dev/sdb进行分区格式化。这里暂时先分2个区,即boot分区根分区,其大小分别为500MB(/dev/sdb1)和7GB(/dev/sdb2)。(注:你应该视根据实际情况来决定分区的数量和大小,此处为示范)

代码语言:javascript
复制
[root@Centos6 ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x6b763459.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6b763459

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +500M

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (66-1305, default 66):
Using default value 66
Last cylinder, +cylinders or +size{K,M,G} (66-1305, default 1305): +7GB

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6b763459

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          65      522081   83  Linux
/dev/sdb2              66         917     6843690   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

分区后,请务必使用partx diskName命令通知内核读取新的分区表,否则内核中还是旧的分区信息

代码语言:javascript
复制
[root@Centos6 ~]# partx /dev/sdb
# 1:        63-  1044224 (  1044162 sectors,    534 MB)
# 2:   1044225- 14731604 ( 13687380 sectors,   7007 MB)
# 3:         0-       -1 (        0 sectors,      0 MB)
# 4:         0-       -1 (        0 sectors,      0 MB)

接下来,我们使用mkfs命令对分区进行格式化操作。(≥CentOS 6.0且≤CentOS 7.0的系统版本建议选择ext4格式; CentOS 7及以上建议选择xfs格式)

代码语言:javascript
复制
[root@Centos6 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
130560 inodes, 522080 blocks
26104 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
64 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@Centos6 ~]# mkfs.ext4 /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
428240 inodes, 1710922 blocks
85546 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1753219072
53 block groups
32768 blocks per group, 32768 fragments per group
8080 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

使用blkid命令确认格式化是否成功

代码语言:javascript
复制
[root@Centos6 ~]# blkid
/dev/sda1: UUID="3f86a9dc-ff91-45a7-aba3-0f1650fd3710" TYPE="ext4"
/dev/sda2: UUID="cd7b1e2a-a71d-4abd-85e3-48f86be97065" TYPE="swap"
/dev/sda3: UUID="a16dc872-9b0b-45f0-9385-a54db36932ee" TYPE="ext4"
/dev/sdb1: UUID="6653f52b-7723-4094-b7da-616880915d2f" TYPE="ext4"    #用作boot的分区
/dev/sdb2: UUID="5c9804c1-192e-4abb-bae6-f273ef1169cc" TYPE="ext4"    #用作root的分区

3. df 查看当前主机的挂载情况

代码语言:javascript
复制
[root@Centos6 ~]# df
Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/sda3       28565504 747452  26360340   3% /
tmpfs             502056      0    502056   0% /dev/shm
/dev/sda1         194241  31787    152214  18% /boot

至此,系统“硬配置”方面我们已经完成了,但是用于新系统的boot分区和根分区中还没有任何数据,因此我们需要进一步完成相应的“软配置”。具体步骤为:创建临时挂载目录 -> 挂载boot和根分区 -> 写入必要数据

  • Ⅰ. 创建临时挂载目录
代码语言:javascript
复制
[root@Centos6 ~]# mkdir /mnt/boot
[root@Centos6 ~]# mkdir /mnt/root
  • Ⅱ. 挂载对应分区
代码语言:javascript
复制
[root@Centos6 ~]# mount /dev/sdb1 /mnt/boot/
[root@Centos6 ~]# mount /dev/sdb2 /mnt/root/
[root@Centos6 ~]# df
Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/sda3       28565504 747464  26360328   3% /
tmpfs             502056      0    502056   0% /dev/shm
/dev/sda1         194241  31787    152214  18% /boot
/dev/sdb1         497412   2318    468990   1% /mnt/boot
/dev/sdb2        6605060  15036   6247840   1% /mnt/root
  • Ⅲ. 写入必要数据
    • 写入grub数据
代码语言:javascript
复制
[root@Centos6 ~]# grub-install --root-directory=/mnt /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb
代码语言:javascript
复制
- 复制```vmlinuz```和```initramfs```文件到挂载好的```boot```中
代码语言:javascript
复制
[root@Centos6 ~]# cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /boot/initramfs-2.6.32-696.el6.x86_64.img /mnt/boot/
[root@Centos6 ~]# ll /mnt/boot/    #查看是否成功写入/拷贝grub及相关系统启动文件
total 26860
drwxr-xr-x. 2 root root     1024 Aug 19 06:51 grub
-rw-------. 1 root root 23212280 Aug 19 06:56 initramfs-2.6.32-696.el6.x86_64.img
drwx------. 2 root root    12288 Aug 19 06:43 lost+found
-rwxr-xr-x. 1 root root  4274992 Aug 19 06:56 vmlinuz-2.6.32-696.el6.x86_64
代码语言:javascript
复制
- 创建并手动编辑```grub.conf文件```(安装完grub,并不会自动生成该文件,故需要手动创建)
代码语言:javascript
复制
[root@Centos6 ~]# vim /mnt/boot/grub/grub.conf
[root@Centos6 ~]# cat /mnt/boot/grub/grub.conf
default 0    #默认菜单启动项
timeout=3    #菜单选择时延
title LinuxDIY    #菜单名称的自定义设置
    root(hd0,0)    #指明相对于boot的根分区位置,其中(hd0,0)指的是第1块磁盘的第1个分区
    kernel /vmlinuz-2.6.32-696.el6.x86_64 root=/dev/sda2 selinux=0 init=/bin/bash  #内核文件路径
    initrd /initramfs-2.6.32-696.el6.x86_64.img  #initramfs文件路径
代码语言:javascript
复制
- 在目标系统的根分区下,创建目标系统的各个一级目录
代码语言:javascript
复制
[root@Centos6 ~]# mkdir -p /mnt/root/{etc,lib,lib64,bin,sbin,tmp,var,usr,sys,proc,opt,home,root,boot,dev,mnt,media}
[root@Centos6 ~]# tree /mnt/root/
/mnt/root/
├── bin
├── boot
├── dev
├── etc
├── home
├── lib
├── lib64
├── lost+found
├── media
├── mnt
├── opt
├── proc
├── root
├── sbin
├── sys
├── tmp
├── usr
└── var
代码语言:javascript
复制
- 复制想要用于目标系统的```库文件```和```命令```到根分区,[点此](https://github.com/zhimajihua/System-Management-Tools/blob/master/SystemDIY/copyScript.sh)下载该脚本
代码语言:javascript
复制
[root@Centos6 ~]# bash copyScript.sh
Please enter an executable inCMD/g name(eg: ls; cp): bash
Please enter an executable inCMD/g name(eg: ls; cp): ls
Please enter an executable inCMD/g name(eg: ls; cp): cat
Please enter an executable inCMD/g name(eg: ls; cp): vim
Please enter an executable inCMD/g name(eg: ls; cp): reboot
Please enter an executable inCMD/g name(eg: ls; cp): poweroff
Please enter an executable inCMD/g name(eg: ls; cp): mv
Please enter an executable inCMD/g name(eg: ls; cp): cp
Please enter an executable inCMD/g name(eg: ls; cp): scp
Please enter an executable inCMD/g name(eg: ls; cp): ssh
Please enter an executable inCMD/g name(eg: ls; cp): rm
Please enter an executable inCMD/g name(eg: ls; cp): mkdir
Please enter an executable inCMD/g name(eg: ls; cp): ping
Please enter an executable inCMD/g name(eg: ls; cp): quit
Now, program will exit.
代码语言:javascript
复制
- 尝试使用```chroot```命令切换根目录,如能成功,则说明拷贝相关文件都已成功
代码语言:javascript
复制
[root@Centos6 ~]# chroot /mnt/root
bash-4.1# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  sys  tmp  usr  var
bash-4.1# exit
exit

4. 如上,相关工作已完成。接下来关机,并拆除该硬盘。

代码语言:javascript
复制
[root@Centos6 ~]# poweroff
Broadcast message from root@Centos6
        (/dev/pts/0) at 7:29 ...
The system is going down for power off NOW!

5. 在目标主机装载名为"LinuxDIY"的虚拟磁盘,并开机。不出意外,你应该能看到和笔者一样的界面,这也意味着你制作好了属于自己的Linux系统。

1
1
2
2

6. 怎么样,是不是很开心(~ ̄▽ ̄)~


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/09/08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档